In 2dys code, m trying best for updating values on the basis of id coumn value but its not updating... thn i tried to target data using select command.. nd it shws d result thr4 now m confused wth update command... plss chkout ths code as well...

<?php
$host="localhost";
$username="root"; 
$password=""; 
$db_name="new";
$tbl_name="web_members"; 

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

$id=$_GET['id'];

echo $id, $name, "<hr>";

echo $rows['id'], "<hr>";

$b="Select * from $tbl_name where id='$id'";
$result=mysql_query($b);
while($rows=mysql_fetch_array($result))
{
echo $rows['id'], "<hr>";
echo $rows['name'], "<hr>";
echo $rows['lastname'], "<hr>";
echo $rows['email'], "<hr>";
}



// Having problem in update data in mysql database
$sql="UPDATE $tbl_name set name='$name', lastname='$lastname', email='$email' where id='$id'";

$result=mysql_query($sql);


if($result){

echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}

else {
echo "ERROR";
}


?>

Recommended Answers

All 7 Replies

Your variables $name $lastname $email that you are using in

$sql="UPDATE $tbl_name set name='$name', lastname='$lastname', email='$email' where id='$id'";

have not been set anywhere so you'll be updating your DB with NULLs.

Other than that, this ran for me the first time.

Member Avatar for rajarajan2017

Like you id, where the name, lastname and email are received. use like below if you are receiving from the url.

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

Then use your sql query to update the values.

i tried as you said but still not working, i think its not getting the data related to last name, name and email from previous page. i tried few changes in previous page code.. but its not working

<form name="form1" method="post" action="update_ac.php?id= <?php echo $rows['id'], name= <?php echo $rows['name'], lastname= <?php echo $rows['lastname'], email= <?php echo $rows['email']; ?>">

without changes, it seems like this...

<form name="form1" method="post" action="update_ac.php?id= <?php echo $rows['id']; ?>">

i think the matter is all about the update command as sing id data, it is showing other column data using select case.

Let me know, what to do now =)

Member Avatar for rajarajan2017
<form name="form1" method="get" action="update_ac.php">
$id= //your id
$name= //user name
$lastname=//lastname
 <input type="submit" name="commit" value="submit"/>
</form>

Check with the url on updata_ac.php whether it shows the query data or not? It will sure work

commented: Good, right on target!!! +0

Thanks dear for replying me time to time. I tried whatever you said but didn't succeed, so finally it thought it would be better that i should give full details with the idea. This could be better decision.

There are 3 pages
1. list_records - here, we are showing the values from database.
2. update - here, we give the values to update.
3. update_ac - here, we finally get values updated.

list_records code--

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="new"; // Database name
$tbl_name="web_members"; // Table name

// 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 $tbl_name";
$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>Listing... 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>Click to Update</strong></td>
</tr>

<?php

while($rows=mysql_fetch_array($result)){

// link to update.php and send value of id
?>


<tr>
<td><?php echo $rows['name']; ?></td>
<td><?php echo $rows['lastname']; ?></td>
<td><?php echo $rows['email']; ?></td>

<td align="center"><a href="update.php?id= <?php echo $rows['id']; ?>">Update</a></td>

</tr>




<?php
}
?>

</table>
</td>
</tr>
</table>

<?php
mysql_close();
?>

update code--

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="new"; // Database name
$tbl_name="web_members"; // Table name

// 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");


// get value of id that sent from address bar
$id=$_GET['id'];

echo "Fetched ID is"; echo $id;

// Retrieve data from database
$sql="SELECT * FROM $tbl_name 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?id= <?php echo $rows['id']; ?>">
<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="<?php echo $rows['name']; ?>"></td>
<td align="center"><input name="lastname" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>" size="15"></td>
<td><input name="email" type="text" id="email" value="<?php echo $rows['email']; ?>" size="15"></td>
</tr>

<tr>
<td>&nbsp;</td>
<td><input name="id" type="hidden" id="id" value="<?php 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 code--

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="new"; // Database name
$tbl_name="web_members"; // Table name

// 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");

$id=$_GET['id'];

$name=$_GET['name'];

$lastname=$_GET['lastname'];

$email=$_GET['email'];
echo "<hr>";
echo "<hr>";

echo $id, $name, "<hr>";

echo $rows['id'], "<hr>";

$b="Select * from $tbl_name where id='$id'";
$result=mysql_query($b);
while($rows=mysql_fetch_array($result))
{
echo $rows['id'], "<hr>";
echo $rows['name'], "<hr>";
echo $rows['lastname'], "<hr>";
echo $rows['email'], "<hr>";
}



// update data in mysql database
$sql="UPDATE $tbl_name 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_records.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

<form name="form1" method="get" action="update_ac.php">
<?php
$id= $_GET['id']; //your id
$name= $_GET['name'];
$lastname= $_GET['lastname'];
$email= $_GET['email'];
?>
<input type="submit" name="commit" value="submit"/>
</form>

now, please solve this, i am sure it will be easy for you to trace the problem.

You should have defined the variable $tbl_name='your table name'; somewhere on the top of the php page. Removing single quote mark from $id as below, If id field is integer type.

$sql="UPDATE $tbl_name set name='$name', lastname='$lastname', email='$email' where id=$id";

Hope this will help.

Thanks dear!! i tried that method="get" and it start taking values correct inspite of taking blank. thanks! that why i have vote in your favor, keep this good work going. And keep guiding me too.

<form name="form1" method="get" action="update_ac.php">
$id= //your id
$name= //user name
$lastname=//lastname
 <input type="submit" name="commit" value="submit"/>
</form>

Check with the url on updata_ac.php whether it shows the query data or not? It will sure work

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.