0

I want to all users to be able to update their about page. Here is the string I am using in my php to query the MySQL update. I am getting an error. What am I doing wrong?

$insert_query= "UPDATE user_info SET bio= $bio_ans, residence= $residence_ans, work= $work_ans WHERE user_id= $user_id"; mysqli_query($connect, $insert_query) or die('error with query1'); 
0

4 Answers 4

2

If the update value is not an integer then you need to use quotes around the value.

 $insert_query= "UPDATE user_info SET bio= '$bio_ans', residence= '$residence_ans', work = '$work_ans' WHERE user_id= '$user_id'"; mysqli_query($connect, $insert_query) or die('error with query1'); 
Sign up to request clarification or add additional context in comments.

2 Comments

that will just insert the string $bio_ans etc I think whereas I think the user is ooking to put ht evalue of the variable by concatenation
@PaulSullivan: no, the single quotes are ignored by PHP since they're within the double quotes. the whole string is a double-quote one, so variable interpolation will occur. quotes don't nest.
0
$insert_query= "UPDATE user_info SET bio= $bio_ans, residence= $residence_ans, work=$work_ans WHERE user_id= $user_id"; mysqli_query($connect, $insert_query) or die('error: $mysqli->error'); 

The $mysqli->error will let you know specifically what went wrong.

Comments

0

$insert_query= "UPDATE user_info SET bio='" . $bio_ans. "', residence='" . $residence_ans."', work=
'".$work_ans."' WHERE user_id= '$user_id'"; mysqli_query($connect, $insert_query) or die('error with query1');

1 Comment

Note that string concatenation of this type is horrendously insecure and you should be calling parameterised procedures
0

Besides the lack of quotes (as shown by RPM), the really wrong thing you're doing is to use variable interpolation to create an SQL query.

This is only acceptable for numeric variables, and then only if you have forcibly cast it to a numeric type just before use. String escaping is supposedly safe, but very error-prone (doing it twice, forgetting to add when you add a new value, etc). The slightest slip will open a huge hole for SQL injection, which is the absolute easiest way to crack a web app.

Use prepared statements with parameter bindings and you'll be safe.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.