如何在单列,不同行的mysql数据库中存储3个单选按钮列表?

时间:2013-01-09 08:09:56

标签: php mysql database radiobuttonlist

这是我的ratings.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数据库中存储这样的内容 - &GT;

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

但现在它的存储方式如下:

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

我用过之后 - &gt;

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

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

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

3 个答案:

答案 0 :(得分:1)

您的$rating是一个数组

你应该存储像$rating[0]$rating[1]$rating[2]那样的值,所以要存储它们就像你可以在php中管理它们一样,选择或点击按钮然后将它们存储在你的表

答案 1 :(得分:0)

尝试把它放在像这样的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包含产品数组。 (例如阵列('衬衫','裤子','夹克'),它被映射到单选按钮的值。

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

答案 2 :(得分:0)

使用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在过滤数据时查看该列。