Error in your SQL syntax. Update table issue - php

I really hate that error message, since it is the most useless error message in the history of man.
Anyhow I think I have stared at this VERY simple sql for an hour and still come up blank as to where it believes the problem is. Hope someone can help me or maybe some rubber ducking will do the trick.
The php code:
$sql = "UPDATE events SET titel = '$this->estart',
endTime = '$this->eend',
desc = '$this->desc',
dd = '$this->dDmed',
dato = '$this->dato',
ticketId = '$this->ticket' WHERE id = $this->id";
And the SQL error thrown:
right syntax to use near 'desc = '2222222222222222',
dd = 'shop',
dato = '2015-01-14' at line 3[ UPDATE events SET titel = '08:30:00',
endTime = '09:00:00',
desc = '2222222222222222',
dd = 'shop',
dato = '2015-01-14',
ticketId = '2222222222222' WHERE id = 4]' in ....
the table layout:
id int(11)
titel varchar(200)
navn varchar(200)
email varchar(255)
tlf varchar(20)
domæne varchar(150)
kundNumb int(14)
abnId varchar(15)
startTime time
endTime time
desc text
ticketId varchar(20)
dd varchar(5)
dato date
Hope someone can assist, I am sick and tired of that sql statement.

The problem is with the desc field. DESC is a keyword in the SQL syntax. So you will have to quote desc with back-ticks like this `desc`.
UPDATE events SET
`titel` = '$this->estart',
`endTime` = '$this->eend',
`desc` = '$this->desc',
`dd` = '$this->dDmed',
`dato` = '$this->dato',
`ticketId` = '$this->ticket'
WHERE id = $this->id

You missed a comma:
$sql = "UPDATE events SET titel = '$this->estart',
endTime = '$this->eend',
desc = '$this->desc',
dd = '$this->dDmed',
dato = '$this->dato',
ticketId = '$this->ticket' WHERE id = $this->id";

I believe changing
ticketId = '$this->ticket'
to
ticketId = $this->ticket
should work..

Seems Error is in ticket_ID i think you are using ticket_id as number type or integer type and passing it as string '$this->ticket' remove '' and keep it as $this->ticket

you miss a comma, and some " and '. :
"UPDATE events SET titel = '".$this->estart."',
endTime = '".$this->eend."',
desc = '".$this->desc."',
dd = '".$this->dDmed."',
dato = '".$this->dato."',
ticketId = '".$this->ticket."' WHERE id = ".$this->id.";

Related

Need to figure out total hours between check-in and check-out times

I am trying to create a check-in/check-out table in my database. My check-in form works without issue, inserting the time into my database. The problem occurs when I try to check out. Everything is good on the first entry...
But when I try to check in and check out again, this happens...
So far so good, but when I check out...
Currently, my code updates the out column and totalTime column of all matching child_id's.
Here is my code:
// Select the correct child from the database
$sql_childID = "SELECT id FROM child
WHERE firstName = '$childFirstName'
AND lastName = '$childLastName'";
$result = $pdo->query($sql_childID);
$row = $result->fetch();
$var = $row['id'];
// Insert the check out time for the child
$query = "UPDATE checkinout
SET `out` = :nowTime
WHERE child_id = $var
AND `in` IS NOT NULL";
$statement = $pdo->prepare($query);
$statement->bindValue(':nowTime', date("YmjHis"));
$statement->execute();
// Select check in time for specified child
$sql_inTime = "SELECT `in` FROM checkinout
WHERE child_id = $var";
$inResult = $pdo->query($sql_inTime);
$inRow = $inResult->fetch();
$inTime = strtotime($inRow['in']);
// Select the check out time for specified child
$sql_outTime = "SELECT `out` FROM checkinout
WHERE child_id = $var";
$outResult = $pdo->query($sql_outTime);
$outRow = $outResult->fetch();
$outTime = strtotime($outRow['out']);
// Find total hours
$totalTime = abs($outTime - $inTime)/(60*60);
// Update totalHours column for specified child
$queryTotalTime = "UPDATE checkinout
SET totalTime = :totalTime
WHERE child_id = $var
AND 'out' IS NOT NULL";
$statement = $pdo->prepare($queryTotalTime);
$statement->bindValue(':totalTime', $totalTime);
$statement->execute();
I think you could do all of this in your first update statement using TIMESTAMPDIFF rather than figuring the total time with PHP:
UPDATE checkinout
SET
out = NOW(),
totalTime = TIMESTAMPDIFF(SECOND, `in`, NOW()) / 3600
WHERE
child_id = $var
AND out IS NULL
The criteria WHERE out IS NULL will only update rows that do not have a value in the out column yet.
IF you have MySQL Db THEN sql will be
SELECT TIMESTAMPDIFF(HOUR,in,out) from checkinout;

