trying to display mysql data in an html text field


trying to display mysql data in an html text field



I would like to be able to view and edit information contained within a table from my web browser however I can't for the life in me get the current values to pull though to an html text field.



Can anyone shed any light as im quite new to php?



Table name: request_details



Column Names: id, name, email_address



My PHP code is:


<?
$order = "SELECT * FROM request_details WHERE id='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>



HTML Code


<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo "$row[id]"?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name" size="20" value="<?php echo "$row[name]"?>">
</td>
</tr>
<tr>
<td>Email Address</td>
<td>
<input type="text" name="email_address" size="40" value="<?php echo "$row[email_address]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>



Right to make it a bit easier, when I use an actual ID not a variable it works, so for example


<?
include "db.inc.php";//database connection
$order = "SELECT * FROM request_details WHERE id='19'";
$result = mysql_query($order);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
?>



The above displays, yet the below does not


<?
include "db.inc.php";//database connection
$order = "SELECT * FROM request_details WHERE id='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
?>



Ok, so the premise is I have two web pages, the first displays all the information I require



code is:


<table>
<tr>
<td>
<table border="1">
<tr>
<td>Name</td>
<td>Telephone Number</td>
<td>Email Address</td>
<td>Venue</td>
<td>Event Date</td>
<td>Guests</td>
<td>Chair Cover Colour</td>
<td>Sash Colour</td>
<td>Price</td>
<td>Damage Deposit</td>
<td>Status</td>
<td>Notes</td>
</tr>
<tr>

<?
$order = "SELECT * FROM request_details";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){

echo ("<tr><td>$row[name]</td>");
echo ("<td>$row[telephone_number]</td>");
echo ("<td>$row[venue]</td>");
echo ("<td>$row[event_date]</td>");
echo ("<td>$row[guests]</td>");
echo ("<td>$row[cover_colour]</td>");
echo ("<td>$row[sash_colour]</td>");
echo ("<td>$row[price]</td>");
echo ("<td>$row[damage_deposit]</td>");
echo ("<td>$row[notes]</td>");
echo ("<td><a href="edit_form.php?id=$row[id]">View</a></td></tr>");
}
?>
</table>
</td>
</tr>
</table>



When i click on view it takes me to the following url:



edit_form.php?id=1



for example



edit_form.php?id=19



