UPDATE syntax

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
neesley
Forum Commoner
Posts: 26
Joined: Tue Aug 31, 2010 3:22 am

UPDATE syntax

Post by neesley »

Code: Select all

UPDATE schedule SET schedule.jakes = schedule_default.jakes WHERE schedule.date = '$newDatesArray[$i]' AND schedule_default.ID = '5'
Would you please help me with this syntax? I'm not getting any errors, but it's not updating the table...
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: UPDATE syntax

Post by Christopher »

The query looks correct, but you don't show what quotes you are using. I would recommend"

Code: Select all

$sql = "UPDATE schedule SET schedule.jakes = schedule_default.jakes WHERE schedule.date = '{$newDatesArray[$i]}' AND schedule_default.ID = '5'";
Also, are you escaping that value?
(#10850)
neesley
Forum Commoner
Posts: 26
Joined: Tue Aug 31, 2010 3:22 am

Re: UPDATE syntax

Post by neesley »

Thanks for the reply Christopher. Here's the full code for this part. I know everything works except the UPDATE command... I've been staring at this for three hours. Yikes...

Code: Select all

$queryLastDateArray = "SELECT date FROM schedule ORDER BY ID DESC LIMIT 1";
$lastDateArray = mysql_query($queryLastDateArray);
while($row = mysql_fetch_array($lastDateArray))
			{
			$lastDate = $row['date'];
			}			
$lastDatePlusOne = date("Y-m-d", strtotime("+1 day", strtotime($lastDate)));

$newDatesArray = GetDays($lastDatePlusOne, $_POST[date]);
$i = 0;
while($i < count($newDatesArray))
	{
	if ((date('D', strtotime($newDatesArray[$i]))) == 'Fri')
		{
		$insDate = "INSERT INTO schedule (date) VALUES ('$newDatesArray[$i]')";
		$result = mysql_query($insDate);
		$insEmp = "UPDATE schedule SET schedule.jakes = schedule_default.jakes FROM schedule, schedule_default WHERE schedule.date = '$newDatesArray[$i]' AND schedule_default.ID = '5'";
		$result2 = mysql_query($insEmp);
		}
	$i++;
	}
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: UPDATE syntax

Post by Christopher »

Don't just stare! ;) I would recommend two things: 1) find out what SQL in $insEmp is, check for errors and try it directly in MySQL. And 2) check for a MySQL error and echo the MySQL error message in the code.

It is good practice to have an error check after each query.

Code: Select all

$queryLastDateArray = "SELECT date FROM schedule ORDER BY ID DESC LIMIT 1";
$lastDateArray = mysql_query($queryLastDateArray);
if (!mysql_errno($lastDateArray)) {
     while($row = mysql_fetch_array($lastDateArray))
...
} else {
     $error_message = mysql_error($lastDateArray);
}
(#10850)
Post Reply