I tried many times to update a record. when I click update, it pop-up new tab. Then I try change name. When click submit. it show updating is successful but not update it in database. I dont know what wrong.. please help me.

list_record.php :

<?php

$host="localhost"; 
$username="MY_USERNAME"; 
$password="MY_PASSWORD"; 
$db_name="test_create_DB"; 
$tbl_name="web_members";

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");


$sql="SELECT * FROM web_members";
$result=mysql_query($sql);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>
<table width="400" border="1" cellspacing="0" cellpadding="3">
<tr>
<td colspan="4"><strong>List data from mysql </strong> </td>
</tr>

<tr>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
<td align="center"><strong>Update</strong></td>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>
<tr>
<td><? echo $rows['name']; ?></td>
<td><? echo $rows['lastname']; ?></td>
<td><? echo $rows['email']; ?></td>
<td align="center"><a href="update.php?id=<? echo $rows['id']; ?>">update</a></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
</table>
<?php
mysql_close();
?>

update.php :

<?php

$host="localhost"; 
$username="MY_USERNAME"; 
$password="MY_PASSWORD"; 
$db_name="test_create_DB"; 
$tbl_name="web_members";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$id=$_POST['id'];
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];
 
$sql="SELECT * FROM web_members WHERE id='$id'";
$result=mysql_query($sql);

$rows=mysql_fetch_array($result);
?>
<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center"><input name="name" type="text" id="name" value="<? echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname" type="text" id="lastname" value="<? echo $rows['lastname']; ?>" size="15"></td>
<td><input name="email" type="text" id="email" value="<? echo $rows['email']; ?>" size="15"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><input name="id" type="hidden" id="id" value="<? echo $rows['id']; ?>"></td>
<td align="center"><input type="submit" name="Submit" value="Submit"></td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?

// close connection 
mysql_close();

?>

update_ac.php :

<?php

$host="localhost"; 
$username="MY_USERNAME"; 
$password="MY_PASSWORD";  
$db_name="test_create_DB"; 
$tbl_name="web_members";

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$id=$_POST['id'];
$name=$_POST['name'];
$lastname=$_POST['lastname'];
$email=$_POST['email'];


$sql="UPDATE web_members SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
$result=mysql_query($sql);

// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_record.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

listrecord.php does not contain a form, so what you send to update.php is just the id through the link, so in update.php change $id=$_POST['id']; to $id=$_GET['id']; and remove the others $_POST, they are not defined.

Hi, Please help. my data is never updated and I quried the results and got this: SET name='', lastname='', email='' WHERE id=''Successful

"UPDATE web_members SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";

$PDO_con = new PDO; // Create DB connection
$sql = "UPDATE web_members SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
$q = $PDO_con->prepare($sql);
$q->execute; // Execute cmd
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.