UPDATE Syntax in mysql 5.7 - php

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 !

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

Related

Error in SQL Syntax use near '06:09:43 WHERE Book_id = 19 AND Date_issued = 2017-03-29 17:28:34' at line 1

ERROR: Could not able to execute You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version for
the right syntax to use near '06:09:43 WHERE Book_id = 19 AND
Date_issued = 2017-03-29 17:28:34' at line 1
I've been getting the above error upon executing the following code -
if(isset($_GET['rt']))//rt is a variable I'm passing in an anchor tag
{
$dateis=$_GET['rt'];
}
if(isset($_GET['id']))//id is also sent along with rt
{
$bk_id=$_GET['id'];
}
$ret = 'returned successfully';
$date = date('Y-m-d H:i:s');
if(mysqli_query($conn, "UPDATE issued_books SET Date_returned = $date WHERE Book_id = $bk_id AND Date_issued = $dateis"))
{
$fine = mysqli_query($conn, "SELECT DATEDIFF('$dateis', '$date') AS days");
$fine = $fine*10;
mysqli_query($conn, "UPDATE issued_books SET Fine_amt = (Fine_amt + $fine) WHERE Date_issued = $dateis");
mysqli_query($conn, "UPDATE library_books SET Available = (Available + 1) WHERE Book_id = $bk_id");
header("Location: return-books.php?book=".urlencode($ret));
} else{
echo "ERROR: Could not able to execute " .mysqli_error($conn);
}
In your first update query use ' single quotes here
Date_returned = '$date'
I also suggest to use pdo
Try these, just add single quotes to variables
"UPDATE issued_books SET Date_returned = '$date' WHERE Book_id = '$bk_id' AND Date_issued = '$dateis'"
"UPDATE issued_books SET Fine_amt = (Fine_amt + '$fine') WHERE Date_issued = '$dateis'"
"UPDATE library_books SET Available = (Available + 1) WHERE Book_id = '$bk_id'"
Although it would be better if you use prepared statements.

MySQL query went wrong

