iwpg Posted April 19, 2008 Share Posted April 19, 2008 Hi guys, I have been pulling my hair out trying to find a good example on this. ??? I know that arrays play the main theme in the function, and I am an array flunkie. I have a textarea, where I am inserting 1 item per line and I am trying to get it to upload to my mysql database. This is kind of like the Google Adwords "add keywords" function. I also need to be able to remove selected values once they're in the db using a checkbox. I just have no way of handling the submitted data, and inserting it. Any help is GREATLY appreciated. Thanks, Mike Link to comment https://forums.phpfreaks.com/topic/101823-solved-insert-multiple-rows-using-a-textarea-with-linebreaks/ Share on other sites More sharing options...
discomatt Posted April 19, 2008 Share Posted April 19, 2008 Try this: <?php //Split data $data = preg_split('/\r\n|\n/', $_POST['textarea'], -1, PREG_SPLIT_NO_EMPTY); //Iterate through array foreach ($data as $val) { echo $val . '<br>'; } ?> Link to comment https://forums.phpfreaks.com/topic/101823-solved-insert-multiple-rows-using-a-textarea-with-linebreaks/#findComment-521061 Share on other sites More sharing options...
iwpg Posted April 19, 2008 Author Share Posted April 19, 2008 Thanks for the much needed help. The insert new records function is working perfectly. Many thanks... Now, I am having trouble removing multiple records by using a checkbox. This is what I have: $id = array(); $id = $_POST['kwcb']; if (count($id) > 0) { foreach ($id as $removeid) { mysql_query("delete from table where kw='$removeid'"); } } Thanks again. Mike Link to comment https://forums.phpfreaks.com/topic/101823-solved-insert-multiple-rows-using-a-textarea-with-linebreaks/#findComment-521101 Share on other sites More sharing options...
iwpg Posted April 19, 2008 Author Share Posted April 19, 2008 Sorry, I fogot to mention that the above outputs: Invalid argument supplied for foreach() Thanks again - mike Link to comment https://forums.phpfreaks.com/topic/101823-solved-insert-multiple-rows-using-a-textarea-with-linebreaks/#findComment-521108 Share on other sites More sharing options...
Fadion Posted April 19, 2008 Share Posted April 19, 2008 foreach() needs an array as the first argument and yours is a string. If u want to pass all or at least one post value to your array: <?php $id[] = $_POST['kwcb']; ?> or <?php foreach($_POST as $val){ $id[] = $val; } ?> Link to comment https://forums.phpfreaks.com/topic/101823-solved-insert-multiple-rows-using-a-textarea-with-linebreaks/#findComment-521114 Share on other sites More sharing options...
iwpg Posted April 19, 2008 Author Share Posted April 19, 2008 Thanks, I got rid of the error, but only 1 result of the multiple checkboxes will delete. It doesn't loop through the rest of them. This is my code: elseif ($action=='postdelete'){ $id = array(); $id[] = $_POST['kwcb']; if (count($id) > 0) { foreach ($id as $removeid) { mysql_query("delete from table where kw='$removeid'"); } } } Link to comment https://forums.phpfreaks.com/topic/101823-solved-insert-multiple-rows-using-a-textarea-with-linebreaks/#findComment-521125 Share on other sites More sharing options...
Fadion Posted April 19, 2008 Share Posted April 19, 2008 If i get this right, u need to see which checkbox is checked and delete the related data. Then u need to have checkboxes with names like: <input type="checkbox" name="checkbox[]" value="1" /> <input type="checkbox" name="checkbox[]" value="2" /> Note the square paranthesis in the checkbox name, which makes them an array. To get the values: <?php $id = array(); $id = $_POST['checkbox']; //u pass all the array data of the checkboxes to $id foreach($id as $removeid){ $mysql_query("DELETE FROM table WHERE kw='$removeid'"); } ?> Hope its what u need. Link to comment https://forums.phpfreaks.com/topic/101823-solved-insert-multiple-rows-using-a-textarea-with-linebreaks/#findComment-521245 Share on other sites More sharing options...
iwpg Posted April 20, 2008 Author Share Posted April 20, 2008 Just what I needed. I really do appreciate you helping me out on this. Many thanks. Mike Link to comment https://forums.phpfreaks.com/topic/101823-solved-insert-multiple-rows-using-a-textarea-with-linebreaks/#findComment-521663 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.