您好我的表单包含评分,姓名,电子邮件和评论。我能够为名称,电子邮件和评论插入用户输入数据。但不知道如何将星级评级存储在数据库中。有人请帮助我。感谢
<?php
if(isset($_POST['submit']))
{
$name = $_POST['name'];
$email = $_POST['email'];
$comments = $_POST['comments'];
$ratings = $_POST['ratings'];
$link = mysqli_connect("localhost", "root", "", "imakr");
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
$res = mysqli_query($link, "insert into imakr.customer_review(name, email, comments, ratings) values('$name','$email','$comments', '$ratings')");
if($res)
{
echo "Your feedback is saved";
}
else
{
echo " OOPs!! there is some error. Please check the fields";
}
}
?>
<form id="customer_review" name="cust_rev"action="" method="post" onsubmit="return validate()">
<table width="535" border="0">
<tr>
<td>Rate This Product:
</td>
<td>
<span id="rateStatus">Rate Me...</span>
<span id="ratingSaved">Rating Saved!</span>
<div id="rateMe" title="Rate Me...">
<a onclick="rateIt(this)" id="_1" title="Poor" onmouseover="rating(this)" onmouseout="off(this)"><span class="ratings">1</span></a>
<a onclick="rateIt(this)" id="_2" title="Not Bad" onmouseover="rating(this)" onmouseout="off(this)"><span class="ratings">2</span></a>
<a onclick="rateIt(this)" id="_3" title="Pretty Good" onmouseover="rating(this)" onmouseout="off(this)"><span class="ratings">3</span></a>
<a onclick="rateIt(this)" id="_4" title="Excellent" onmouseover="rating(this)" onmouseout="off(this)"><span class="ratings">4</span></a>
<a onclick="rateIt(this)" id="_5" title="Marvellous" onmouseover="rating(this)" onmouseout="off(this)"><span class="ratings">5</span></a>
</div>
</td>
</tr>
<tr>
<td width="129"><span class="titles">Name</span><span class="star">*</span>:</td>
<td width="396"><label for="name"></label>
<input type="text" name="name" id="name" /></td>
</tr>
<tr>
<td><span class="titles">Email</span><span class="star">*</span>:</td>
<td><label for="email"></label>
<input type="text" name="email" id="email" /></td>
</tr>
<tr>
<td height="61">Comments:</td>
<td><label for="comments"></label>
<textarea name="comments" id="comments" cols="45" rows="5" onchange="maxlength('comments', 500)"></textarea></td>
</tr>
<tr>
<td> </td>
<td><input type="submit" name="submit" id="submit" value="Submit" /></td>
</tr>
</table>
<p> </p>
</form>
答案 0 :(得分:3)
创建名称为“
”的隐藏输入字段<input id="ratings" type="hidden" name="ratings" value="" />
然后使用Javascript动态更新此输入的值,具体取决于点击蜜蜂的评级值。
如下所示:
document.getElementById('ratings').value='4'
或者,如果您不想在不提交表单的情况下动态执行此操作,则可以使用AJAX请求将此信息提交到数据库。
作为一个非常重要的旁注,在您继续前进之前,请请停止使用mysql_*
功能并开始使用PDO或mysqli。特别是查看带有绑定值的预准备语句,除非您希望通过SQL注入看到整个数据库被删除。
答案 1 :(得分:3)
TINYINT
。如果您要存储星星的分数,[例如:3.5 / 5星],则使用DECIMAL(2,1)
。