I'd like to update a field in my database using PHP. I wrote this code and works fine. The value of var2
is 0, after the update its value is 1.
I'd like to use the field var2
as a PHP variable, thus I can use it as an if condition.
With the if conditional I want to update var2
if its value is 0
, and redirect to other php file if var2
's value is 1
.
<?php
session_start();
$con=mysql_connect("localhost","root","");
mysql_select_db("database",$con);
$sql = "SET SQL_MODE = ''";
mysql_query($sql,$con);
$sql = "UPDATE table SET var2=1 WHERE var1 = '".$_SESSION['user']."'";
if (){}
mysql_query($sql,$con);
if(mysql_error() != ""){
echo $sql." ".mysql_error();
}
mysql_close($con);
?>
I have tried with
$abc = $_POST['var2'];
$sql = "UPDATE table SET "$abc"=1 WHERE var1 = '".$_SESSION['user']."'";
but it doesn't work for me, I got the next message "Parse error: syntax error, unexpected T_VARIABLE in C:\xampp\htdocs\egresadoslunes\finalizar.php on line 10"