can you say me where are i am making mistakes in this simple query
$q = "UPDATE users SET ".$aItemSlot." = '$seton' WHERE username='$us'";
$r = #mysqli_query($dbc, $q);
$q = "UPDATE items SET item_position='3' WHERE it_id='$seton'";
$r = #mysqli_query($dbc, $q);
error-
1064 - 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 ''$aItemSlot' = '$seton' WHERE username='$us'' at line 1
Here is my source
$q = "SELECT * FROM users WHERE username='$us'";
$r = #mysqli_query($dbc, $r);
$row = mysqli_fetch_array($r);
$aHelmet_Slot = $row['helmet_slot'];
$aShield_Slot = $row['shield_slot'];
$aWeapon_Slot = $row['weapon_slot'];
$aGloves_Slot = $row['g1loves_slot'];
$aShoes_Slot = $row['shoes_slot'];
$aArmor_Slot = $row['armor_slot'];
$aEar_Slot = $row['ear_slot'];
$aBelt_Slot = $row['belt_slot'];
$aRing1_Slot = $row['ring1_slot'];
$aRing2_Slot = $row['ring2_slot'];
$aRing3_Slot = $row['ring3_slot'];
$aRing4_Slot = $row['ring4_slot'];
$aCharLevel = $row['char_lvl'];
if ($aItemSlot == 'ring_slot'){
if($aCharLevel >= $aItem_Level){
$NotEmpty = false;
if ($aRing1_Slot == 0){
$q = "UPDATE users SET ring1_slot='$seton' WHERE username='$us'";
$r = #mysqli_query($dbc, $q);
$NotEmpty = true;
}
if (($aRing2_Slot == 0) && (!$NotEmpty)){
$q = "UPDATE users SET ring2_slot='$seton' WHERE username='$us'";
$r = #mysqli_query($dbc, $q);
$NotEmpty = true;
}
if (($aRing3_Slot == 0) && (!$NotEmpty)){
$q = "UPDATE users SET ring3_slot='$seton' WHERE username='$us'";
$r = #mysqli_query($dbc, $q);
$NotEmpty = true;
}
if(($aRing4_Slot == 0) && (!$NotEmpty)){
$q = "UPDATE users SET ring4_slot='$seton' WHERE username='$us'";
$r = #mysqli_query($dbc, $q);
$NotEmpty = true;
}
if(!$NotEmpty){
$q = "UPDATE items SET item_position='2' WHERE it_id='$aRing1_Slot'";
$r = #mysqli_query($dbc, $q);
$q = "UPDATE users SET ring1_slot='$seton' WHERE username='$us'";
$r = #mysqli_query($dbc, $q);
$NotEmpty = true;
}
$q = "UPDATE items SET item_position='3' WHERE it_id='$seton'";
$r = #mysqli_query($dbc, $q);
}
}
else
{
if ($aCharLevel >= $aItem_Level){
$link_slot_var = "a" .$aItemSlot;
$aSlotItemID = $$link_slot_var;
if($aSlotItemID <> 0){
$q = "UPDATE items SET item_position='2' WHERE it_id='$aSlotItemID'";
$r = #mysqli_query($dbc, $q);
}
$q = "UPDATE users SET '$aItemSlot' = '.$seton.' WHERE username='$us'; // it fails there
$r = #mysqli_query($dbc, $q);
$q = "UPDATE items SET item_position='3' WHERE it_id='$seton'";
$r = #mysqli_query($dbc, $q);
}
}
There Should not be a $ symbol before the mysql database field name it should be something like this
UPDATE users SET aItemSlot = '".$seton."' WHERE username='".$us."'
Modify your query in the above format and try to execute
Why do you use a table name as variable?
{$aItemSlot}
In general it should be like this:
$mysqli->query("Update users
set aItemSlot = '$seton'
where username = $us
") ;
Also, try to use prepared statements.
UPDATE
Make update of the row which related to this table:
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
SET col_name1={expr1|DEFAULT} [, col_name2={expr2|DEFAULT}] ...
[WHERE where_condition]
[ORDER BY ...]
[LIMIT row_count]
UPDATE statement updates columns of existing rows in the named table
with new values. The SET clause indicates which columns to modify and
the values they should be given. Each value can be given as an
expression, or the keyword DEFAULT to set a column explicitly to its
default value. The WHERE clause, if given, specifies the conditions
that identify which rows to update. With no WHERE clause, all rows are
updated. If the ORDER BY clause is specified, the rows are updated in
the order that is specified. The LIMIT clause places a limit on the
number of rows that can be updated.
You can also perform UPDATE operations covering multiple tables.
UPDATE items,month SET items.price=month.price
WHERE items.id=month.id;
UPDATE2
You need to check/update each row.
$stmt = $mysqli->prepare("UPDATE table SET col1 = ?, col2 = ?, col3 = ? WHERE id = ? ")
$stmt->bind_param('sssi', $var1, $var2, $var3, $id);
This shows what you need to do.
You missed assignment operator :
UPDATE users SET " . $aItemSlot . " = '" . $seton . "' WHERE username='$us'";
Your query should be like this:
"UPDATE users SET " . $aItemSlot . "='$seton' WHERE username='$us'";
^ assignment operator
This is a valid syntax.. how ever you have to be sure that those params are VALID before making the query...
$sql = "UPDATE users SET {$aItemSlot} = '{$seton}' WHERE username = '{$us}'";
Try this, this will surely work
$q = "UPDATE users SET ".$aItemSlot." = " . $seton . " WHERE username= " . $us;
You will need to make some slight adjustments.
PHP/SQL
$q = "UPDATE users SET aItemSlot = '".$seton."' WHERE username='".$us."'";
// Or if $aItemSlot actually is a variable
$q = "UPDATE users SET '".$aItemSlot."'='".$seton."' WHERE username='".$us."'";
Bottom note: because $aItemSlot starts with an 'a' I am wondering if this is an array. In that case your script will fail saying that the array to string conversion has failed. If this is the case, check what value $aItemSlot holds using var_dump().

clear votes greater than 50 php script

I only want to reset votes greater than 50, currently is resets every one
<?
include('mysql_connect.php');
$query = "SELECT id, votes, callback FROM websites";
$result = mysql_query($query) OR die(mysql_error());
$query = "UPDATE websites SET votes = 0";
$result = mysql_query($query) OR die(mysql_error());
$row = mysql_fetch_array($result, MYSQL_ASSOC);
echo ('Database has been cleaned with a tissue.');
?>
Im used to coding Java so im not sure if i can use this
$query = "UPDATE websites WHERE votes >= 50 SET votes = 0";
Thanks for your help
you're close. Do this instead.
$query = "UPDATE websites SET votes = 0 WHERE votes >= 50 ";
Also mysql_* functions are deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.
You need the WHERE statement after the SET:
$query = "UPDATE websites SET votes = 0 WHERE votes >= 50";

Same query works different

I made php script that updates database:
<?php
include 'config.php';
$PauseID = "2";
$ProductionID = "1411979966";
$sql = "SET #max = (SELECT MAX(Id) FROM tblproductionbreaks); UPDATE tblproductionbreaks SET IDPause = '$PauseID' WHERE ProductionID = '$ProductionID' AND Id = #max;";
mysql_query($sql) or die(mysql_error());
mysql_close($connect);
?>
While executing this script it returns 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 tblproductionbreaks SET IDPause = '2' WHERE ProductionID
= '1411979966' A' at line 1
But if I try same update query execute by command line it works
SET #max = (SELECT MAX(Id) FROM tblproductionbreaks); UPDATE tblproductionbreaks SET IDPause = '2' WHERE ProductionID = '1411979966' AND Id = #max;
I don't understand how the same thing works differently.
You can not run multiple queries as one statement with PHP. Try like this:
$sql = "UPDATE tblproductionbreaks SET IDPause = '$PauseID' WHERE ProductionID = '$ProductionID' ORDER BY Id DESC LIMIT 1;";
You don't need this variable anyway.
Its is because your PHP interpreter treats your $sql as string. Change your code to
$sql = "UPDATE tblproductionbreaks SET IDPause = '{$PauseID}' WHERE ProductionID = '{$ProductionID}' AND Id = (SELECT MAX(Id) FROM tblproductionbreaks)";
Also you can debug it by echo $sql and see what actually the $sql returning

SET and SELECT query not executing when run in PHP code

I have this php code -:
$q = "SET #session = '1', #buddys = '12,7,10', #rejects = 'post_0'; SELECT f.* FROM feed as f"; $r = mysqli_num_rows($q);.
This results in this 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 'SELECT f.* FROM feed as f' at line 3.
But surprisingly, when the same mysql query is run in phpmyadmin it runs as needed.
Whats the matter? Thanks for help...
your problem is the semicolone ; before select
try replace it by comma ,
like that
$q = "SET #session = '1', #buddys = '12,7,10', #rejects = 'post_0' , SELECT f.* FROM feed as f";
$r = mysqli_num_rows($q);
or you ould also separate your query like that
$q = "SET #session = '1', #buddys = '12,7,10', #rejects = 'post_0' ";
$q .= "SELECT f.* FROM feed as f";

Categories