Forum Moderators: coopster

Message Too Old, No Replies

Updating data from form to database(PHP and MySQL)

         

slaly

7:30 am on Jun 1, 2009 (gmt 0)

10+ Year Member



im having problem updating data from a form to my database.i wanted something like this:

ID Name Telephone Birthday
1 aaa 123 1 July Edit
2 sss 456 15 August Edit
3 ddd 789 25 May Edit
4 fff 951 13 September Edit

And once u click the "Edit" link,for eg. you click on the first "edit" link, it will appear:

ID 1
Name aaa
Telephone 123
Birthday 1 July

For the bold values, it is suppose to be appearing in a textbox for others to change.i have started doing on the form and below are some of the codes that i've tried doing.

edit_form.php:

<form method="get">
<?php
//connects to database
$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("sally", $con);

$result = mysql_query("SELECT * FROM people");
Print "<table border cellpadding=3>";
while($row = mysql_fetch_array( $result ))
{
Print "<tr>";
Print "<td>" . $row['ID'] . "</td>";
Print "<td>" . $row['Name'] . "</td>";
Print "<td>" . $row['Telephone'] . "</td>";
Print "<td>" . $row['Birthday'] . "</td>";
Print "<td>" . "<a href='edit_one.php?action=edit&amp'>Edit</a>" . "</td>";

Print "</tr>";
}
echo "</table>";

?>
</form>

edit_one.php:

<input type="hidden" name="id" value="<? echo "$id" ?>">

<html><head><title></title></head>
<body>
<?php
$id=$_POST['id'];
$db="sally";
$con = mysql_connect("localhost", "root", "");
if (! $con){
die("Couldn't connect to MySQL");
}
mysql_select_db($db , $con)
or die("Couldn't open $db: ".mysql_error());

$result=mysql_query(" SELECT * FROM people WHERE id='$id'");

$num=mysql_num_rows($result);
$i=0;
while ($i < $num) {
$name=mysql_result($result,$i,"Name");
$telephone=mysql_result($result,$i,"Telephone");
$birthday=mysql_result($result,$i,"Birthday");
?>
<table width="300" cellpadding="3" cellspacing="0" border="2">
<tr align="center" valign="top">
<td colspan="1" rowspan="1" align="center">
<form action="edit_one.php" method="post">
<input type="hidden" name="id" value="<? echo "$id" ?>">
Name: <input type="text" name="name" value="<? echo "$name"?>"><br>
Telephone: <input type="text" name="telephone" value="<? echo "$telephone"?>"><br>
Birthday: <input type="text" name="birthday" value="<? echo "$birthday"?>"><br>
<input type="Submit" value="Update">
</form></td></tr></table>
<?
++$i;
}
if (!++$i){
Print "Fail to update!";
}
else{
Print "<? echo "$id"?>"<br>
Print "<? echo "$name"?>"<br>
Print "<? echo "$telephone"?>"<br>
Print "<? echo "$birthday"?>"<br>
}
?>
</body>
</html>

i can get the first part but not the 2nd part.sorry for the long and messy codes.please help me with my codes.thank you very much.

MartinWeb

3:43 am on Jun 3, 2009 (gmt 0)

10+ Year Member



This might not help, but in the link try using \" instead of ' . Also, are you sure that ++$i is a code? If it is, ignore me, but I normally use $i++ for addition.

slaly

4:06 am on Jun 3, 2009 (gmt 0)

10+ Year Member



im sorry.wrong code.

edit_one.php:

<input type="hidden" name="ID" value="<? echo "$ID" ?>">

<html><head><title></title></head>
<body>
<?php
if (isset($_GET["abc"]))
{
$ID=$_POST['ID'];
$Name = $_POST['Name'];
$Telephone = $_POST['Telephone'];
$Birthday = $_POST['Birthday'];

$con = mysql_connect("localhost","root","");
if (!$con)
{
echo "Fail to connect to mySQL!";
}
mysql_select_db("sally", $con);
$sql="UPDATE people
SET Name='$Name', Telephone='$Telephone', Birthday='$Birthday'
WHERE ID = $ID";
$result = mysql_db_query($db, $sql, $con);

if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
else {
echo "Updated successfully!";
}
//trying to mk the data appear into my textbox
//mysql_close($con);
}
?>

<?php
$query = mysql_query ("SELECT * from people WHERE ID = $ID");
if (!$query){
die('Error: ' . mysql_error());
}
else{
................
}
?>


<table width="300" cellpadding="3" cellspacing="0" border="2">
<tr align="center" valign="top">
<td colspan="1" rowspan="1" align="center">
<form method="get">
<p>
<input type="hidden" name="ID" value="<? echo $ID; ?>">
<p><label>Name: </label>&nbsp;

<? echo "<input type=\"text\" name=\"Name\" value=\"$Name\">"; ?>

</p>
<p><label>Telephone: </label>&nbsp;
<? echo "<input type=\"text\" name=\"Telephone\" value=\"$Telephone\">"; ?>
</p>
<label>Birthday: </label>&nbsp;
<? echo "<input type=\"text\" name=\"Birthday\" value=\"$Birthday\">"; ?>

<br>
<br>
<input name="abc" type="Submit" value="Update">
</p>
</form></td></tr></table>

</body>
</html>

im really stuck with the population of form for this.please help me.