'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>
Solution 1:[1]
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'] ;?>" />
Solution 2:[2]
You can do like this :
<input type="text" name="email_address" size="40" value="<?=$row['email_address']?>">
Solution 3:[3]
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)
Solution 4:[4]
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'] ;
Solution 5:[5]
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
Solution 6:[6]
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]'";
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Pranab Sharma |
| Solution 2 | |
| Solution 3 | |
| Solution 4 | |
| Solution 5 | user3524093 |
| Solution 6 | Harry |
