-2

I want to update data from form to database using PHP. But there too many issues come on my code just like "Could not update data: Unknown column 'Nabeel' in 'field list'".

Please check the code and give me solution.

Thanks, Nabeel.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script type="text/javascript">
function doSomething() {
    alert('Are you sure you want to delete this file ??');
}
</script>
</head>

<body>



<h2>Update Data Using PHP</h2>
</div>

<?php
$con = mysql_connect("localhost", "root", "");
$db = mysql_select_db("firstphp", $con);
if (isset($_GET['submit'])) {
$id = $_GET['id'];
$username = $_GET['username'];
$passward = $_GET['passward'];
$name = $_GET['name'];

$query = mysql_query("update users set
username='$username', passward='$passward', name='$name' where id='$id'", $con);
}
$query = mysql_query("select * from users", $con);
while ($row = mysql_fetch_array($query)) 
{
    echo "<b><a href='update.php?update={$row['id']}'>{$row['id']}</a></b>";
    echo "<br />";
}
?>
</div><?php
if (isset($_GET['submit'])) {
$submit = $_GET['submit'];
$query1 = mysql_query("select * from users where id=$submit", $con);
while ($row1 = mysql_fetch_array($query1)) {
echo "<form method='get'>";
echo "<hr/>";
echo"<input class='input' type='hidden' name='id' value='{$row1["id"]}' />";
echo "<br />";
echo "<label>" . "Name:" . "</label>" . "<br />";
echo"<input class='input' type='text' name='username' value='{$row1["username"]}' />";
echo "<br />";
echo "<label>" . "Email:" . "</label>" . "<br />";
echo"<input class='input' type='text' name='passward' value='{$row1["passward"]}' />";
echo "<br />";
echo "<label>" . "Name:" . "</label>" . "<br />";
echo"<input class='input' type='text' name='name' value='{$row1["name"]}' />";
echo "<br />";


echo "<input class='submit' type='submit' name='submit' value='submit' />";
echo "</form>";
}
}
if (isset($_GET['submit'])) {
echo '<div class="form" id="form3"><br><br><br><br><br><br>
<Span>Data Updated Successfuly......!!</span></div>';
}

mysql_close($con);
?>
</bod

></body>
</html>
  • 1
    `where id=$submit` ? Are you sure that's what you want to use? That's your submit button `` – Funk Forty Niner Aug 27 '14 at 10:07
  • **Danger**: You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Aug 27 '14 at 10:09
  • You also have three `if (isset($_GET['submit']))` – Funk Forty Niner Aug 27 '14 at 10:14

3 Answers3

0
$query1 = mysql_query("select * from users where id=$submit", $con);

Change to

$query1 = mysql_query("select * from users where id='".$submit."'", $con);
Andreas L.
  • 2,805
  • 23
  • 23
0

Try this:

$query1 = mysql_query("select * from users where id={$submit}", $con);

The curly brackets will make sure that your PHP variable is being interpreted and not taken as a simple string.

CodeShark
  • 1,709
  • 2
  • 14
  • 22
0

1) You cannot use where id=$submit as Id needs to be an integer. $submit always has the value submit (because you set it to that here:

echo "<input class='submit' type='submit' name='submit' value='submit' />";

You want to use the ID value from this line:

echo"<input class='input' type='hidden' name='id' value='{$row1["id"]}' />";

2) Replace

$query1 = mysql_query("select * from users where id=$submit", $con);

with

$query1 = mysql_query("select * from users where id=$id", $con);

and make sure id is an integer.

3) do not use mysql, use mysqli instead: The mysql extension is deprecated and will be removed in the future: use mysqli or PDO instead

4) You misspelled the word password. Change from 'passward' to password

5) Your HTML is broken towards the end:

</bod

></body>
</html>

Delete </bod >

6) No need to use HTML4 doctype declaration. Replace

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

with

<!DOCTYPE html>
Community
  • 1
  • 1
bg17aw
  • 2,818
  • 1
  • 21
  • 27