imgrooot Posted January 13, 2019 Share Posted January 13, 2019 (edited) 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 January 13, 2019 by imgrooot Quote Link to comment https://forums.phpfreaks.com/topic/308159-is-there-a-way-to-submit-same-entry-multiple-times-on-a-single-form-submit/ Share on other sites More sharing options...
ginerjm Posted January 13, 2019 Share Posted January 13, 2019 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/308159-is-there-a-way-to-submit-same-entry-multiple-times-on-a-single-form-submit/#findComment-1563516 Share on other sites More sharing options...
imgrooot Posted January 14, 2019 Author Share Posted January 14, 2019 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!'; } Quote Link to comment https://forums.phpfreaks.com/topic/308159-is-there-a-way-to-submit-same-entry-multiple-times-on-a-single-form-submit/#findComment-1563532 Share on other sites More sharing options...
Barand Posted January 14, 2019 Share Posted January 14, 2019 You only need to bind once. You are checking only the last insert Quote Link to comment https://forums.phpfreaks.com/topic/308159-is-there-a-way-to-submit-same-entry-multiple-times-on-a-single-form-submit/#findComment-1563543 Share on other sites More sharing options...
ginerjm Posted January 14, 2019 Share Posted January 14, 2019 When this all is described better maybe I'll jump in again. Quote Link to comment https://forums.phpfreaks.com/topic/308159-is-there-a-way-to-submit-same-entry-multiple-times-on-a-single-form-submit/#findComment-1563547 Share on other sites More sharing options...
imgrooot Posted January 14, 2019 Author Share Posted January 14, 2019 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!'; } } Quote Link to comment https://forums.phpfreaks.com/topic/308159-is-there-a-way-to-submit-same-entry-multiple-times-on-a-single-form-submit/#findComment-1563553 Share on other sites More sharing options...
Barand Posted January 14, 2019 Share Posted January 14, 2019 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/308159-is-there-a-way-to-submit-same-entry-multiple-times-on-a-single-form-submit/#findComment-1563557 Share on other sites More sharing options...
imgrooot Posted January 15, 2019 Author Share Posted January 15, 2019 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); Quote Link to comment https://forums.phpfreaks.com/topic/308159-is-there-a-way-to-submit-same-entry-multiple-times-on-a-single-form-submit/#findComment-1563563 Share on other sites More sharing options...
Barand Posted January 15, 2019 Share Posted January 15, 2019 Yes Quote Link to comment https://forums.phpfreaks.com/topic/308159-is-there-a-way-to-submit-same-entry-multiple-times-on-a-single-form-submit/#findComment-1563566 Share on other sites More sharing options...
imgrooot Posted January 15, 2019 Author Share Posted January 15, 2019 54 minutes ago, Barand said: Yes Perfect! Thanks a bunch! Quote Link to comment https://forums.phpfreaks.com/topic/308159-is-there-a-way-to-submit-same-entry-multiple-times-on-a-single-form-submit/#findComment-1563567 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.