Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

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

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote