doushao8421 2014-09-24 10:45
浏览 114
已采纳

从复选框到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!";
  • 写回答

5条回答 默认 最新

  • dsa80833 2014-09-24 10:50
    关注

    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')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?