Quick help..my eyes are tired.. what am I missing?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Quick help..my eyes are tired.. what am I missing?

Post by hybris »

What am I missing?

Code: Select all

   echo $Deviation_ID; //FOR TEST PURPOSE
    if ($stmt = $mysqli->prepare("SELECT Logfile FROM Deviation_Update_History WHERE ID = ?")) { 
      $stmt->bind_param('i', $Deviation_ID);
      $stmt->execute(); 
      $stmt->bind_result($Logfile);
      $stmt->store_result();
      $stmt->fetch();
         if (mysqli_num_rows($result) > 0) {
         $Logfile="1"; //Just for test
          if ($stmt = $mysqli->prepare("UPDATE Logfile SET Deviation_Update_History WHERE ID = ?")) { 
              $stmt->bind_param('is', $Deviation_ID, $Logfile);
              $stmt->execute(); 
              $stmt->close();
      } else { //If doesnt exist
      
          $Logfile="2"; //Just for test
          if ($stmt = $mysqli->prepare("INSERT INTO Logfile(Dev_ID, Logfile) VALUES (?) WHERE ID = ?")) { 
            $stmt->bind_param('is', $Deviation_ID, $Logfile);
            $stmt->execute();  
            $stmt->close();
           }
          }
      }
    }
I been staring at this for several minutes but can't find what I'm missing.

It wont work it skips the entire thing..

Thanks in advance
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Quick help..my eyes are tired.. what am I missing?

Post by requinix »

You know what I'm missing? Context. And a better description than "it skips the entire thing".

Uh, let's see...

Code: Select all

if (mysqli_num_rows($result) > 0) {
Where's $result? Do you mean to use $stmt->num_rows?
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: Quick help..my eyes are tired.. what am I missing?

Post by hybris »

Yeah I mean't to use $stmt->num_rows but it still doesn't work..

It wont update or write anything to the database.. it just print the dev ID (first line) and then skips the rest and move on.. I don't even get any error msg?

Well I'm late to pick up the kid. I'll take a closer look at this tomorrow.. Thanks anyway :)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Quick help..my eyes are tired.. what am I missing?

Post by Christopher »

hybris wrote:What am I missing?
How about some error checking?

Code: Select all

   echo $Deviation_ID; //FOR TEST PURPOSE
$stmt = $mysqli->prepare("SELECT Logfile FROM Deviation_Update_History WHERE ID = ?");
if ($mysqli->errno) {
    echo "ERROR: {$mysqli->error}<br>";
} else {
    if ($stmt) { 
...
    }
}
(#10850)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Quick help..my eyes are tired.. what am I missing?

Post by requinix »

Ah. Your indentation is wrong. Here's what it should look like:

Code: Select all

echo $Deviation_ID; //FOR TEST PURPOSE
if ($stmt = $mysqli->prepare("SELECT Logfile FROM Deviation_Update_History WHERE ID = ?")) { 
	$stmt->bind_param('i', $Deviation_ID);
	$stmt->execute(); 
	$stmt->bind_result($Logfile);
	$stmt->store_result();
	$stmt->fetch();
	if (mysqli_num_rows($result) > 0) {
		$Logfile="1"; //Just for test
		if ($stmt = $mysqli->prepare("UPDATE Logfile SET Deviation_Update_History WHERE ID = ?")) { 
			$stmt->bind_param('is', $Deviation_ID, $Logfile);
			$stmt->execute(); 
			$stmt->close();
		} else { //If doesnt exist

			$Logfile="2"; //Just for test
			if ($stmt = $mysqli->prepare("INSERT INTO Logfile(Dev_ID, Logfile) VALUES (?) WHERE ID = ?")) { 
				$stmt->bind_param('is', $Deviation_ID, $Logfile);
				$stmt->execute();  
				$stmt->close();
			}
		}
	}
}
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: Quick help..my eyes are tired.. what am I missing?

Post by hybris »

Ah. Your indentation is wrong. Here's what it should look like:
Haha, you made me spill my coffe sir :D

I was in a hurry so I was sloppy, I will do some error checking (Thanks Chrisopher) and I will fix my indentation :)
hybris
Forum Contributor
Posts: 172
Joined: Wed Sep 25, 2013 4:09 am

Re: Quick help..my eyes are tired.. what am I missing?

Post by hybris »

aaaand I found what the real problem was...

I switched place on Logfile (column) with Deviation_Update_History (Table)...

Duh.. now it works much better ;D
Post Reply