homepage Welcome to WebmasterWorld Guest from 54.197.108.124
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
How to make data in MySQL appear in textbox for users to edit?
slaly




msg:3924447
 6:02 am on Jun 2, 2009 (gmt 0)

im a beginner learning php and i've met some errors and problems on the way.i've attached 2 codes that i've done.

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>&nbsp;
<input type="text" name="Name" value="<?php echo '$Name'?>">
</p>
<p><label>Telephone: </label>&nbsp;
<input type="text" name="Telephone" value="<?php echo '$Telephone'?>">
</p>
<label>Birthday: </label>&nbsp;
<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!

 

d40sithui




msg:3924882
 4:42 pm on Jun 2, 2009 (gmt 0)

When you use single quotes with the echo() function, the variable will be displayed as is - in other words, not the value itself. Try this:

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

or

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

As for the mysql error, i cant be sure. What is on line 3? try printing out the query to see if it is valid on screen.

slaly




msg:3925201
 1:28 am on Jun 3, 2009 (gmt 0)

but my variable Name is varchar(100).and varchar() needs the ' ' when showing the value of the variable right?

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.

slaly




msg:3925222
 2:50 am on Jun 3, 2009 (gmt 0)

may be i should rephrase my question.i just want to populate the form by retrieving data from my own database.i'm currently stuck at the SELECT statement.i do not know how to continue.can anyone give me the codes to do it?

slaly




msg:3925223
 2:53 am on Jun 3, 2009 (gmt 0)

updated codes:

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>&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 stuck at the part in bold.i do not know how to continue.please help...

daveginorge




msg:3925464
 9:06 am on Jun 3, 2009 (gmt 0)

Try reading through here
[no2.php.net...]

d40sithui




msg:3925747
 4:38 pm on Jun 3, 2009 (gmt 0)


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 ))

Since you use mysql_fetch_array(), you can't use the associative keys to retrieve the data. Your data will be in $row[0], $row[1], $row[2], etc...A quick fix is to change your result set to mysql_fetch_assoc(). It is the same, but you can retrieve your data with $row['ID'], $row['Name'], etc

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.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved