jarv Posted July 12, 2008 Share Posted July 12, 2008 ok so I can tick one keyword check box and it adds it into the keyword table, what if i need to check several keywords and have them go into a new line in the keyword table is there a loop called FOR EACH for this? the FOREACH I used below does not work?! <?php include_once('config.php'); $result = mysql_query("SELECT MAX(HeaderID) AS HeaderCount FROM Headertbl"); while($row = mysql_fetch_array($result)){ $HeaderID = $row['HeaderCount']; } $keyword_id = mysql_real_escape_string(stripslashes($_POST['keyword_id'])); foreach ($keyword_id as $keyword_id) { $sql1 = "INSERT INTO keyword (keyword_id,HeaderID) Values ('$keyword_id','$HeaderID')"; $result = mysql_query($sql1,$link) or die('Error: ' . mysql_error() . '<br>SQL: ' . $sql1); } header("Location: display.php"); mysql_close($link); ?> Link to comment https://forums.phpfreaks.com/topic/114401-for-each-checkbox-ticked-have-them-insert-into-database-table/ Share on other sites More sharing options...
ratcateme Posted July 12, 2008 Share Posted July 12, 2008 this would help if you posted your html form with the checkbox's in it but anyway what i think you are doing in your html is making keyword_id an array like this <input type="checkbox" name="keyword_id[]" /> and trying to stripslashes would fail because you are inputing an array not a string i think the mysql_real_escape_string() will also fail for the same reason. you could do this check in your foreach statement also regarding that i don't think foreach ($keyword_id as $keyword_id) { will work because the to vars are the same. if anyone has further info on this i cant test it my server is broken Scott. Link to comment https://forums.phpfreaks.com/topic/114401-for-each-checkbox-ticked-have-them-insert-into-database-table/#findComment-588300 Share on other sites More sharing options...
jarv Posted July 12, 2008 Author Share Posted July 12, 2008 can anyone please help? Link to comment https://forums.phpfreaks.com/topic/114401-for-each-checkbox-ticked-have-them-insert-into-database-table/#findComment-588442 Share on other sites More sharing options...
Barand Posted July 12, 2008 Share Posted July 12, 2008 can anyone please help? It looks like someone has to me Link to comment https://forums.phpfreaks.com/topic/114401-for-each-checkbox-ticked-have-them-insert-into-database-table/#findComment-588489 Share on other sites More sharing options...
ratcateme Posted July 12, 2008 Share Posted July 12, 2008 can you post your html form code at the moment i have no idea of the input that goes into the php you posted Scott. Link to comment https://forums.phpfreaks.com/topic/114401-for-each-checkbox-ticked-have-them-insert-into-database-table/#findComment-588602 Share on other sites More sharing options...
Barand Posted July 12, 2008 Share Posted July 12, 2008 you could do this check in your foreach statement also regarding that i don't think foreach ($keyword_id as $keyword_id) { will work because the to vars are the same. if anyone has further info on this i cant test it my server is broken I have to admit that I expected this code to go up in smoke, but it does actually work ??? <?php $a = array (1,2,3); foreach ($a as $a) echo "$a "; ?> --> 1 2 3 Link to comment https://forums.phpfreaks.com/topic/114401-for-each-checkbox-ticked-have-them-insert-into-database-table/#findComment-588608 Share on other sites More sharing options...
ratcateme Posted July 12, 2008 Share Posted July 12, 2008 cool knowing that i might be able to make code that makes more sense. with my foreach loops. Scott. Link to comment https://forums.phpfreaks.com/topic/114401-for-each-checkbox-ticked-have-them-insert-into-database-table/#findComment-588612 Share on other sites More sharing options...
Barand Posted July 12, 2008 Share Posted July 12, 2008 However, you can't use the array again afterwards as it has been destroyed If you try echo '<pre> the array ', print_r($a, true), '</pre>'; after the above code. all you get is 3 The array has gone, you are just left with the scalar variable $a Link to comment https://forums.phpfreaks.com/topic/114401-for-each-checkbox-ticked-have-them-insert-into-database-table/#findComment-588613 Share on other sites More sharing options...
ratcateme Posted July 12, 2008 Share Posted July 12, 2008 yea through that might have happened. o well still good to know. Scott. Link to comment https://forums.phpfreaks.com/topic/114401-for-each-checkbox-ticked-have-them-insert-into-database-table/#findComment-588619 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.