I'm a php/mysql newbie, working on an invoicing application. I have a summary.php page, in which I'm using php to query a db and render a table with the retrieved data:
< ?php
mysql_connect ... [ snip ] ...
// retrieve all data in the 'Invoices' table ...
$result = mysql_query("SELECT * FROM Invoices") or die(mysql_error());
// ... and store each record in a variable:
$row = mysql_fetch_array($result);
and my table is:
<table class="record-summary">
<form action="/edit.php" method="post">
<tr>
<th ...
[ snip: more table headers ] ...
<?php
$controls = "<input type=\"submit\" value=\"details.php\" /><input type=\"submit\" value=\"edit.php\" />";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class=\"id\">". $row['ID']. "</td>";
echo "<td class=\"inv\">". $row['invoiceNumber']. "</td>";
[ snip: more data cells ] ...
echo "<td class=\"controls\">". $controls . "</td>";
The last cell (td.controls) in each row contains two submits, one linking to a detail view (details.php), the other to a form page (edit.php). My objective is to populate either page with the values of the record/row. Where I need help is how exactly to use the $row variable to carry this array to e.g., edit.php, and populate the fields in the form (actually I'm fairly certain I have the syntax for the form inputs' value attributes). Both details.php and edit.php have all the data fields in the record represented, though in summary.php only a portion of the fields are displayed (ergo, 'summary.php'). So my questions are:
How do I e开发者_运维知识库nsure that when either button is clicked, the $row contains the values of the given record, and how to push the array into either details.php or edit.php?
Many thanks in advance,
src
You can pass the ID to edit.php
and details.php
but without the form just with two links to edit.php?id=
and details.php?id=
:
<?php
// $controls = "<input type=\"submit\" value=\"details.php\" /><input type=\"submit\" value=\"edit.php\" />"; <-- is not necessary
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td class=\"id\"><a href=\"details.php?id=". $row['ID']. "\">details.php</a> | <a href=\"edit.php?id=". $row['ID']. "\">edit.php</a></td>";
echo "<td class=\"inv\">". $row['invoiceNumber']. "</td>";
// [ snip: more data cells ] ...
// echo "<td class=\"controls\">". $controls . "</td>"; <-- is not necessary
You can grab the data using the id:
//....
if (isset($_GET['id']) && is_numeric($_GET['id']))
{
$id = $_GET['id'];
}
else
{
die("There is no id.");
}
$sql = sprintf("SELECT * FROM Invoices WHERE id = %s", $id);
$result = mysql_query($sql) or die(mysql_error());
$row = mysql_fetch_array($result);
echo "<input type=\"hidden\" name=\"row\" value=\"" . $row . "\"/>";
Include this in your loop and your form's "action" script will be able to access the corresponding $row
variable via $_POST['row']
.
精彩评论