1

I have the following columns in the mysql:

firstname, lastname,age,email,phone

When I update say I have the following:

John Doe 23 eee@yahoo.com 5555555555

but when I want to edit any of the fields and I submit it comes out to:

Mike 0 

and thats it. Any Ideas?

Heres my edit.php

<html>
<body>
<table>
<?php
include 'db.inc.php';
// Connect to MySQL DBMS
if (!($connect = @ mysql_connect($hostName, $username,
  $password))){
  showerror();
 }
// Use the food database
if (!mysql_select_db($databaseName, $connect)){
  showerror();

}
$uid=$_GET[id];
$query=mysql_query("SELECT * FROM contact WHERE id='$uid'");
$row=mysql_fetch_array($query); 

?>
<form method ="post" action="update.php">
<table>
<tr>
<td><input type="hidden" name="id" value="<?php echo "$row[id]"?>"></td>
</tr>

<tr>
<td>First Name:</td>
<td><input type="text" name="ud_firstname" value="<?php echo          "$row[ud_firstname]"?>"></td>
</tr>

<tr>
<td>Last Name:</td>
<td><input type="text" name="ud_lastname" value="<?php echo"$row[ud_lastname]"?>"></td>
</tr>

<tr>
<td>Age:</td>
<td><input type="text" name="age" value="<?php echo"$row[ud_age]"?>"></td>
</tr>

<tr>
<td>Email:</td>
<td><input type="text" name="email" value="<?php echo "$row[ud_email]" ?>">    </td>
</tr>

<tr>
<td>Phone Number:</td>
<td><input type="text" name="phone" value="<?php echo "$row[ud_phone]" ?>">  </td>
</tr>
<input type = "submit">
</table>
</form>
</body>
</html>

and then this is my update.php

 include 'db.inc.php';
 // Connect to MySQL DBMS
if (!($connect = @ mysql_connect($hostName, $username,
  $password))){
  showerror();
  }
 
if (!mysql_select_db($databaseName, $connect)){
  showerror();

 }
 $uid =$_POST["id"];


 $ud_firstname = mysql_real_escape_string($_POST["ud_firstname"]);
 $ud_lastnamename = mysql_real_escape_string($_POST["ud_lastname"]);
 $ud_age = mysql_real_escape_string($_POST["ud_age"]);
 $ud_email = mysql_real_escape_string($_POST["ud_email"]);
 $ud_phone = mysql_real_escape_string($_POST["ud_phone"]);


 mysql_query("UPDATE contact
    SET firstname = '$ud_firstname', lastname = '$ud_lastname', age =    '$ud_age', email='$ud_email', phone='$ud_phone'
     WHERE id=$uid");
if(mysql_affected_rows()>=1){
echo "<p>($id) Record Updated<p>";
header('location:view.php');
}else{
    echo "<p>($id) Not Updated<p>";
}
?>

Any ideas?

vvvvv
  • 25,404
  • 19
  • 49
  • 81
mdavies23
  • 67
  • 8
  • 1
    It's not really clear to me the problem you're describing. Is the wrong record being updated? Is the right record being updated to the wrong values? Something else? – David Dec 05 '16 at 01:41
  • Does your form posts the right values? Place `print_r($_POST); exit();` somewhere in your `update.php`. – lephleg Dec 05 '16 at 01:43
  • its the right record and the right value but its only updates the first value which is the firstname and then sets all the others to null. – mdavies23 Dec 05 '16 at 01:44
  • `name="age"` != `$_POST["ud_age"]`. The index of the `POST` needs to match the element's name. Same with `email` and `phone`. You should be getting undefined index notices if error reporting is on. – chris85 Dec 05 '16 at 01:48
  • LePhleg - I did that a now it shows the first and the last name but none of the other fields. – mdavies23 Dec 05 '16 at 01:48
  • @mdavies23 this means that the problem is in your form. Check for typos on the fields that are not included in POST. – lephleg Dec 05 '16 at 01:51

1 Answers1

2

Replace the following in your update.php:

 $ud_age = mysql_real_escape_string($_POST["age"]);
 $ud_email = mysql_real_escape_string($_POST["email"]);
 $ud_phone = mysql_real_escape_string($_POST["phone"]);

OR

the following in your form:

<tr>
<td>Age:</td>
<td><input type="text" name="ud_age" value="<?php echo"$row[ud_age]"?>"></td>
</tr>

<tr>
<td>Email:</td>
<td><input type="text" name="ud_email" value="<?php echo "$row[ud_email]" ?>">    </td>
</tr>

<tr>
<td>Phone Number:</td>
<td><input type="text" name="ud_phone" value="<?php echo "$row[ud_phone]" ?>">  </td>
</tr>

$_POST index is defined by name attributes on your form input fields. These two have to match.

lephleg
  • 1,724
  • 2
  • 21
  • 41