Reputation: 1713
I am doing some simple PHP and SQL for the first time in quite a while, but for some reason am not getting it to work. When I provide fixed values it all works fine, but as soon as I replace them with variables my code fails. I have checked and it seems that the variables have proper values in them:
This works:
<?php
$con=mysqli_connect("localhost","xxx","xxx","xxx");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_GET['id'])){
$ID = $_GET['id'];
$newStock = $_GET['stockcount'] - 1;
mysqli_query($con,'UPDATE products SET stockcount = "3" WHERE id = "1"');
}
else
echo "Invalid item";
mysqli_close($con);
header('Location: browse.php');
?>
But not this:
<?php
$con=mysqli_connect("localhost","xxx","xxx","xxx");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(isset($_GET['id'])){
$ID = $_GET['id'];
$newStock = $_GET['stockcount'] - 1;
mysqli_query($con,'UPDATE products SET stockcount = "'+$newStock+'" WHERE id = "'+$ID+'"');
}
else
echo "Invalid item";
mysqli_close($con);
header('Location: browse.php');
?>
Do I need to do something with the variables, or am I doing something else wrong? As always, any advice would be greatly appreciated :)
Upvotes: 0
Views: 36
Reputation: 2169
mysqli_query($con,'UPDATE products SET stockcount = "'.$newStock.'" WHERE id = "'.$ID.'"');
This isn't javascript. Connotate with .
not +
. Better yet, just pass it in string by using double-quotes!
mysqli_query($con,"UPDATE products SET stockcount = '$newStock' WHERE id = '$ID'");
Upvotes: 0
Reputation: 44844
" + " is not used for concatenate string in PHP as you did
mysqli_query($con,'UPDATE products SET stockcount = "'+$newStock+'" WHERE id = "'+$ID+'"');
it should be as
mysqli_query($con,'UPDATE products SET stockcount = "'.$newStock.'" WHERE id = "'.$ID.'"');
Upvotes: 1