Jump to content

check box help


irtrsh

Recommended Posts

Hi I am new to this forum and also new in PHP/MySQL world. I just stuck in taking the check boxes value to MySQL table. I have got a form, there is 3 check box to get the input from the user and stored them to the database. but when I select the values (check box) from the form, it takes all the values and put it in the same column of the table instead of putting them separately. suppose I've the the check box called police, firstname and lastname and when I select all of them it should put the values in the table in police, firstName & lastname column respectively. here is the code....

 

<html>
<head>
</head>
<body>
<form action="rezach1.php" method="post">
<p>
  please vote for that...<br />
  <input type="checkbox" name="form[]"
value=999 />police<br />
  <input type="checkbox" name="form[]"
value="Name" />firstname<br />
  <input type="checkbox" name="form[]"
value="family" />lastname<br />
</p>
<input type="submit" name="formSubmit" value="Submit" />
</form>


</body>
</html>

 

and this is my php code 


<?php

# DATABASE configuration
#########################################
$hostname = "foot.doc.ac.uk"; //server
$db_user = "rafor"; // change to your database password
$db_password = "gher"; // change to your database password
$database = "rafor"; // provide your database name
$db_table = "rez"; // leave this as is

# THIS CODE IS USED TO CONNECT TO THE MYSQL DATABASE
######################################

$db = mysql_connect($hostname, $db_user, $db_password);
mysql_select_db($database,$db);
?>

<html>
<head>
<title> Insert Data Into MySQL </title>
</head>
<body>
<?php
if($_POST['formSubmit'] != "") {
   $val = $_POST['form']; 
  if(is_null($val)) {
    echo("<p>You didn't select any thing.</p>\n");
  } else {
   // echo("<p>You selected door(s): ");
    for($i=0; $i < count($val); $i++)
    {
      echo '<br />';
    echo($val[$i] . " ");

  $val_array = $_POST['form'];  
  foreach ($val_array as $form) {
  $source .= $form.", ";  

  }  
$val = substr($source, 0, -2); 
  
for ($j=0;$j<count($val_array);$j++)
{
$num=$val_array[$j];
echo '<br />';
print ("$num ");

$sql = "INSERT INTO $db_table(ID, Name, Status) values ('".mysql_real_escape_string(stripslashes($val))."','".mysql_real_escape_string(stripslashes($val))."','".mysql_real_escape_string(stripslashes($val))."')";
}
        if($result = mysql_query($sql ,$db)) {
echo '<h1>Thank you</h1>Your information has been entered into our database<br><br>';
} else {
        echo "ERROR: ".mysql_error();
        }
    }
    echo("</p>");
  }
    exit();
  }
?>

can anyone please help me to sort this out and tell me where is the problem?

thank you

 

irtrsh 

Link to comment
Share on other sites

It's not in PHP in the html code in the first code box.  That is strictly HTML.  Unless they just meant to leave out the opening and closing php tags???

 

You're wrong, when each of those HTML form fields called form[] are passed into PHP, it will see them as items in an array.

 

Create the following file form_test.php

<?php

echo '<pre>';
print_r($_GET['form']);
echo '</pre>';

?>

 

Now call it with a string like this: form_test.php?form[]=string1&form[]=string2&form[]=string3

 

This is exactly what the string would like like if you were using the GET method in an HTML form.  Something tells me you'll be surprised by the output.

 

Regards

Rich

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.