kcm2611 Posted August 8, 2010 Share Posted August 8, 2010 I am trying to write a script to add multiple lines into the database with one submit, this is my effort - can anyone suggest anything better that works Many Thanks <?php error_reporting (E_ALL); include "include/z_db.php"; // $query = "INSERT INTO table(name,surname,age)VALUES"; foreach ($_POST["name"] as $key => $value) { $query = "('$key','$_POST[surname][$key]','$_POST[age][$key]')"; $result = mysql_query($query)or die(mysql_error()); if (isset($_POST['addto'])) { $name = $_POST['name']; $surname = $_POST['surname']; $age = $_POST['age']; if ($age == '0' && $surname == '0') { echo "0"; } else { $queries = array(); for ($i = 0; $i < count($name); $i++) { if (!get_magic_quotes_gpc()) { $name[$i] = addslashes($name[$i]); $surname[$i] = addslashes($surname[$i]); $age[$i] = addslashes($age[$i]); # etc } $queries[] = "('$userid ','$name[$i]','$surname[$i]','$age[$i]')"; } if (count($queries) == 0) { # Nothing passed # exit } $piece = implode(", ", $queries); $query = "INSERT INTO 'TABLE' (id,name,surname,age) VALUES $piece"; $result = mysql_query($query) or die(mysql_error()); } } ?> <form action='result.php'method="POST"> <table> <tr> <td>name<input type="text" name="name[]"></td> <td>surname<input type="text" name="qty[]"></td> <td>age<input type="text" name="qty2[]"></td> </tr> <tr> <td>name<input type="text" name="name[]"></td> <td>surname<input type="text" name="qty[]"></td> <td>age<input type="text" name="qty2[]"></td> </tr> <tr> <td>name<input type="text" name="name[]"></td> <td>surname<input type="text" name="qty[]"></td> <td>age<input type="text" name="qty2[]"></td> </tr> <tr> <td>name<input type="text" name="name[]"></td> <td>surname<input type="text" name="qty[]"></td> <td>age<input type="text" name="qty2[]"></td> </tr> <tr> <td>mane<input type="text" name="name[]"></td> <td>surname<input type="text" name="qty[]"></td> <td>age<input type="text" name="qty2[]"></td> </tr></table> <td><input type="submit" name="submit" value="add"> Quote Link to comment Share on other sites More sharing options...
TOA Posted August 8, 2010 Share Posted August 8, 2010 Whats wrong with it? What does it do/not do? Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 8, 2010 Share Posted August 8, 2010 I'd name the form fields like so <form action='result.php'method="POST"> <table> <tr> <td>name<input type="text" name="person[0][name]"></td> <td>surname<input type="text" name="person[0][surname]"></td> <td>age<input type="text" name="person[0][age]"></td> </tr> <tr> <td>name<input type="text" name="person[1][name]"></td> <td>surname<input type="text" name="person[1][surname]"></td> <td>age<input type="text" name="person[1][age]"></td> </tr> </table> <input type="submit" name="submit" value="add"> </form> Now all data from the form will be grouped together in the $_POST['person'] array. Now you can easily loop through the data if(isset($_POST['submit'])) { foreach($_POST['person'] as $person) { $name = mysql_real_escape_string($person['name']); $surname = mysql_real_escape_string($person['surname']); $age = (int) $person['age']; $values[] = "('$name', '$surname', $age)"; } $query = "INSERT INTO table(name,surname,age) VALUES " . implode(', ', $values); $result = mysql_query($query); } Quote Link to comment Share on other sites More sharing options...
kcm2611 Posted August 8, 2010 Author Share Posted August 8, 2010 Thanks Wildteen88 for your help, I am not sure if I have still got something wrong but still doesnt work any suggestions thanks <?php error_reporting (E_ALL); include "include/z_db.php"; include "include/session.php"; if(isset($_POST['submit'])) { foreach($_POST['person'] as $person) { $name = mysql_real_escape_string($person['name']); $surname = mysql_real_escape_string($person['surname']); $age = (int) $person['age']; $values[] = "('$name', '$surname', $age)"; } $query = "INSERT INTO plus_signup (name,surname,age) VALUES " . implode(', ', $values); $result = mysql_query($query); } ?> <form method="POST" action='multi.php' name="multi"> <table> <tr> <td>name<input type="text" name="prrson[0][name]"></td> <td>surname<input type="text" name="person[0][surname]"></td> <td>age<input type="text" name="person[0][age]"></td> </tr> <tr> <td>name<input type="text" name="person [1][name]"></td> <td>surname<input type="text" name="person[1][surname]"></td> <td>age<input type="text" name="person[1][age]"></td> </tr> <tr> <td>name<input type="text" name="person [2][name]"></td> <td>surname<input type="text" name="person[2][surname]"></td> <td>age<input type="text" name="person[2][age]"></td> </tr> </table> <td><input type="submit" name="submit" value="add"> </form> Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 8, 2010 Share Posted August 8, 2010 Umm, Maybe there is an error $result = mysql_query($query) or die("Error!<br />Query: $query<br />Error: " . mysql_error()); EDIT: Just noticed this. Your form fields are mistyped, prrson[0] should be person[0]. Remove the space for person [1] and person [2]. PHP will treat those three names differently. Quote Link to comment Share on other sites More sharing options...
kcm2611 Posted August 8, 2010 Author Share Posted August 8, 2010 getting closer I feel I now get this error when I fill all fields in Error! Query: INSERT INTO plus_signup (name,surname,age) VALUES ('n0', 's0', a0), ('n1', 's1', a1), ('n2', 's2', a2) Error: Unknown column 'a0' in 'field list' I have a field 'age' <?php error_reporting (E_ALL); include "include/z_db.php"; include "include/session.php"; if(isset($_POST['submit'])) { foreach($_POST['person'] as $person) { $name= mysql_real_escape_string($person['name']); $surname= mysql_real_escape_string($person['surname']); $age= mysql_real_escape_string($person['age']); $values[] = "('$name', '$surname', $age)"; } $query = "INSERT INTO plus_signup (name,surname,age) VALUES " . implode(', ', $values); $result = mysql_query($query) or die("Error!<br />Query: $query<br />Error: " . mysql_error()); } ?> <form method="POST" action='multi.php' name="multi"> <table> <tr> <td>name<input type="text" name="person[0][name]"></td> <td>surname<input type="text" name="person[0][surname]"></td> <td>age<input type="text" name="person[0][age]"></td> </tr> <tr> <td>name<input type="text" name="person[1][name]"></td> <td>surname<input type="text" name="person[1][surname]"></td> <td>age<input type="text" name="person[1][age]"></td> </tr> <tr> <td>name<input type="text" name="person[2][name]"></td> <td>surname<input type="text" name="person[2][surname]"></td> <td>age<input type="text" name="person[2][age]"></td> </tr> </table> <td><input type="submit" name="submit" value="add"> </form> Quote Link to comment Share on other sites More sharing options...
TOA Posted August 8, 2010 Share Posted August 8, 2010 It's hard to read code like that (use code tags please) but I noticed you have the rest in single quotes and not the one causing an issue... Quote Link to comment Share on other sites More sharing options...
kcm2611 Posted August 8, 2010 Author Share Posted August 8, 2010 hopefully this will be better to read now, have added those quotes & now get this error Error! Query: INSERT INTO plus_signup (name,surname,age) VALUES ('n0', 's0', 'a0'), ('n1', 's1', 'a1'), ('n2', 's2', 'a2') Error: Duplicate entry '' for key 1 <?php error_reporting (E_ALL); include "include/z_db.php"; include "include/session.php"; if(isset($_POST['submit'])) { foreach($_POST['person'] as $person) { $name= mysql_real_escape_string($person['name']); $surname= mysql_real_escape_string($person['surname']); $age= mysql_real_escape_string($person['age']); $values[] = "('$name', '$surname', '$age')"; } $query = "INSERT INTO plus_signup (name,surname,age) VALUES " . implode(', ', $values); $result = mysql_query($query) or die("Error!<br />Query: $query<br />Error: " . mysql_error()); } ?> <form method="POST" action='multi.php' name="multi"> <table> <tr> <td>name<input type="text" name="person[0][name]"></td> <td>surname<input type="text" name="person[0][surname]"></td> <td>age<input type="text" name="person[0][age]"></td> </tr> <tr> <td>name<input type="text" name="person[1][name]"></td> <td>surname<input type="text" name="person[1][surname]"></td> <td>age<input type="text" name="person[1][age]"></td> </tr> <tr> <td>name<input type="text" name="person[2][name]"></td> <td>surname<input type="text" name="person[2][surname]"></td> <td>age<input type="text" name="person[2][age]"></td> </tr> </table> <td><input type="submit" name="submit" value="add"> </form> Quote Link to comment Share on other sites More sharing options...
jcbones Posted August 8, 2010 Share Posted August 8, 2010 Error! Query: INSERT INTO plus_signup (name,surname,age) VALUES ('n0', 's0', 'a0'), ('n1', 's1', 'a1'), ('n2', 's2', 'a2') Error: Duplicate entry '' for key 1 This error is generated from the database. Your query is good, only you have one of those columns named a primary, or unique key. This means you cannot put duplicate values in that column. Your script is good. Post your database structure, and we can help. Quote Link to comment Share on other sites More sharing options...
kcm2611 Posted August 14, 2010 Author Share Posted August 14, 2010 Hi thanks for all the help on this it working a treat.. I have one more question if there is a line of fields that is not completed how can I stop it inserting a blank set of fields Many thanks again Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 14, 2010 Share Posted August 14, 2010 You'll have to perform some validation within the foreach loop, eg if(isset($_POST['submit'])) { $values = array(); foreach($_POST['person'] as $person) { // performing some basic validation // checking to see if name and surname is not empty // and that age is a number if(!empty($_POST['name']) && !empty($_POST['surname']) && is_numeric($_POST['age'])) { $name = mysql_real_escape_string($person['name']); $surname = mysql_real_escape_string($person['surname']); $age = mysql_real_escape_string($person['age']); $values[] = "('$name', '$surname', '$age')"; } } if(count($values) > 0) { $query = "INSERT INTO plus_signup (name,surname,age) VALUES " . implode(', ', $values); $result = mysql_query($query) or die("Error!<br />Query: $query<br />Error: " . mysql_error()); } } Quote Link to comment Share on other sites More sharing options...
kcm2611 Posted August 15, 2010 Author Share Posted August 15, 2010 Thanks again for all your help How can I now add the userid='$_SESSION[userid] to the table that i am using for my multiple line form, from the table that the user logs onto . i.e the person loged in then populates the multiple form & they all have the loged in users id in there table I have tried combining the multiple line form table with the login table but I cant seem to get it to work, but if i have 2 different tables it works ok ie inserting 'plus_signup' instead of 'plus_user' $query = "INSERT INTO plus_user (name,surname,age) VALUES " . implode(', ', $values); <? include "include/session.php"; include "include/z_db.php"; // check the login details of the user and stop execution if not logged in require "check.php"; // If member has logged in then below script will be execuated. // let us collect all data of the member $row=mysql_fetch_object(mysql_query("select * from plus_signup where userid='$_SESSION[userid]'")); if(isset($_POST['submit'])) { foreach($_POST['person'] as $person) { $name= mysql_real_escape_string($person['name']); $surname= mysql_real_escape_string($person['surname']); $age= mysql_real_escape_string($person['age']); $values[] = "($name', '$surname', '$age')"; } $query = "INSERT INTO plus_user (name,surname,age) VALUES " . implode(', ', $values); $result = mysql_query($query) or die("Error!<br />Query: $query<br />Error: " . mysql_error()); } ?> <form method="POST" action='multi.php' name=""> <table> <tr> <td>Name<input type="text" name="person[0][name]"></td> <td>surname<input type="text" name="person[0][surname]"></td> <td>age<input type="text" name="person[0][age]"></td> </tr> <tr> <td>name<input type="text" name="person[1][name]"></td> <td>surname<input type="text" name="person[1][surname]"></td> <td>age<input type="text" name="person[1][age]"></td> </tr> <tr> <td>name<input type="text" name="person[2][name]"></td> <td>surname<input type="text" name="person[2][surname]"></td> <td>age<input type="text" name="person[2][age]"></td> </tr> </table> <td><p> <input type="submit" name="submit" value="add"> </p> <p> </p>| <a href=update-profile.php>back</a>| </form> <? require "bottom.php"; ?> <center> <br>| <a href=update-profile.php>back</a>|<br></center> </body> </html> Quote Link to comment 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.