【问题标题】:How do store 3 radio button lists in mysql database in single column, different rows?如何将 3 个单选按钮列表存储在 mysql 数据库中的单列、不同行中?
【发布时间】:2012-12-23 05:42:15
【问题描述】:

这是我的 rating.php(html 代码)

<input type="radio" name="selectThree" value="1">
<input type="radio" name="selectThree" value="2">
<input type="radio" name="selectThree" value="3">
<input type="radio" name="selectThree" value="4">
<input type="radio" name="selectThree" value="5">

<input type="radio" name="selectTwo" value="1">
<input type="radio" name="selectTwo" value="2">
<input type="radio" name="selectTwo" value="3">
<input type="radio" name="selectTwo" value="4">
<input type="radio" name="selectTwo" value="5">

<input type="radio" name="selectOne" value="1">
<input type="radio" name="selectOne" value="2">
<input type="radio" name="selectOne" value="3">
<input type="radio" name="selectOne" value="4">
<input type="radio" name="selectOne" value="5">

所以当用户选择该值时,它会在此处生成以下代码插入数据库:

<?php
include_once "mysqli.connect.php";
include_once "config.php";

if(isset($_POST['Click']))      
{

$rating = explode($_POST['selectOne'], $_POST['selectTwo'], $_POST['selectThree']);
$_SESSION['commentInput'] = array();
$_SESSION['commentInput'][] = $_POST['comment'][0];
$_SESSION['commentInput'][] = $_POST['comment'][1];
$_SESSION['commentInput'][] = $_POST['comment'][2];

if(isset($_REQUEST["comment"]))
{

$merge = array_combine ($_SESSION['product'],$_SESSION['commentInput']);
foreach($merge as $key => $value)
{

$sqlComment = "INSERT into comment (comment, product) VALUES ('".$value."', '".$key."')";
$result = $mysqli->query($sqlComment);
}

echo"<script type='text/javascript'>alert('Thank you for your comment!' )</script>";
}

else
{

echo "<script type='text/javascript'>alert('Please comment!')</script>";    
}   

}

我想这样存储在mysql数据库中 ->

product|rating
--------------
shirt  | 2
pants  | 3
dress  | 5

但现在它是这样存储的:

product|rating
--------------
shirt  | Array
pants  | Array
dress  | Array

在我使用这个之后 ->

$rating = explode($_POST['selectOne'], $_POST['selectTwo'], $_POST['selectThree']);

//mysql
$sqlRating = "INSERT into ratings (product, rating) VALUES ('".$key."', '".$rating."')";
$result = $mysqli->query($sqlRating);

如何将值存储到 mysql 中?请帮忙谢谢!

【问题讨论】:

  • 单选按钮的名称是什么?
  • @ianace selectOne, selectTwo, selectThree 我有 3 个单选按钮列表
  • 是在foreach循环下生成插入查询吗?
  • @ArunKillu 我的关键是将产品存储到单列中,正确输出的不同行。至于爆炸,我正在尝试我的单选按钮。
  • @artsylar 是的,我需要那个 foreach 循环。

标签: php mysql database radiobuttonlist


【解决方案1】:

你的$rating 是一个数组

您应该存储像 $rating[0]$rating[1]$rating[2] 这样的值,以便像这样存储它们,您可以在 php 中管理它们,选择或单击按钮然后将它们存储在您的表中

【讨论】:

    【解决方案2】:

    尝试将它放在类似这样的 foreach 循环中。

    // create an array here that contains the key and the rating key=>rating and save it to $ratings array.
    
    foreach($ratings as $key=>$rating){
    
       //mysql
       $sqlRating = "INSERT into ratings (product, rating) VALUES ('".$key."', '".$rating."')";
       $result = $mysqli->query($sqlRating);
    }
    

    顺便说一下,这里是发布单选按钮值http://www.homeandlearn.co.uk/php/php4p10.html的教程

    更新: 假设 $key 包含产品数组。 (例如 array('shirt', 'pants', 'jacket') 并且它被映射到单选按钮的值。

    'shirt' => $_POST['selectOne'];
    'pants' => $_POST['selectTwo'];
    'jacket' => $_POST['selectThree'];
    

    我们可以为 $key 和 $ratings 创建一个名为 $prod_ratings 的数组

    $ratings = array($_POST['selectOne'], $_POST['selectTwo'], $_POST['selectThree']);
    $prod_ratings = array_combine($key, $ratings);
    

    然后通过以下方式将数组的值插入到数据库中:

    foreach($prod_ratings as $key=>$rating){
    
       //mysql
       $sqlRating = "INSERT into ratings (product, rating) VALUES ('".$key."', '".$rating."')";
       $result = $mysqli->query($sqlRating);
    }
    

    【讨论】:

    • $key=>$rating 你必须使用
    • 它显示了这个错误 -> 为 foreach() 提供的参数无效
    • 您创建了 $ratings 数组吗?请发布您的最新代码,以便我们为您提供帮助。
    • 你在哪里得到你帖子中 $key 的值?
    • @artsylar $key -> 产品(来自另一个 php 页面,因此我使用 session 来存储。)
    【解决方案3】:

    使用 implode 以逗号分隔的方式放置它们:

    $rating = explode($_POST['selectOne'], $_POST['selectTwo'], $_POST['selectThree']);
    
    //mysql
    $sqlRating = "INSERT into ratings (product, rating) VALUES ('".$key."', '".implode(",",$rating)."')";
    $result = $mysqli->query($sqlRating);
    

    稍后您应该能够在过滤数据时使用 find_in_set 查看该列。

    【讨论】:

    • 嗨,它将所有相同的值存储到数据库中。
    猜你喜欢
    • 2012-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-24
    • 2019-12-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多