PHP/MYSQL UPDATE not writing to the database

68 views Asked by At

I am trying to update a database with a constant, Guest. I have an array containing dates, and have set a for loop to cycle through them, the query should amend one row whos date matches the array key AND the starttime of 19:00:00. However it is not amending the database. Where am i going wrong?

$size1 = sizeof($dayOfTheWeek) - 1;


for ($count = 0; $count <= $size1; $count++) {


$query = "UPDATE rota SET title=Guest WHERE date = '$dateMonthYearArr[$count]' AND starttime = '19:00:00'" or die;               
$dayresult = mysql_query($query); 

echo "".$dateMonthYearArr[$count]."</br>";
}

echo "Complete";
4

There are 4 answers

0
Beau On BEST ANSWER
$query = "UPDATE rota SET title='Guest' WHERE date = '" . $dateMonthYearArr[$count] . "' AND starttime = '19:00:00'";

You should also make sure $dateMonthYearArr[$count] is SQL safe =).

1
jhnferraris On

Fix query:

$query = "UPDATE rota SET title='Guest' 
          WHERE date = '" . $dateMonthYearArr[$count] . "' AND starttime = '19:00:00'";               
$dayresult = mysql_query($query);

$dateMonthYearArr is a php array so you should concatenate it to your string in a proper way.

0
smiggle On

This actually

$query = "UPDATE rota SET title=Guest WHERE date = '$dateMonthYearArr[$count]' AND starttime = '19:00:00'" or die;   

is a comparison because 'or' is an operator and it behaves like an IF-statement. So your are not setting the string properly.

Try it with:

$query = "UPDATE rota SET title = 'Guest' WHERE date = '" . $dateMonthYearArr[$count] . "' AND starttime = '19:00:00'";
mysql_query($query) or die('Cannot update title');
0
Quixrick On

I deleted my other answer and combined the points of the other two posters. Please do not award me points for this. I just combined their [better] answers.

<?php

$query = "UPDATE rota SET title = 'Guest' WHERE date = '".$dateMonthYearArr[$count]."' AND starttime = '19:00:00'";
mysql_query($query) or die('Cannot update title');