0

Im trying to run a simple UPDATE query:

UPDATE students SET enterTime = '".$enterTime."' WHERE s_id = '".$s_id."'; UPDATE timeLimit  SET listed = listed + 1  WHERE enterTime = '".$enterTime."' AND building = '".$building."';"

It works fine in MySql version 5.1 , my server has been upgraded to MySql version 5.7 and I get the following error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UPDATE students SET enterTime = '09:00' WHERE ' at line 1 BEGIN; UPDATE students SET enterTime = '09:00' WHERE s_id = '312433931' ; UPDATE timeLimit SET listed = listed + 1 WHERE enterTime = '09:00' AND building = '2'; UPDATE timeLimit SET listed = listed - 1 WHERE enterTime = '08:00' AND building = '2'; COMMIT;

PHP code:

function update_time_for_student($student,$enterTime,$oldTime){
    $s_id = $student['s_id'];
    $building = $student['building'];
    $query = "BEGIN; UPDATE students 
                SET enterTime = '".$enterTime."' 
                WHERE s_id = '".$s_id."' ;              
               UPDATE timeLimit 
                SET listed = listed + 1 
                WHERE enterTime = '".$enterTime."' AND building = '".$building."';
              UPDATE timeLimit 
                SET listed = listed - 1 
                WHERE enterTime = '".$oldTime."' AND building = '".$building."'; COMMIT;";
    if (mysql_query($query)){
        $fullName = $student['fname']." ".$student['lname'];
        send_email($student['email'],$enterTime,$fullName,$s_id);
        header("Location:/index.php?d=1"); 
    } else {
        echo mysql_error();
        echo "<br/>";
        echo $query;
        //header("Location:/index.php?d=3");
    }  
}

Thank you very much for your help !

Jocelyn
  • 11,209
  • 10
  • 43
  • 60
user3617337
  • 23
  • 1
  • 8

1 Answers1

1

Option-1

Try breaking the lines into multiple php statements:

$query = "BEGIN";
mysql_query($query) or die (mysql_error());

$query = "UPDATE students SET enterTime = '".$enterTime."' WHERE s_id = '".$s_id."';";
mysql_query($query) or die (mysql_error());

$query = "UPDATE timeLimit  SET listed = listed + 1  WHERE enterTime = '".$enterTime."' AND building = '".$building."';";
mysql_query($query) or die (mysql_error());

$query = "COMMIT";
mysql_query($query) or die (mysql_error());

Option-2

you need to use multi_query instead. Docs here

Note: mysql_* commands are deprecated and removed in PhP7. Instead use mysqli or PDO. Refer the accepted answer here

Community
  • 1
  • 1
Tamil
  • 1,193
  • 9
  • 24
  • My code looks like this : https://docs.google.com/document/d/14sIJUfwbGGzLQAVxOf5JCU6FO4zpPhMBu2sB_h4TiuM/edit?usp=sharing I get the following error on the line 53 ( highlighted in red in the code ) **Parse error: syntax error, unexpected T_VARIABLE** – user3617337 Aug 05 '16 at 12:02
  • You are missing `semi-colon` for previous line. `mysql_query($query) or die (mysql_error())` -> `mysql_query($query) or die (mysql_error());`. I have edited my answer too!! – Tamil Aug 05 '16 at 12:48