0

How would I go about creating a script that post data multiple times depending on how many times the user wants. I was thinking I could do it using a for loop but not sure where to start. The goal is to post this data row 1, 3, or maybe 5 times into the mySQL table. Anybody got any means on where to start?

Below is what I'm working with...

   // check if the form was submitted
if( isset($_POST['save_stuff']) ) {

  // create the website object
  $stuff = new stuff($_POST['stuff']);

  // prepare an SQL query
  $stmt = $dbh->prepare("INSERT INTO `".$example."` (`title`, `stuf`, `due`, `details`, `category`, `user`) VALUES (:title, :stuff, :due, :details, :category, :user)");

$id = mysql_real_escape_string($_GET['id']);


  // run the SQL query
  if(  $stmt->execute(array(
        'title' => $stuff->title,
        'stuff' => $id,
        'due' => $stuff->due,
        'details' => $stuff->details,
        'category' => $stuff->category,
        'user' => $stuff->user
      ))
  ) {

    // if successful then go back to home page
    header("Location: ../stuff/?id=".$id."");
  } else {

    // display an error if it failed
    echo "<p>failed to add stuff</p>";
  }
7
  • 3
    You don't need to escape a number before you're passing it to execute(). mysql_real_escape_string also makes no sense as soon as PDO provides its own one escape function Commented May 31, 2013 at 3:54
  • but what about the question? Commented May 31, 2013 at 4:40
  • execute() multiple times? Commented May 31, 2013 at 4:49
  • yes, how do I execute the array to submit multiple times in one instance? Commented May 31, 2013 at 4:51
  • 1
    what is "execute the array"? You need to invoke execute() method several times with different arguments Commented May 31, 2013 at 4:52

1 Answer 1

1
<?php 
// check if the form was submitted
if( isset($_POST['save_stuff']) ) {

    // create the website object
    $stuff = new stuff($_POST['stuff']);

    // prepare an SQL query
    $stmt = $dbh->prepare("INSERT INTO `".$example."` (`title`, `stuf`, `due`, `details`, `category`, `user`) VALUES (:title, :stuff, :due, :details, :category, :user)");

    // Unnecesary since PDO takes care of it anyway trough execute
    //$id = mysql_real_escape_string($_GET['id']);

    // number of times to run
    $timestorun = 10;
    for($i = 1; $i <= $timestorun; $i++){
        // Notice it will insert the same stuff multiple times! 
        // run the SQL query
        if(  $stmt->execute(array(
        'title' => $stuff->title,
        'stuff' => $id,
        'due' => $stuff->due,
        'details' => $stuff->details,
        'category' => $stuff->category,
        'user' => $stuff->user
        ))
        ) {
            $insertworkedArr[] = 1;
            $allIds .= $id . ",";
        } else {
            $insertworkedArr[] = 0;
        }
    }
    if(min($insertworkedArr) == 1){
        // if successful then go back to home page
        // $allIds contains all IDs that are inserted, accessible 
        // as an array trough an explode($allIds,",") at the next page
        header("Location: ../stuff/?id=".$id."&wasTheLastOneButAlso".$allIDs."");
    } else {
        // display an error if it failed
        echo "<p>failed to add stuff</p>";
    }
}
?>
Sign up to request clarification or add additional context in comments.

2 Comments

It functions like it's suppose inserting multiple times but it comes up with the error "failed to add stuff" instead of redirecting to the header.
Sorry, alright now it's changed to if(min($insertworkedArr) == 1) instead of 0. Tell me if it's working as a solution for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.