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

标签 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数据库中 ->

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 中?请帮忙谢谢!

最佳答案

你的$rating是一个数组

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

关于php - 如何在单列、不同行的 mysql 数据库中存储 3 个单选按钮列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14230770/

相关文章:

php - 从 iOS 应用程序安全地访问数据库

java - 测量与数据库相关的网站的运行时间

.net - 制作百万美元主页动态图像网格的最佳方法?

php - 将 .php 重写为 .html 并将 .php 重定向到 .htaccess 中的 404

php - 将字符串插入给定索引的字符串中

mysql - 获取与列表(数组)中的精确值匹配的记录

node.js - MongoDB 的 db.collection.distinct() 对于每个用户的效率与保存为数据库条目并检索结果的效率

php - SimpleXML:添加 "xmlns:wp"属性仅添加 "wp",为什么?

PHP Web服务需要文件错误

MySQL 左连接在没有数据的情况下填充值