Jump to content

for each checkbox ticked have them INSERT INTO database table


jarv

Recommended Posts

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);
?> 

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.

 

 

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.