The code for this page (where the information isn't displaying in the text field):


<table border=1>
<tr>
<td>
<table>

<?
$order = "SELECT * FROM request_details WHERE id='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result,MYSQL_ASSOC);
?>

<form method="post" action="edit_data.php">
<tr>
<input type="hidden" name="id" value="<?php echo $row['id']; ?>" />
<tr>
<td>Name</td>
<td>
<input type="text" name="name" size="20" value="<?php echo $row['name']; ?>">
</td>
</tr>
<tr>
<td>Email Address</td>
<td>
<input type="text" name="email_address" size="40"
value="<?php echo $row['email_address']; ?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit" name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>





Have you turned on error reporting? Do you get any errors? What troubleshooting steps have you done to identify the issue?
– Amal Murali
Aug 26 '13 at 12:06





i think you forgot the quotes around the row index name.. $row["name"] not $row[name]
– Kenneth Garza
Aug 26 '13 at 12:06





Your code is exceptionally vulnerable to attack using it as it is. You really should investigate PDO (php.net/manual/en/book.pdo.php) and prepared statements. This is like leaving your car windows open while you leave your valuables in the car and go to work.
– Fluffeh
Aug 26 '13 at 12:09





6 Answers
6



Change your php code to



$row = mysql_fetch_array($result,MYSQL_ASSOC);



Also change the HTML code like the following line


<input type="hidden" name="id" value="<?php echo $row['id'] ;?>" />



You can do like this :


<input type="text" name="email_address" size="40" value="<?=$row['email_address']?>">



Your code is incomplete, it misses a definition within the call of the row. It doesnt know $row[name] because the variable should be defined by quotations.


<input type="text" name="name" size="20" value="<? echo $row['name']; ?>">
<input type="text" name="email_address" size="40" value="<? echo $row['email_address']; ?>">



Next to that, you are quoting on a variable, which is not needed. A variable doesnt have to be quoted at all. You quote the value type correctly, the double quote doesnt make sense.



Your code is really messy btw. Try putting it correctly down with the appropiate syntax. (dont forget ; behind the variable)



do them like that


<input type="hidden" name="id" value="<?php echo $row['id'] ;?>" />

<input type="text" name="name" size="20" value="<?php echo $row['name']; ?>" />

<input type="text" name="email_address" size="40" value="<?php echo $row['email_address']; ?>" />



edit;



sorry to say that your code is catastroph and messed up . many errors there i cant fix all. any way change your line to this line.


echo "<td><a href="edit_form.php?id=$row['id']">View</a></td></tr>";



and add in the file where you want display edit page , before your query this line


$id = $_GET['id'] ;





still no joy, but thanks for your suggestion
– user2717967
Aug 26 '13 at 12:13





@user2717967 then the problem is not here , this is how you echo values from database. i guesss your problem is in the $id in your query. did you tried to echo it and see if there is value ?
– echo_Me
Aug 26 '13 at 12:23



$id





i have tried to just echo and display in a <td></td> tag but no joy there either, so im presuming it is something rather simple that i've missed
– user2717967
Aug 26 '13 at 12:40





i said just try echo $id ; before your query and see if there is something echoed.
– echo_Me
Aug 26 '13 at 12:41



echo $id ;





no nothing when i try that
– user2717967
Aug 26 '13 at 12:43



i made corrections for the hole code and it works


<html>
<head>
<title>PAGE Edit</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<table>
<tr>
<td align="center">EDIT DATA</td>
</tr>
<tr>
<td>
<table border="1">
<?php
include"db.inc.php";//database connection
$order = "SELECT * FROM data_employees";
$result = mysql_query($order);
while ($row=mysql_fetch_array($result)){
// Print "<th>Name:</th> <td>".$data['user'] . "</td> ";
echo ("<tr><td>".$row['name']."</td>");
echo ("<td>".$row['employees_number']."</td>");
echo ("<td>".$row['address']."</td>");
echo ("<td><a href=edit_form.php?id=".$row['employees_number'].">Edit</a></td></tr>");
}
?>
</table>
</td>
</tr>
</table>
</body>
</html>



Second PART : Calling edit_form.php to edit


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Form Edit Data</title>
</head>

<body>
<table border=1>
<tr>
<td align=center>Form Edit Employees Data</td>
</tr>
<tr>
<td>
<table>
<?php
$id=$_GET['id'];

include "db.inc.php";//database connection
$order = "SELECT * FROM data_employees
where employees_number='$id'";
$result = mysql_query($order);
$row = mysql_fetch_array($result);
?>
<form method="post" action="edit_data.php">
<input type="hidden" name="id" value="<?php echo "$row[employees_number]"?>">
<tr>
<td>Name</td>
<td>
<input type="text" name="name"
size="20" value="<?php echo "$row[name]"?>">
</td>
</tr>
<tr>
<td>Address</td>
<td>
<input type="text" name="address" size="40"
value="<?php echo "$row[address]"?>">
</td>
</tr>
<tr>
<td align="right">
<input type="submit"
name="submit value" value="Edit">
</td>
</tr>
</form>
</table>
</td>
</tr>
</table>
</body>
</html>



Finally calling edit_Data.php to update the DATA TABLE


<?php
//edit_data.php
$name=$_POST['name'];
$address=$_POST['address'];
$id=$_POST['id'];
include "db.inc.php";
$order = "UPDATE data_employees
SET name='$name',
address='$address'
WHERE
employees_number='$id'";
mysql_query($order);
header("location:edit.php");
?>



I hope that it's clear now ! :D



If your URL is edit_form.php?id=19



You need to write your query like:


$order = "SELECT * FROM request_details WHERE id='$_GET[id]'";






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to input without newline? (Python)

C++ thread error: no type named ‘type’ MINGW

Analog for TagView in flutter