Updating MYSQL table field using PHP

imtuned

New Member
Messages
4
Reaction score
0
Points
0
I am having some problems when trying to update a MYSQL database table using PHP. Here go the code I am using:

PHP:
/*** update Field*/ 
$conn = mysql_connect("localhost", "user", "pass"); 
mysql_select_db("database",$conn);
 $sql = "UPDATE users SET pic = ".$newname." WHERE username = ".$session->username."";
if (mysql_query($sql, $conn)) {      
echo "record added!"; }
 else {    
 echo "something went wrong";
 }
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
1. You should be learning PDO instead of the mysql interface.

2. "some problems"????? Like what?

3. echo $newname

4. echo $session->username
 

learning_brain

New Member
Messages
206
Reaction score
1
Points
0
I have found sometimes that the concatenation in the SQL string throws errors...

try

"UPDATE users SET pic = $newname WHERE username = $session->username";
 

ellescuba27

Member
Messages
273
Reaction score
3
Points
18
Try printing the return of mysql_error() and tell us what it said. Or if there was a PHP error, tell us what that said.
EDIT: There should also be single quotes in the $sql variable, like:
$sql = "UPDATE users SET pic='".$newname."' WHERE username='".$session->username."'";
I also don't know why you don't just type the value of $sql into mysql_query directly?
 
Last edited:
Top