【问题标题】:Multiple values into MySQL from checkbox多个值从复选框进入 MySQL
【发布时间】:2014-11-18 19:55:00
【问题描述】:

我有一个 html 页面,其中有一个表单并将结果保存到 MySQL。问题是该复选框仅在 MySQL 表上保存一个值。为了在 db 列中保存多个值,我该怎么做?

HTML 代码:

<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 代码:

$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!";

【问题讨论】:

  • $rating = mysqli_real_escape_string($con, $_POST['rating_value']); 是否将数组转换为字符串?

标签: php mysql arrays checkbox insert


【解决方案1】:

您可以将用户检查的所有评分存储在 COMMA SEPERATED FORMAT 中, 这些值可以在任何需要的地方轻松地从数据库中检索。

简单地爆炸价值。

例子:

    $ratingsArray=$_POST['rating'];

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

现在字符串包含所有以逗号分隔的选定评级值。 您可以简单地将字符串值存储在数据库中。

当您需要以数组形式返回值时。

只需使用:

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

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

现在这些值再次以 Array 的形式出现,根据您的要求使用。

【讨论】:

    【解决方案2】:

    for 循环内的用户查询....如果您想为每个值保存每个单独的重新编码...

     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')";
    
    }
    

    或在下面的代码中使用逗号分隔的相同值..

    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')";
    

    【讨论】:

    • @MixalisPapoulakis 我已经更新了第二部分......使用 foreach 而不是 for 循环......并且首先检查你的值是否正确并且你选择了任何复选框......
    • 我收到以下错误:警告:为 foreach() 提供的参数无效
    • @MixalisPapoulakis 在代码中使用了它,其次,您创建的变量存在一些问题.. 可能是您选择的值没有出现在表单的下一页中.. 首先检查... .
    • 您确定语法正确吗?你写了 ratingvalue(没有 sapce),它是 rating 和 rating_value。
    • 是的,它是正确的@MixalisPapoulakis,我今天使用了相同的场景,它适用于那个场景...... $rating 是你在上面创建的变量......我使用了那个变量......
    【解决方案3】:

    您可以将 rating_value 作为逗号分隔值添加到数据库中......即。 “项目 1,项目 2,项目 3,...”。

    其他选项是使用 if 语句...即。

    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"
    }
    

    或类似性质的东西 - 如果您不将值存储在逗号分隔的字符串中,则需要在数据库中拥有正确的列。

    【讨论】:

      【解决方案4】:
         <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>
      

      【讨论】:

        【解决方案5】:

        类似这样的:

        $ratings  = NULL;
        $post_rat = $_POST['rating'];
        
        foreach($post_rat as $v){
            if(next($post_rat) $ratings .= $v.'|';
            else $ratings .= $v
        }
        

        @user3762527

        这些键不​​是必需的。服务器端 $_POST['rating'][0]

        中的相同索引

        【讨论】:

          猜你喜欢
          • 2012-08-20
          • 2015-07-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-02-19
          • 2020-11-07
          • 1970-01-01
          相关资源
          最近更新 更多