Sven van den Boogaart
Sven van den Boogaart

Reputation: 12323

Table with post button

i get data from my mysql database and want to make a list. in the list i want a button that uses post to send a value to the next page.

$result = mysql_query("SELECT * FROM all");
echo "<table border=\"1\">";
echo"<tr>
    <th>ID</th>
    <th>Naam</th>
    <th>Eigenaar</th>
    <th>Plaats</th>
    <th>Bewerk</th>
    </tr>";
    ?>
    <form action="editshop.php" method="post">
    <?php
    while($rij = mysql_fetch_array($result))
  {

    ?> 


        <?php
        echo "<tr><td>".$rij['id'] . "</td><td> " . $rij['naam'] ."</td><td>";
        echo $rij['eigenaar'] . "</td><td>" . $rij['plaats']."</td>" ;
        echo"<input type=\"hidden\" name=\"id\" value=".$rij['id']."  /><td><input type=\"submit\" /></td>";
        echo "</tr>";
    }
echo "</form></table>";

the table looks good but when i pres the button and the editshops.php opens wher i have echo $_POST["id"]; it always the last id of the list. i only want the number thats the same as the id in the list. in the list the id's are shown good. What have i done wrong (sorry if the layout of this post is not good this is my firstpost and i dont understand how to highlight the code) Update changed the loop to

<?php
    while($rij = mysql_fetch_array($result))
  {

    ?> 
        <form action="bewerkshop.php" method="post">

        <?php
        echo "<tr><td>".$rij['id'] . "</td><td> " . $rij['naam'] ."</td><td>";
        echo $rij['eigenaar'] . "</td><td>" . $rij['plaats']."</td>" ;
        echo"<td><input type=\"submit\" name=\"id\" value=\"".$rij['id']."\"  /></td>";
        echo "</tr>";
    }
echo "</form></table>";

?>

all working now but the names of the buttons are the id number can i change it to edit. so all the buttons say edit and not the id number

Upvotes: 2

Views: 1406

Answers (4)

Daniel M
Daniel M

Reputation: 3379

NOTE: I'm not sure if you want to edit multiple rows with a submit or only one. My answer is only helpful for editing multiple rows.

You're writing an input field for each row, all with the same name. That's the reason why the last one overwrites all the others.

You need to name your input field accordingly.

Example:

<input name="naam[1]" value="..." />

Where 1 here is the ID of your row.

You can then iterate over $_POST['naam'] and fetch the values.

foreach ($_POST['naam'] as $id => $value) {
    $otherValue = $_POST['otherValue'][$id];
    // ...

    // do something with it and add some checks if the keys exist.
}

Upvotes: 1

AboQutiesh
AboQutiesh

Reputation: 1716

because all your hidden input have the same name when you post it will give you the last id either you can post as an array

"<input type=\"hidden\" name=\"id[]\" value=".$rij['id']."  />

or post them by passing the id to the submit button itself.

Upvotes: 0

wich
wich

Reputation: 17117

I believe you need to use something like name="id[]" to get an array

I see now that you have a submit button per row, that means you need to make a form per row, or put the id value in the submit button itself.

Upvotes: 1

Fluffeh
Fluffeh

Reputation: 33504

It looks like the <input> is missing a set of quotes around the value:

value=\"".$rij['id']."\"

Should do the trick properly.

Also however, if you are using the same name for each input, PHP will overwrite the data to the last ID sent (As all the hidden inputs have the same name).

You might want to consider making a form for each row of data. so you know which one was actually sent - or use some javascript to submit the form and select the right value to pass.

Upvotes: 0

Related Questions