Insert multiple selections checkbox form one row php mysql
Hello All,
The script below works fine but when I input a record it goes to two separate rows in the database table, how could i modify the code so it only goes to one row ? Thank you in advance
HTML Code:
<form action="test_post.php" method="post">
<input type="checkbox" name="tv[]" value="a">a<br />
<input type="checkbox" name="tv[]" value="b">b<br />
<input type="checkbox" name="tv[]" value="c">c<br />
<input type="checkbox" name="tv[]" value="d">d<br />
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?php
$sDbHost = '';
$sDbName = '';
$sDbUser = '';
$sDbPwd = '';
$dbConn = mysql_connect ($sDbHost, $sDbUser, $sDbPwd) or die ('MySQL connect failed. ' . mysql_error());
mysql_select_db($sDbName,$dbConn) or die('Cannot select database. ' . mysql_error());
session_start();
$checkbox1 = $_POST['tv'];
if($_POST["Submit"]=="Submit")
{
for ($i=0; $i<sizeof($checkbox1);$i++) {
$query="INSERT INTO equipment (tv) VALUES ('".$checkbox1[$i]."')";
mysql_query($query) or die ('Error updating database');
echo "Record is inserted.";
}
}
?>
Inserting multiple options from a dropdown menu php and mysql
Hello DyDr and Styxlawyer,
Limit didn't do anything :(
I see what you mean, I have this other script that insert records in one row under tv field, what do you think about this different approach.
Thank you very much for taking the time to help me and many other members of this forum
Code:
<form method="POST" action="formsubmit.php">
<p>(hold "Ctrl" key to select multiple)
<select size="5" name="tv[]" multiple="multiple">
<option value="None">None</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">4</option>
</select>
<input type="submit" value="Send" name="Send">
</form>
<?php
$tv = $_POST['tv']; // Retrieve POST data
if(isset($tv)){ // Check if selections were made
$serializedoptions = serialize($tv);
$con = mysql_connect("", "", "");
mysql_select_db('', $con);
$sql="INSERT INTO equipment (tv) VALUES ('$serializedoptions')";
mysql_query($sql) or die(mysql_error());
echo 'The following tv were saved to database:<br /><br />';
foreach($tv as $key => $value){
echo $value . "<br />";
}
Output:
a:2:{i:0;s:1:"1";i:1;s:1:"3";} //chosen options are 1 and 4 inside the double quotes