3raser Posted April 10, 2011 Share Posted April 10, 2011 Is there a way to send exploded data into a database without using multiple loops just to do so? My current code, but it only inserts the name value in the database as Array. $chicken_names = mysql_real_escape_string($_POST['names']); $chicken_names_exp = explode(' ', $_POST['names']); foreach($chicken_names_exp as $value) { mysql_query("INSERT INTO names VALUES (null, '$chicken_names_exp"', '". $_POST['type'] ."')"); } echo "Success! You have successfully sent in your chicken names! <a href='index.php'>Home</a>"; Quote Link to comment https://forums.phpfreaks.com/topic/233281-exploded-data-insert-into-db/ Share on other sites More sharing options...
Jnerocorp Posted April 10, 2011 Share Posted April 10, 2011 shouldnt: mysql_query("INSERT INTO names VALUES (null, '$chicken_names_exp"', '". $_POST['type'] ."')"); be: mysql_query("INSERT INTO names VALUES (null, '$value"', '". $_POST['type'] ."')"); Quote Link to comment https://forums.phpfreaks.com/topic/233281-exploded-data-insert-into-db/#findComment-1199700 Share on other sites More sharing options...
3raser Posted April 10, 2011 Author Share Posted April 10, 2011 shouldnt: mysql_query("INSERT INTO names VALUES (null, '$chicken_names_exp"', '". $_POST['type'] ."')"); be: mysql_query("INSERT INTO names VALUES (null, '$value"', '". $_POST['type'] ."')"); Heh, yeah! Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/233281-exploded-data-insert-into-db/#findComment-1199701 Share on other sites More sharing options...
Jnerocorp Posted April 10, 2011 Share Posted April 10, 2011 No problem gotta love those simple little mistakes (I hate when i make spelling errors in odd places that are hard to find lol) Quote Link to comment https://forums.phpfreaks.com/topic/233281-exploded-data-insert-into-db/#findComment-1199706 Share on other sites More sharing options...
3raser Posted April 10, 2011 Author Share Posted April 10, 2011 No problem gotta love those simple little mistakes (I hate when i make spelling errors in odd places that are hard to find lol) I always mess up mysql_query somehow. Usually I accidentally type mysql_insert, or mysql_qery. Quote Link to comment https://forums.phpfreaks.com/topic/233281-exploded-data-insert-into-db/#findComment-1199717 Share on other sites More sharing options...
Jnerocorp Posted April 10, 2011 Share Posted April 10, 2011 I had a tendancy to do the same stuff for a long time I always had to visit tizag.com for a reference on making sure my syntax was all correct. it just takes a long time of practice and the more you try the more it just sticks. Quote Link to comment https://forums.phpfreaks.com/topic/233281-exploded-data-insert-into-db/#findComment-1199722 Share on other sites More sharing options...
Pikachu2000 Posted April 10, 2011 Share Posted April 10, 2011 You can avoid running the query in the loop as below. I've added some comments in the code to explain. $_POST['names'] = preg_replace('~[\s]{2,}~', ' ', $_POST['names']); // Replace multiple spaces with one space to avoid empty records $chicken_names = mysql_real_escape_string($_POST['names']); $chicken_names_exp = explode(' ', $chicken_names); $query = "INSERT INTO names VALUES "; // Start building query string $records = array(); //initialize an empty array to hold the records foreach($chicken_names_exp as $value) { $records[] = "(null, '$value', '{$_POST['type']}')"; // build an array of values to insert } $query .= implode( ', ', $records ); // implode the array of values with a comma, and concatenate to query string if( $result = mysql_query($query) ) { echo "Success! You have successfully sent in " . mysql_affected_rows() . " chicken names! <a href='index.php'>Home</a>"; } Quote Link to comment https://forums.phpfreaks.com/topic/233281-exploded-data-insert-into-db/#findComment-1199758 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.