phorcon3 Posted January 23, 2008 Share Posted January 23, 2008 html output: <form method="post" action="test.php"> <input type="hidden" name="do" value="1"> <input type="hidden" name="title[]" value="title1"> <input type="hidden" name="text[]" value="text1"> ..code.. <input type="hidden" name="title[]" value="title2"> <input type="hidden" name="text[]" value="text2"> ..code.. <input type="submit" value="Submit"> </form> now I dun know how to insert it into my MySQL Databank. I tried: <?php if(isset($_POST['do'])) { foreach($_POST['title'] as $title) { foreach($_POST['text'] as $text) { mysql_query("INSERT INTO `table` (`title`, `text`) VALUES ('$title', '$text')"); } } } ?> but that of course didn't work. does anyone have any ideas/suggestion on how I can solve this problem? I'd appreciate it. Link to comment https://forums.phpfreaks.com/topic/87432-solved-mysql-insert/ Share on other sites More sharing options...
p2grace Posted January 23, 2008 Share Posted January 23, 2008 Try this: <? if(isset($_POST['title'])){ $title = $_POST['title']; $text = $_POST['text']; for($i=0;$i<count($title);$i++){ $ins_title = trim(mysql_real_escape_string($title[$i])); $ins_text = trim(mysql_real_escape_string($text[$i])); mysql_query("INSERT INTO `table` (`title`, `text`) VALUES ('$ins_title', '$ins_text')"); } } ?> Link to comment https://forums.phpfreaks.com/topic/87432-solved-mysql-insert/#findComment-447183 Share on other sites More sharing options...
phorcon3 Posted January 23, 2008 Author Share Posted January 23, 2008 it worked!! thanks a bunch!! I used this instead: <?php if(isset($_POST['do'])) { for($i = 0; $i < $_POST['count']; $i++) { $title = $_POST['title_'.$i]; $text = $_POST['text_'.$i]; mysql_query("INSERT INTO `table` (`title`, `text`) VALUES ('$title', '$text')"); } } ?> <form method="post" action="test.php"> <input type="hidden" name="do" value="1"> <input type="hidden" name="count" value="2"> <input type="hidden" name="title_1" value="title1"> <input type="hidden" name="text_1" value="text1"> ..code.. <input type="hidden" name="title_2" value="title2"> <input type="hidden" name="text_2" value="text2"> ..code.. <input type="submit" value="Submit"> </form> but yours looks a helluva lot better lol ..plus it works with the html code I had before ..thanks! Link to comment https://forums.phpfreaks.com/topic/87432-solved-mysql-insert/#findComment-447190 Share on other sites More sharing options...
p2grace Posted January 23, 2008 Share Posted January 23, 2008 Glad I could help Link to comment https://forums.phpfreaks.com/topic/87432-solved-mysql-insert/#findComment-447193 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.