Unable to update my database

So I was doing my edit page for my html form, however it is not updating the database and it always show me error message update fail. I was wondering if there is any error in my codes or query.
This is my query
$queryupdate = "Update anno
SET where id = '$no'
and title = '$row' and serial_no = '$no'
and type = '$type' and anno = '$row1' ";
Your query syntax is wrong:
UPDATE tableName SET columnToModify = newValue WHERE coniditons;
in your case you did not specify the column to be modified and jumped ahead to the condition.
Are you doing like that ?
$queryupdate = "Update anno
SET title = '$row' and serial_no = '$no'
and type = '$type' and anno = '$row1' where id = '$no'
";

MySQL update column only if value not empty where

I have an UPDATE query and using Ajax, I wanted to know if any value is empty can I only update the values that not empty in the database. I don't know if this is possible to have a if statement or something to check to skip the empty values. I know I can just add another form element but just wanted to know if there was another solution.
Only if the data is POST from front end form. If data not POST don't update this Title = '.$title .',
$id = $_POST['id'];
$title = "";
$description = $_POST['Description'];
$date = $_POST['Date'];
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = '.$title .',
`Description` = '.$description.',
`Date` = '.$date =.'
WHERE `id` = '.$id;
$result = mysql_query($query) or die("<b>A fatal MySQL error occured</b>.<br />Query: ".$query."<br />Error: (".mysql_errno().") ".mysql_error());
Update: This is what worked for me. Thanks Karim Daraf
$query = " UPDATE user SET
Title = Coalesce($title,Title ) etc...
Try it with Coalesce .
$query = " UPDATE user
SET
`Title` = CASE WHEN `Title`='' or `Title` IS NULL THEN '$title' END,
`Description` = CASE WHEN `Description`='' Or `Description` IS NULL THEN '$description' END,
`Date` = CASE WHEN `Date`='' Or Date` IS NULL THEN '$date' END
WHERE `id` = '".$id."' ";
or :
$query = " UPDATE user
SET
`id` = Coalesce('$id''".$id."' , NULLIF(`id`,'')),
`Title` = Coalesce('$title''".$title."',NULLIF(`Title`,'') ) ,
`Description` = Coalesce('$description''".$description."' , NULLIF(`Description`,'') ) ,
`Date` = Coalesce('$date''".$date."',NULLIF(`Date`,''))
WHERE `id` = '$id''".$id."' ";
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = COALESCE(NULLIF("'.$title.'", ""),`Title`),
`Description` = "'.$description.'",
`Date` = "'.$date.'"
WHERE `id` = "'.$id.'"';
Not sure to understand: you have data and want to update, but only if some fied in the DB are empty?
In the case perfom only a where:
$query = 'UPDATE user SET
`id` = '.$id.',
`Title` = '.$title .',
`Description` = '.$description.',
`Date` = '.$date =.'
WHERE `id` = '.$id.' AND Title = '';
for example

Error updating mysql

I am sending data from an android app to save in my mysql database.
My Codes are
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = $time, transaction_status = 2 , remark = $rem WHERE transaction_id = $t_id LIMIT 1");
And my dabase is as,
remark varchar(500) latin1_general_ci.
The problem is whenever I have $rem as a numbric value then database is updated but if I use any text then it wont.
Please help me.
Make sure wrapping string data with single quote.
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = '$time', transaction_status = 2 , remark = '$rem' WHERE transaction_id = $t_id LIMIT 1")
Text should be wrapped by single quotes: ''
$ress = mysql_query("UPDATE btrack_transaction SET delivery_date = $time, transaction_status = 2 , remark = '$rem' WHERE transaction_id = $t_id LIMIT 1");

MYSQL Query not working as it should

$sql = "UPDATE `shows` SET `title` = '$title', `tagline` = '$tagline', `desc` = '$desc' , `img_src = '$imgsrc' WHERE id = $showid";
The query above does not want to work, I simply get a mysql_error saying error at '' on line 1;
Any idea where I am going wrong?
You're missing a tick:
`img_src = '$imgsrc' WHERE id = $showid";
should be:
`img_src` = '$imgsrc' WHERE id = $showid";

Categories