Page 1 of 1

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

Posted: Mon Apr 11, 2016 9:09 am
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

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

Posted: Mon Apr 11, 2016 9:16 am
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?

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

Posted: Mon Apr 11, 2016 9:30 am
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 :)

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

Posted: Mon Apr 11, 2016 3:26 pm
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) { 
...
    }
}

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

Posted: Mon Apr 11, 2016 5:55 pm
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();
			}
		}
	}
}

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

Posted: Wed Apr 13, 2016 1:10 am
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 :)

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

Posted: Wed Apr 13, 2016 3:41 am
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