Please help me with this PHP script Im trying to update an S

Please help me with this PHP script. I\'m trying to update an SQL database.

So when I replace \'$question\' with sex it works fine. But I need to pass it as a variable because I will change question to a $_POST.

Below is my script, please only answer if you are confident

Broken Script

require \"conn.php\";
$username = $_POST[\"username\"];
$answer = $_POST[\"answer\"];
$question =\"sex\";
$sql = \"UPDATE user_info SET \'$question\'= \'$answer\' WHERE username=\'$username\';\"; <<<<--- Error
$do = mysqli_query($conn, $sql);
?>

Working Script (Cannot use because $question will become a $_POST)

require \"conn.php\";
$username = $_POST[\"username\"];
$answer = $_POST[\"answer\"];
$question =\"sex\";
$sql = \"UPDATE user_info SET sex= \'$answer\' WHERE username=\'$username\';\"; <sex into \'$question\'
$do = mysqli_query($conn, $sql);
?>

Solution

Try removing \' from question like-

$sql = \"UPDATE user_info SET $question= \'$answer\' WHERE username=\'$username\';\";

In your sql the column name was coming in single quotes

Please help me with this PHP script. I\'m trying to update an SQL database. So when I replace \'$question\' with sex it works fine. But I need to pass it as a v

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site