在单柱如何店三个单选按钮列出MySQL数据库,不同行?(How do store 3 radio b

2019-10-17 14:10发布

这是我的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数据库 - >

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? 请帮助的感谢!

Answer 1:

你的$rating是一个数组

你应该STOR这样的值$rating[0]$rating[1]$rating[2]所以将它们存储像u可以在PHP至极按钮被选中进行管理或点击,然后将它们存储在你的表



Answer 2:

把它们使用破灭逗号分隔的方式:

$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寻找到该列同时过滤数据。



Answer 3:

尝试把它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);
}

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

更新:假设$项包含的产品阵列。 (实施例阵列(“衬衣”,“裤”,“夹克”),并将其映射到单选按钮的值。

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

我们可以创建一个名为$ 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);
}


文章来源: How do store 3 radio button lists in mysql database in single column, different rows?