connection table for overcoming the checkbox problem
i have a php form with text box,radiobutton and checkboxes.I have connected it to the databse , the values are getting stored into the database except the checkbox values.I want to enter all the c开发者_运维技巧heckbox values into the database.I want an backend such that it links to two tables.the text box and the radio button values are to be stored in the first table and the id's of the selected checkbox values in the other table.
u can store only that value in database which is checked, but you can not store all value of check box with same name attribute, because by checking that checkbox, that value is proceed to next page via POST/GET
but if u want all value of check box ( multiple check box) then use name array like below
<form action="checkbox.php" method="post">
<input type="checkbox" name="checkbox[]" value="a">
<input type="checkbox" name="checkbox[]" value="b">
<input type="checkbox" name="checkbox[]" value="c">
<input type="checkbox" name="checkbox[]" value="d">
<br>
<br>
<input type="submit" name="Submit" value="Submit">
</form>
<?
/* and in your checkbox.php you do this: */
if(isset($_POST['Submit']))
{
for ($i=0; $i<count($_POST['checkbox']);$i++) {
echo "<br />value $i = ".$_POST['checkbox'][$i];
}
}
?>
a connection table can be created (i.e A single php page connection is given to two tables of the same database)the code ia as follows. this code should be given as backend to the php page.
$dbhost = "localhost:3306"; // usually is localhost, but if not sure, check with your hosting company, if you are with webune leave as localhost $dbuser = "root"; // change to your database password $dbpass = "mysql"; // change to your database password $dbname = "probe_config"; // provide your database name $db_table = "mapping"; // leave this as is
$conn = mysql_connect("$dbhost", "$dbuser", "$dbpass"); $select = mysql_select_db("$dbname"); //selecting the urls $selected = $_POST['urlSelect'];
if (count($selected) > 0) {
for ($i=0;$i<count($selected);$i++) {
echo "$selected[$i] <br />";
}
} $timeout=$_POST['timeout'] ; $wait=$_POST['wait']; $clearcache=$_POST['clearcache']; $name=$_POST['name']; $replication=$_POST['replication']; //inserting into the databse $query = "INSERT INTO webmeasurementsuite (wait, timeout, clearcache, name, replication) values ($wait, $timeout, '$clearcache', '$name', $replication)";
if (!mysql_query($query,$conn)) { die('Error: ' . mysql_error()); } else { echo "1 record added to WMS"; $query = "SELECT wms_id FROM webmeasurementsuite ORDER BY wms_id DESC LIMIT 1"; if (!($result=mysql_query($query,$conn))) { die('Error: ' . mysql_error()); } else { $row = mysql_fetch_assoc($result); $id=$row['wms_id']; $selected = $_POST['urlSelect'];
if (count($selected) > 0)
{
for ($i=0;$i<count($selected);$i++) {
$urlentry=$urlentry.", ";
if($i==0)
{
$urlentry="";
$j++;
}
$urlentry=$urlentry .$selected[$i];
}
}
echo $urlentry;
echo '<br />id='.$id;
//insert for the second table $query= "INSERT INTO mapping(wms_Id, wm_Id) values ($id, '$urlentry')"; if (!mysql_query($query,$conn)) { die('Error: ' . mysql_error()); } else { echo "Mapping Done"; } }
}
mysql_close($conn);
?>
精彩评论