0

I am creating a database front end that will populate a table with the records in a the db, putting all values into input boxes so that they can be changed. I want to be able to make changes and have it update just that specific record. This is what I currently have.

{
if($row["order_number"]===NULL)
        {
            $on='<input type="number" name="orderNo" />';
        } else {$on='<input type="number" name="orderNo" value="'. $row["order_number"] . '" />';}
        if($row["name"]===NULL)
        {
            $cn='<input type="text" name="cusName" />';
        }else {$cn='<input type="text" name="cusName" value="'. $row["name"] . '" />';}
        if($row["product_type"]===NULL)
        {
            $pt='<input type="text" name="prodType" />';
        } else {$pt='<input type="text" name="prodType" value="'. $row["product_type"] . '" />';}
        $id='<input type="number" name="orderID" value="'.$row["order_id"].'"/>';
        $tableCode.='<tr><td>' . $on . '</td><td>' . $cn . '</td><td>' . $pt . '</td><td><input type="submit" value="Update" name="'. $row["order_id"]. '" /></td><td hidden>'.$id.'</td></tr>';
}

This code is looped for all results in the table. samples.html.php simply sticks the results in some table tags on a webpage, along with the headers. The order_id is the primary key of the table (actually 2 tables, but I know how to handle that side of it)

Any help in this would be appreciated! Thanks!

  • Post your code, not pictures of your code. Use the code sample delimiter icon `{ }` to format. – Alex Howansky Jun 02 '17 at 20:03
  • Whatever it will look like it would be easier for us if you include the code within your question. – Picard Jun 02 '17 at 20:06
  • OK, I got it working. I didn't realize that I could just paste it in and hit the code button to get it working. I was manually adding in the 4 spaces and somehow messed up one of the last lines of code. – Robert Ellis Jun 02 '17 at 20:09
  • What's the problem you're having? What's not working? – Alex Howansky Jun 02 '17 at 20:10
  • Im not sure how I would make it so when I click on the button for the record I want to change, it only updates the particular record the button is next to. – Robert Ellis Jun 02 '17 at 20:14
  • I don't see any code to process the POST or perform any sort of update query. – Alex Howansky Jun 02 '17 at 20:17
  • @AlexHowansky I wasnt sure how to begin with handling a multiple button set up. Just before I saw your comment, I did come up with an idea that may solve my problem, I just need to implement it. I will come back and let you know if that worked. Thank you for your patience, either way. – Robert Ellis Jun 02 '17 at 20:24
  • Alright, I'm pretty sure this will work, Im just going to switch to using a CSS display:table setup and have each row be its own form. That should allow me to do everything I'm looking for. Thank you for your time. Once I have verified that my solution works, I will close the question. – Robert Ellis Jun 02 '17 at 20:47

1 Answers1

0

OK, as promised, here is what I came up with. Mignt not be the best way, but it seems to work for my relatively small application.

while($row = mysqli_fetch_assoc($result))
{
    $on='';
    $cn='';
    $pt='';
    $id='';
    if($row["order_number"]===NULL)
    {
        $on='<input type="number" name="orderNo" />';
    } else {$on='<input type="number" name="orderNo" value="'. $row["order_number"] . '" />';}
    if($row["name"]===NULL)
    {
        $cn='<input type="text" name="cusName" />';
    }else {$cn='<input type="text" name="cusName" value="'. $row["name"] . '" />';}
    if($row["product_type"]===NULL)
    {
        $pt='<input type="text" name="prodType" />';
    } else {$pt='<input type="text" name="prodType" value="'. $row["product_type"] . '" />';}
    $id='<input type="number" name="orderID" value="'.$row["order_id"].'"/>';
    $tableCode.='<form class="tr" action="?" method="post"><span class="td">' . $on . '</span><span class="td">' . $cn . '</span><span class="td">' . $pt . '</span><span class="td"><input type="submit" value="Update" name="Update" /></span><span hidden class="td_hidden">'.$id.'</span></form>';
}

This way, only the values from the form that I used get submitted, so only that row is updated. I used CSS display:table styling to get it to look like a table, similar to the third answer of this question.

Thanks everyone for their willingness to help! If anyone has ideas for improvements, I am open to them.