Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

populate php form fields with mysql array (on click)

Tags:

arrays

php

mysql

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

like image 293
src Avatar asked Dec 07 '25 06:12

src


1 Answers

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);
like image 66
tttony Avatar answered Dec 08 '25 20:12

tttony



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!