0

I tried to update a MySQL database table (online) with a php function, but everytime that i click on "button update" it answers me:

Could not update data: Unknown column '$username' in 'where clause'

can somebody help me with this error or only suggest me the correct way to resolve it?

here is it the code:

<html>
<head>
<title>Update Name of my_table in MySQL Database</title>
</head>
<body>

<?php
if(isset($_POST['update']))
{
$dbhost = 'localhost';
$dbuser = 'dbuser';
$dbpass = 'dbpassword';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$username = $_POST['username'];
$name = $_POST['name'];

$sql = 'UPDATE tbl_user SET name = $name WHERE username = $username';

mysql_select_db('my_table');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
mysql_close($conn);
}
else
{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Usrename</td>
<td><input name="username" type="text" id="username"></td>
</tr>
<tr>
<td width="100">Name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Update">
</td>
</tr>
</table>
</form>
<?php
}
?>
</body>
</html>

and here is it my_table columns:

id | username | password | email | name
3
  • desc your DB table 'tbl_usr", is it having a username column ?? Commented Oct 14, 2014 at 10:19
  • id username password email name Commented Oct 14, 2014 at 10:20
  • If you're using the deprecated mysql_* API, please read up on at least mysql_real_escape_string(). Commented Oct 14, 2014 at 10:24

2 Answers 2

3

Chane this line of query You missing single quete around your variable.

$sql = 'UPDATE tbl_user SET name = $name WHERE username = $username';

to this

$sql = "UPDATE tbl_user SET name = '$name' WHERE username = '$username'";
Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot.... i can t vote you for my reputation, but i want to say you thanks!!!!
of course, i need to wait other 5 minutes according rules to accept the answer!
@MarcC Just FYI (and for future readers that may copy'n'paste), using this answer's code and posting to the page with the username 'OR''=' will update the name of all users in the database.
1
$sql="UPDATE tbl_user SET name = '".$name."' WHERE username = '".$username."'"

There is matter of quotes i think so this will work better because name and username fields contain string. concating string is better solution when you work with string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.