PDA

View Full Version : Update in PHP


leenakrishna
Jan 1, 2009, 11:40 PM
This is my code.Please tell me how to update it.How can I get values individually so that I can update my database.please help me...

<?php
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("hai", $con);

$sql = "SELECT FirstName,LastName,Age FROM persons where Age='12'";

$result = @mysql_query($sql, $con) or die("Cannot execute query.". mysql_error());
$numrow = mysql_num_rows($result);
if ($numrow != 0)
{
// fetch each record in result set
for ($counter == 0; $row = mysql_fetch_row($result); $counter++)
{
// build table to display result
print ("<form action='pp1.php' method='post'><h2><center>Member Profile</center></h2>");
print ("<table border = '1' cellpadding = '3' cellspacing = '2' style = 'background-color: #ADD8E6'>");
print ("<tr>");
print ("<th>Firstname</th>");
print ("<th>Lastname</th>");
print ("</tr>");
print ("<tr>");

foreach ($row as $key => $value)
print ("<td><input type ='text' value='$value' size = '30'></td>");

print ("</tr>");
print ("</table>");
}
}


print ("<table>");
print ("<tr>");
print ("<td>&nbsp;</td>");
print ("<td width='4%'>&nbsp;</td>");
print ("<td></tr>");

print ("<tr>");
print ("<td>&nbsp;</td>");
print ("<td width='4%'>&nbsp;</td>");

print ("<td><input type='submit' name='submit' value='Upgrade'></td>");
print ("</table></form>");



mysql_close($con);




?>

yesbeckjs
Sep 13, 2010, 02:35 PM
I don't see an insert/update statement. If you did this by mistake, try including it and maybe I can assist you further.

rlerne
Oct 22, 2011, 10:56 PM
First things first, I had to update your code. It was kind of hard to read.

Secondly, I'd need more information on the table (are there any IDs available)... If you can provide that, I can show you how to make it update the names.


And just for your learning pleasure, here's the cleaned up script. P.S. You don't need to select the age column when you provide the value in the query :P


&lt;?php
$con = mysql_connect("localhost","root","");
If (!$con)
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;die('Could not connect: ' . Mysql_error());
&nbsp;&nbsp;&nbsp;&nbsp;}
mysql_select_db("hai", $con);
?&gt;

&nbsp;&nbsp;&nbsp;&nbsp;&lt;form action='pp1.php' method='post'&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;h2 align='center'&gt;Member Profile&lt;/h2&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;table border='1' cellpadding='3' cellspacing='2' style='background-color: #add8e6'&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;th&gt;First Name&lt;/th&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;th&gt;Last Name&lt;/th&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
&lt;?PHP
$query = mysql_query("SELECT FirstName,LastName FROM persons where Age=12") or die("Cannot execute query." . mysql_error());
while ($row = mysql_fetch_assoc($query))
&nbsp;&nbsp;&nbsp;&nbsp;{
&nbsp;&nbsp;&nbsp;&nbsp;echo "
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;tr&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;input type='text' value='{$row['FirstName']}' size='30' name='FirstName' /&gt;&lt;/td&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;td&gt;&lt;input type='text' value='{$row['LastName']}' size='30' name='LastName' /&gt;&lt;/td&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/tr&gt;";
&nbsp;&nbsp;&nbsp;&nbsp;}
?&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;/table&gt;
&nbsp;&nbsp;&nbsp;&nbsp;&lt;/form&gt;