Forum Moderators: coopster
1st,i want the initial values in the database to appear in the textbox for people to edit and update.but i could not get it out.all i got out was eg,$Name.literally $Name.that is not what i want.i want the value of $Name and not $Name.
2nd,even if i ignore the 1st part mentioned above and go ahead updating my stuffs,i've got this syntax error.the error message is:
Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
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);
//retrieve data from database
$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&id=" . $row['ID'] . "'>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
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
$query = mysql_query ("SELECT * from people WHERE ID = $ID");
}
?>
<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="<?php echo $ID?>">
<p><label>Name: </label>
<input type="text" name="Name" value="<?php echo '$Name'?>">
</p>
<p><label>Telephone: </label>
<input type="text" name="Telephone" value="<?php echo '$Telephone'?>">
</p>
<label>Birthday: </label>
<input type="text" name="Birthday" value="<?php echo '$Birthday'?>">
<br>
<br>
<input name="abc" type="Submit" value="Update">
</p>
</form></td></tr></table>
</body>
</html>
i've tried checking my codes but they appeared fine to me.i really really need help.please help me solve my problems.sorry for the messy codes that i've attached.thank you in advance!
<input type="text" name="Name" value="<?php echo $Name; ?>">
<? echo "<input type=\"text\" name=\"Name\" value=\"$Name\">"; ?>
could it be the closing of connection that is the problem?because my SELECT statement is right before it closes connection.and because the connection is closed,my values cannot be displayed in the textbox.could it be this way?
im totally new.so please pardon me for my ignorance.
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);
//retrieve data from database
$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&id=" . $row['ID'] . "'>Edit</a>" . "</td>";
Print "</tr>";
}
echo "</table>";
/*
TODO:
1.) When the page loads, retrieve data from database
2.) Populate the form with the retrieved data
3.) When the user is done with the editing and clicks on the Update button, should send all form data back to the database for updating
4.) Display message on whether update was successful or not
*/
?>
</form>
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");
?>
<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>
<? echo "<input type=\"text\" name=\"Name\" value=\"$Name\">"; ?>
</p>
<p><label>Telephone: </label>
<? echo "<input type=\"text\" name=\"Telephone\" value=\"$Telephone\">"; ?>
</p>
<label>Birthday: </label>
<? 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 stuck at the part in bold.i do not know how to continue.please help...
but my variable Name is varchar(100).and varchar() needs the ' ' when showing the value of the variable right?
No, you do not need quotes around any variable when you want to parse it with PHP. The variable will be automatically parsed and evaluated. You would put the quotations only if you are comparing the data to a field in the db in a query. Since you are displaying as HTML text, you do not need use single quotes.
daveginorge might be on to something. I noticed you used
while($row = mysql_fetch_array( $result ))
On a further note, your form looks okay, minus one part. Typically, when creating a form, it is more practical and secure to use the POST method as so. Also since you are already using $_POST to retrieve data =)
<form method="POST" action="edit_one.php">
Then on the action page, it's just
$result = mysql_query("UPDATE people set Name=\"".$POST['Name']."\", Telephone=\"".$_POST['TelePhone']."\", Birthday=\"".$_POST['Birthday']."\" WHERE ID=".$_POST['ID'];//if query was successful
if(mysql_affected_rows()){}
//failed update
else{}
Of course, once you get everything working, don't forget to clean your data as well as making sure it's the right type, not empty, too long, too short, etc. All data should be evaluated with mysql_ real_ escape_ string() before any transaction with the db.