Skip to main content
July 3, 2012
Question

Updating many records PHP script

  • July 3, 2012
  • 1 reply
  • 1119 views

So I use a while loop to show all the photos (using their URL links in the database) as well as 2 inputs: 1) Checkbox and 2) Textbox. The form names I give for each images checkbox and textbox are A$PhotoID and B$PhotoDescription respectively (so all the checkboxes can be accessed through A$PhotoID and B$PhotoDescription - via the post).

However, I'm really confused with the PHP update script.

I want my PHP script to do the following:

-For each of the checkbox values (A) I need to update that field in the DB for each of the Photos (A$PhotoID)

-For each of the textbox values (B) I need to update that field in the DB for each of the photos (B$PhotoID)

Not sure if this is the right way (at all!) to achieve this. I'm going for the old-school Facebook-esk Edit Photos page - where you can change the descriptions and check the box for the Main photo (although my box-checking is a Boolean value which means 'whether to display in the websites gallery or not').

Please can someone enlighten me? Thanks a lot,

Chris

This topic is closed to new replies. Start a new post to keep the conversation going.

1 reply

Inspiring
July 9, 2012

Here is what you need to do.

In the dreamweaver update code you need to replace

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {

with

foreach($_POST['checkbox'] as $i) {

then add [$i] to each of the GetSQLValueString.  For example if your form sends it as photo you should have GetSQLValueString($_POST['photo'], "text"),

you need to replace with

GetSQLValueString($_POST['photo'][$i], "text"),

Finally in your form you need to do this.

Before the loop put in this code:

<?php $i=0;?>

Then in you form for the checbox you have this now or something simliar:

<input type="checkbox" name="checkbox" id="checkbox">

Replace with:

<input type="checkbox" name="checkbox[]" id="checkbox" value="<?php echo $i++;?>">

Also each form field that you have, the name needs to end in [].

Below is an example of a form that I have inserting multiple records.  The update is the same principal, but this will hopefully help you see it all in action so you make changes to your site.

Form Code:

<?php $i=0;?>

<?php do { ?>

            <tr>

              <td width="150"><?php echo $row_Recordset2['first_name']; ?> <?php echo $row_Recordset2['last_name']; ?></td>

              <td width="40"><input type="checkbox" name="checkbox[]" id="checkbox" value="<?php echo $i++;?>">

                <input type="hidden" name="ticket_id[]" value="<?php echo $_SESSION['lastid'];?>">

                <input type="hidden" name="email[]" value="<?php echo $row_Recordset2['email']; ?>">

                <input type="hidden" name="name[]" value="<?php echo $row_Recordset2['first_name']; ?> <?php echo $row_Recordset2['last_name']; ?>">

                <label for="checkbox"></label></td>

            </tr>

            <?php } while ($row_Recordset2 = mysql_fetch_assoc($Recordset2)); ?>

Insert Code:

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

foreach($_POST['checkbox'] as $i) {
  $insertSQL = sprintf("INSERT INTO cc_users (ticket_id, email, name) VALUES (%s, %s, %s)",
                       GetSQLValueString($_POST['ticket_id'][$i], "int"),
                       GetSQLValueString($_POST['email'][$i], "text"),
        GetSQLValueString($_POST['name'][$i], "text"));

  mysql_select_db($database_helpdesk, $helpdesk);
  $Result1 = mysql_query($insertSQL, $helpdesk) or die(mysql_error());

  $insertGoTo = "new_ticket_email2.php";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}