By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,620 Members | 1,303 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,620 IT Pros & Developers. It's quick & easy.

Display and Update record in php

P: 8
I have problem in retrieving record from mysql using php form, I was able to retrieve the record, if I edit, the changes is not updated in mysql.
find below my code
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <body>
  3. <?php
  4. $db = mysql_connect("localhost", "root");
  5. mysql_select_db("k1",$db);
  6. $id = $_POST["id"]; 
  7. echo $id;
  8. if ($submit) {
  9. $first  = $_POST['first'];
  10. $last = $_POST['last'];
  11. $address = $_POST['address'];
  12. $position = $_POST['position'];
  13. $sql = "UPDATE 'employees' SET 'first'='$first','last'='$last','address'='$address','position'='$position' WHERE 'id'='$id'";
  14. $result = mysql_query($sql);
  15. echo $first;
  16. echo "Thank you! Information updated.\n";
  17. } else {
  18. if ($id) {
  19. $sql = "SELECT * FROM employees WHERE id=$id";
  20. $result = mysql_query($sql);
  21. $myrow = mysql_fetch_array($result);
  22. $first  = $myrow['1'];
  23. $last = $myrow['2'];
  24. $address = $myrow['3'];
  25. $position = $myrow['4'];
  26. $id = $myrow['id'];
  27. $submit = 'submit';
  28. }
  29. }
  30. ?>
  31. <form method="post" action="<?php echo $PHP_SELF?>">
  32. <input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
  33. First name:<input type="Text" name="first" value="<?php echo $first ?>"><br>
  34. Last name:<input type="Text" name="last" value="<?php echo $last ?>"><br>
  35. Address:<input type="Text" name="address" value="<?php echo $address ?>"><br>
  36. Position:<input type="Text" name="position" value="<?php echo $position ?>"><br>
  37. <input type="Submit" name="submit" value="Enter information">
  38. </form>
  39. </body>
  40. </html>
[Please use CODE tags when posting source code. Thanks! --pbmods]
Jun 22 '07 #1
Share this Question
Share on Google+
4 Replies


Purple
Expert 100+
P: 404
Hi Bakarre and welcome to TSDN !!

Can you take a look at

this excellent post by Motoma where he works through options to get debug output from PHP. I suspect from looking at your code you have not got error messages turned on.

Purple
Jun 22 '07 #2

Purple
Expert 100+
P: 404
And try this

[PHP]<html>
<body>
<?php
$db = mysql_connect("localhost", "root");
mysql_select_db("k1",$db);
if(isset($_POST["id"])) $id = $_POST["id"];
//echo $id;
if (isset($_POST['submit']))
{
$first = $_POST['first'];
$last = $_POST['last'];
$address = $_POST['address'];
$position = $_POST['position'];
$sql = "UPDATE 'employees' SET 'first'='$first','last'='$last','address'='$addres s','position'='$position' WHERE 'id'='$id'";
$result = mysql_query($sql);
echo $first;
echo "Thank you! Information updated.\n";
}
elseif (isset($id))
{
$sql = "SELECT * FROM employees WHERE id=$id";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
$first = $myrow['1'];
$last = $myrow['2'];
$address = $myrow['3'];
$position = $myrow['4'];
$id = $myrow['id'];
$submit = 'submit';
}
?>
<form method="post" action="<?php echo $PHP_SELF?>">
<input type=hidden name="id" value="<?php echo $myrow["id"] ?>">
First name:<input type="Text" name="first" value="<?php echo $first ?>"><br>
Last name:<input type="Text" name="last" value="<?php echo $last ?>"><br>
Address:<input type="Text" name="address" value="<?php echo $address ?>"><br>
Position:<input type="Text" name="position" value="<?php echo $position ?>"><br>
<input type="Submit" name="submit" value="Enter information">
</form>
</body>
</html>[/PHP]

Let me know how you get on

Purple
Jun 22 '07 #3

P: 8
Hi,
is perfectly now working.
You can buy experience.
I gained alot.

Bakare
Jun 26 '07 #4

P: 8
Hi,
is perfectly now working.
You cann't buy experience.
I gained alot.

Bakarre
Jun 26 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.