从复选框到MySQL的多个值

问题描述:

i have an html page, where there is a form and saves the results into MySQL. The problem is that the checkbox, saves only one value on the MySQL table. What do i have to do, in order to save multiple values inside the db column?

HTML CODE:

<fieldset>
<input type = "checkbox" name = "rating[]" value = "Homepage">Homepage
<input type = "checkbox" name = "rating[]" value = "Facilities"> Facilities 
<input type = "checkbox" name = "rating[]" value = "Reservation"> Reservation 
<input type = "checkbox" name = "rating[]" value = "Contact"> Contact  
<input type = "checkbox" name = "rating[]" value = "current"> The current one  
</fieldset>

PHP CODE:

$con=mysqli_connect("xxx","zzz","yyy","xxx");

if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
 }

$firstname = mysqli_real_escape_string($con, $_POST['firstname']);
$lastname = mysqli_real_escape_string($con, $_POST['lastname']);
$password = mysqli_real_escape_string($con, $_POST['password']);
$email = mysqli_real_escape_string($con, $_POST['email']);
$address = mysqli_real_escape_string($con, $_POST['address']);
$postcode = mysqli_real_escape_string($con, $_POST['postcode']);
$country = mysqli_real_escape_string($con, $_POST['country']);
$phonenumber = mysqli_real_escape_string($con, $_POST['phonenumber']);
$rating = mysqli_real_escape_string($con, $_POST['rating_value']);
$subscribe = mysqli_real_escape_string($con, $_POST['subscribe']);


for($i=0; $i<count($rating); $i++) { $rating_value &= $rating[$i];}

$sql="INSERT INTO customers (firstname, lastname, password, email, address, postcode,     country, phonenumber, rating, subscribe)
VALUES ('$firstname', '$lastname', '$password', '$email', '$address', '$postcode','$country', '$phonenumber', '$rating_value', '$subscribe')";


if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo " Success!";

User Query inside the for loop.... If you want to save each seperate recode for each value...

 for($i=0; $i<count($rating); $i++) { 

$rating_value = $rating[$i];
$sql="INSERT INTO customers (firstname, lastname, password, email, address, postcode,     country, phonenumber, rating, subscribe)
VALUES ('$firstname', '$lastname', '$password', '$email', '$address', '$postcode','$country', '$phonenumber', '$rating_value', '$subscribe')";

}

OR Same value in comma seperated in one column use below code..

foreach ($_POST['rating'] as $rateingvalue)
{  
  $rating_value .= $rateingvalue.",";
}

$sql="INSERT INTO customers (firstname, lastname, password, email, address, postcode,     country, phonenumber, rating, subscribe)
VALUES ('$firstname', '$lastname', '$password', '$email', '$address', '$postcode','$country', '$phonenumber', '$rating_value', '$subscribe')";

You could add the rating_value into the database as comma-separated values ... ie. "item1,item2,item3,...".

Other option is to use if statements... ie.

if($rating[x]){ //Not sure if this syntax is correct, but $rating[x] refers to the name of the rating checkbox which was checked.
   $sql =+ "rating_x"
}

or something of that nature - you would need to have the correct columns in the database if you weren't storing the values in a comma-delimited string.

   <fieldset>
    <input type = "checkbox" name = "rating[0]" value = "Homepage">Homepage
    <input type = "checkbox" name = "rating[1]" value = "Facilities"> Facilities 
    <input type = "checkbox" name = "rating[2]" value = "Reservation"> Reservation 
    <input type = "checkbox" name = "rating[3]" value = "Contact"> Contact  
    <input type = "checkbox" name = "rating[4]" value = "current"> The current one  
    </fieldset>

Something like this:

$ratings  = NULL;
$post_rat = $_POST['rating'];

foreach($post_rat as $v){
    if(next($post_rat) $ratings .= $v.'|';
    else $ratings .= $v
}

@user3762527

These keys are not not necessary. This same indexs in server-side $_POST['rating'][0]

You can store all ratings checked by user in COMMA SEPERATED FORMAT, these value can easily be retrieved from database wherever needed.

Simply explode the value.

Example :

    $ratingsArray=$_POST['rating'];

    $ratingsSelected=implode(',' , $ratingsArray);          /*ALL SELECTED RATING VALUES FROM ARRAY 
                                                                      CONVERT IN COMMA SEPERATED VALUE STRING*/

Now the string contains all the selected ratings values seperated by comma. You can simply store the string value in database.

Whenever you need the value back in the form of array.

Just use:

    $ratingsString=$row['rating'];                          //COMMA SEPERATED VALUE STRING FROM DATABASE                                                  

    $ratingsArray=explode(',' , $ratingsString);

Now the values are again in the form of Array , use as per your requirements.