问题
This is my ratings.php (html codes)
<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">
So when users select the value, it will generate the below codes here to insert into database:
<?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>";
}
}
I want to store like this in mysql database ->
product|rating
--------------
shirt | 2
pants | 3
dress | 5
But now it stores like this:
product|rating
--------------
shirt | Array
pants | Array
dress | Array
after I used this ->
$rating = explode($_POST['selectOne'], $_POST['selectTwo'], $_POST['selectThree']);
//mysql
$sqlRating = "INSERT into ratings (product, rating) VALUES ('".$key."', '".$rating."')";
$result = $mysqli->query($sqlRating);
How do I make the values store into mysql? Please help thanks!
回答1:
your $rating
is an array
you should stor values like that $rating[0]
or $rating[1]
or $rating[2]
, so to store them like that u can manage them in php wich button is selected or clicked then store them in your table
回答2:
Put them in a comma-separated way using implode:
$rating = explode($_POST['selectOne'], $_POST['selectTwo'], $_POST['selectThree']);
//mysql
$sqlRating = "INSERT into ratings (product, rating) VALUES ('".$key."', '".implode(",",$rating)."')";
$result = $mysqli->query($sqlRating);
Later on you should be able to use find_in_set to look into that column while filtering data.
回答3:
try putting it inside a foreach loop something like this.
// 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);
}
by the way, here's tutorial on posting radion buttons values http://www.homeandlearn.co.uk/php/php4p10.html
Update: Assuming that $key contains array of products. (ex. array('shirt', 'pants', 'jacket') and it is mapped to the values of the radio buttons.
'shirt' => $_POST['selectOne'];
'pants' => $_POST['selectTwo'];
'jacket' => $_POST['selectThree'];
We can create an array called $prod_ratings for $key and $ratings
$ratings = array($_POST['selectOne'], $_POST['selectTwo'], $_POST['selectThree']);
$prod_ratings = array_combine($key, $ratings);
Then insert the values of the array to the database by:
foreach($prod_ratings as $key=>$rating){
//mysql
$sqlRating = "INSERT into ratings (product, rating) VALUES ('".$key."', '".$rating."')";
$result = $mysqli->query($sqlRating);
}
来源:https://stackoverflow.com/questions/14230770/how-do-store-3-radio-button-lists-in-mysql-database-in-single-column-different