Michael Crothers Michael Crothers - 5 years ago 193
SQL Question

Redundant way of getting multiple $_POST records?

EDIT : I might just make it a more 'step-by-step' process i.e.

Seeing as it feels more user-friendly & it's only for <5 images. I have read & appreciate all of the help that I have received so far.

I'm fairly new to PHP & I'm building a basic PHP image editor which may allow multiple image data auditing which is then inserted into a MySQL database. A limit of 5 images may be uploaded at a time for this system (implying that there will only be a low amount of records to play with), but I'll get to that further down.


Some of the data columns used:

  • image id

  • caption

  • description

  • published

  • imageposition

  • delete button

All images are echo'd in a while loop by a previous 'SELECT *' result set. Each image will have the 'image id' echo'd inside each input name, so output will be for example:

caption-2, description-2, published-2, imageposition-2, caption-3, description-3

Also, the data values from the previous resultset are echo'd into the input value, allowing the client to edit the current data that exists in the database. There is only one submit button.

My question:

I want to be able to post all of the modified image(s) data to a processor.php form which will then allow me to insert it as an INSERT sql string into the MySQL images table. I need the PHP to be dynamic incase:

  • images are deleted (image id)

  • new images are uploaded (image id)

If there is an easier way of doing any of the above, I am welcome to new ideas/opinions. Sorry in advance for my poor understanding of PHP.

Answer Source

To delete the records easily :

<form action="processor.php" method="post">
foreach ($images as $img) { // assuming $images is the result set of your sql query
  <input type="text" name="name<?php $img['image_id'];?>" value="<?php $img['caption'];?>">
  <input type="text" name="desc<?php $img['image_id'];?>" value="<?php $img['description'];?>">
<a href="http://yoursiteurl/deleteimg.php?id=<?php $img['image_id']?>">
<?php } ?>

In deleteimg.php :

$del = $_POST[id];
$query = $msqli->prepare("DELETE FROM imagetablename WHERE image_id=?");
$query->bind_param( 's',  $del );

Updating and adding new rows may involve javascript, and much more harder.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download