Please help me with this PHP script. I\'m trying to update an SQL database. So w
ID: 3931892 • Letter: P
Question
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);
?>
Explanation / Answer
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
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.