Jump to content

Is there a way to submit same entry multiple times on a single form submit?


imgrooot

Recommended Posts

Say I have an "Entries" table. I want to submit same multiple entries using a form submission. And If I have other queries submitted in the same form, I want those quarries to be submitted only once.  Is that possible to do? 

Here's my code.

if(isset($_POST['submit'])) {

  $entries = 10;

  $id   = 55;
  $name = 'Smith';

  $insert = $db->prepare("INSERT INTO entries(id, name) VALUES(:id, :name)");
  $insert->bindParam(':id', $id);
  $insert->bindParam(':name', $name);
  $result_insert = $insert->execute();
  if($result_insert == false) {
    echo 'Fail';
  } else {
    echo 'Success';
  }

}
?>
<form action="" method="post">
  <input type="submit" name="submit" value="SUBMIT" />
</form>

 

Edited by imgrooot
Link to comment
Share on other sites

I see a form with no inputs, just a button to submit.  Then I see a block of code that builds a query and executes it to insert a record with a static id and static name, neither of which comes from any form.  And it happens just once.

Despite your poorly written post I'm concerned that you have a poorly designed methodology here as well.  And where are these other "quarries" (a place to find rocks?) that you want to have executed as well?

From your post I half-expected some kind of input form with values that you then wanted to repeatedly post to a database using a unique key for each.  Quite an easy exercise.  That was not what you presented.

  • Like 1
Link to comment
Share on other sites

10 hours ago, ginerjm said:

I see a form with no inputs, just a button to submit.  Then I see a block of code that builds a query and executes it to insert a record with a static id and static name, neither of which comes from any form.  And it happens just once.

Despite your poorly written post I'm concerned that you have a poorly designed methodology here as well.  And where are these other "quarries" (a place to find rocks?) that you want to have executed as well?

From your post I half-expected some kind of input form with values that you then wanted to repeatedly post to a database using a unique key for each.  Quite an easy exercise.  That was not what you presented.

I have my reason to use an empty form form to submit outside data. I was not asking advice on that. I simply wanted to know how to insert the same query multiple times. Below is the updated query that does not. It seems to work fine, unless you think other wise.

$entries = 10;

$id   = 55;
$name = 'Smith';

$stmt = $db->prepare("INSERT INTO entries(id, name) VALUES(:id, :name)");

for($i = 1; $i <= $entries; $i++)  {
    $stmt->bindParam(':id', $id);
    $stmt->bindParam(':user_id', $name);
    $result_insert = $stmt->execute();
}
if($result_insert == false) {
  $errors[] = 'There was a problem!';
}

 

Link to comment
Share on other sites

7 hours ago, Barand said:

You only need to bind once.

You are checking only the last insert

 

Do you mean like this?

$entries = 10;

$id   = 55;
$name = 'Smith';

$stmt = $db->prepare("INSERT INTO entries(id, name) VALUES(:id, :name)");
$stmt->bindParam(':id', $id);
$stmt->bindParam(':user_id', $name);
for($i = 1; $i <= $entries; $i++)  {
    $result_insert = $stmt->execute();
  	if($result_insert == false) {
      $errors[] = 'There was a problem!';
    }
}

 

Link to comment
Share on other sites

Almost - you are binding to :user_id when you used :name in the query.

You could speed it up significantly by doing a multiple insert EG

$params = [];
$data = [];
for($i = 1; $i <= $entries; $i++)  {
    $params[] = '(?,?)';
    array_push($data, $id, $name);
}
$stmt = $db->prepare("INSERT INTO entries(id, name) VALUES " . join(',', $params));;
$stmt->execute($data);

which creates a query in the form

INSERT INTO entries (id, name) VALUES (55, 'Smith'), (55, 'Smith'), (55, 'Smith'), ... , (55, 'Smith');

Timings:

Yours : 0.3336 seconds
Mine  : 0.0591 seconds (5.6x faster)

With hundreds of records it could be up to 50 or 60 times faster.

  • Like 1
Link to comment
Share on other sites

1 hour ago, Barand said:

Almost - you are binding to :user_id when you used :name in the query.

You could speed it up significantly by doing a multiple insert EG


$params = [];
$data = [];
for($i = 1; $i <= $entries; $i++)  {
    $params[] = '(?,?)';
    array_push($data, $id, $name);
}
$stmt = $db->prepare("INSERT INTO entries(id, name) VALUES " . join(',', $params));;
$stmt->execute($data);

which creates a query in the form


INSERT INTO entries (id, name) VALUES (55, 'Smith'), (55, 'Smith'), (55, 'Smith'), ... , (55, 'Smith');

Timings:


Yours : 0.3336 seconds
Mine  : 0.0591 seconds (5.6x faster)

With hundreds of records it could be up to 50 or 60 times faster.

 

Oh wow, that's a much more efficient way of doing it. 

My original query has more fields so i tried to simplify it for demonstration purposes. That's where I made the mistake of having :user_id when there shouldn't be.

Just one question. Your example shows inserting two fields(id, name). If I wanted to insert four fields, would it look like this?

$params[] = '(?,?,?,?)';
array_push($data, $id, $name, $field3, $field4);

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.