-2

In my php code, I am trying to update all rows of an sql database table that have a particular value for 2 different fields/columns. When I run the code, the updates are not being made to the sql table.

Assume I have a database called "databasename" with a table called "Pets" with these columns: "Cat" (varchar), "Dog" (varchar) & "Favorite" (boolean). I want to mark all rows that have Cat = Sylvester & Dog = Clifford with a value of 1 in the favorite column.

Here is my code:

<?php 
$connect = mysqli_connect("localhost","root","","databasename");
$dog='Clifford';
$cat='Sylvester';
$query="SET sql_safe_updates=0";
$query.="UPDATE Pets SET Favorite = 1 WHERE Dog= $dog AND Cat = $cat";
  mysqli_multi_query($connect,$query);
?>
user3882316
  • 25
  • 1
  • 4

1 Answers1

-1

If you print out the query string, you'll see that it is:

$query="SET sql_safe_updates=0UPDATE Pets SET Favorite = 1 WHERE Dog= $dog AND Cat = $cat";

I'm not surprised this doesn't work. Try just starting the query with update; there is no reason for the sql safe updates mode.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • Thanks for the help! I have also tried this but it does not work: $query="UPDATE Pets SET Favorite = 1 WHERE Dog= $dog AND Cat = $cat"; mysqli_query($connect,$query); – user3882316 Apr 09 '16 at 22:16
  • @user3882316 . . . You are using "mysqli" so you should be using parameters in your queries, rather than subjecting your queries to SQL injection (and preventing the engine from effectively caching the execution plans). – Gordon Linoff Apr 09 '16 at 22:18
  • thanks for this! do you have any examples or links so i can learn more about how to do this? – user3882316 Apr 09 '16 at 23:50
  • 1
    @user3882316 Visit http://php.net/manual/en/mysqli.error.php and apply that to your query. You'll see where you've gone wrong. Think *"string literals"*. – Funk Forty Niner Apr 09 '16 at 23:51