我正在尝试将Rate Yo添加到数据库中。但是我不能总是得到
的错误未定义索引:C上的评级:\ xampp \ htdocs \ bill \ bill \ add_rate.php 第57行
我尝试了下面所附的代码。
表格
<form action="add_rate.php" method="post">
<div class="rateyo" id= "rating"
data-rateyo-rating="4"
data-rateyo-num-stars="5"
data-rateyo-score="3"></div>
<span class='score'>0</span>
<span class='result'>0</span>
</div>
<div><input type="submit" name="add"> </div>
</form>
jQuery
$(function () {
$(".rateyo").rateYo().on("rateyo.change", function (e, data) {
var rating = data.rating;
$(this).parent().find('.score').text('score :'+
$(this).attr('data-rateyo-score'));
$(this).parent().find('.result').text('rating :'+ rating);
});
});
php代码
require 'db_connection.php';
if ($_SERVER["REQUEST_METHOD"] == "POST")
{
$rating = $_POST["rating"];
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO ratee (rate) VALUES ('$rating')";
if (mysqli_query($conn, $sql))
{
echo "New Rate addedddd successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
答案 0 :(得分:1)
如下所示更改表单代码和jquery代码:
<form action="add_rate.php" method="post">
<div class="rateyo" id= "rating"
data-rateyo-rating="4"
data-rateyo-num-stars="5"
data-rateyo-score="3"></div>
<span class='score'>0</span>
<span class='result'>0</span>
<input type="text" name="rating">
</div>
<div><input type="submit" name="add"> </div> <!-- added hidden field with name rating
</form>
jquery代码:-
$(function () {
$(".rateyo").rateYo().on("rateyo.change", function (e, data) {
var rating = data.rating;
$(this).parent().find('.score').text('score :'+ $(this).attr('data-rateyo-score'));
$(this).parent().find('.result').text('rating :'+ rating);
$(this).parent().find('input[name=rating]').val(rating); //add rating value to input field
});
});
稍微更改php代码:-
if (!empty($_POST["rating"]))
{
//rest of your code
}else{
die('ratings required');
}
注意:-您的php代码已经可以进行SQL注入,因此请使用prepared statements
来阻止它。