这是我的编码。基本上我想根据($ result2)中定义的用户设置表单限制。另一件事是约会。例如。今天可以提交3表格然后明天可以提交另外3表格直到用户对$ result2进行更改。这段代码的问题是它将忽略日期并让用户提交没有限制的表单。希望你们能帮忙,谢谢
$name = $_POST['name'];
$address = $_POST['address'];
$contact = $_POST['contact'];
$email = $_POST['email'];
$tbl_name="torder";
$sql="INSERT INTO $tbl_name
(name,address,contact,email,orderdate)
VALUES('$name','$address','$contact','$email',now())";
$tbl2_name="tblfree";
$query="SELECT * FROM tblfree";
$result2=mysql_query($query);
$row = mysql_fetch_array(
mysql_query("SELECT COUNT(*) AS 'submit' FROM torder"));
if ($row['submit'] > $result2) {
echo 'We have reached our Free-T limit';}
else {
$result=mysql_query($sql);
echo 'success';
}
答案 0 :(得分:1)
问题在于你将标量值与mysql_result进行比较,你可以看到下面发生的事情
if ($row['submit'] > $result2)
相反,您需要获取$result2
的结果并进行比较,因此请将其修改为以下
$result2=mysql_query($query);
$row2 = mysql_fetch_array($result2);
$row = mysql_fetch_array(mysql_query("SELECT COUNT(*) AS 'submit' FROM torder where orderdate = {$current}"));
if ($row['submit'] > $row2['value_you_want_to_compare']) {
echo 'We have reached our Free-T limit';}
else {
$result=mysql_query($sql);
echo 'success';
}
此外,您的代码容易受到SQL注入攻击,为了解决这个问题,请停止使用mysql_ *函数,因为它们已被弃用并开始使用mysqli或PDO和预先准备好的语句
答案 1 :(得分:1)
$tbl2_name="tblfree";
$query="SELECT amount FROM tblfree where id=1";
$result2=mysql_fetch_array(mysql_query($query));
$row = mysql_fetch_array(mysql_query("SELECT COUNT(*) AS submit FROM torder where orderdate = CURDATE()"));
if ($row ['submit'] >= $result2['amount']) {
echo 'We have reached our Free-T limit';
echo $result2['amount'];}
else {
$tbl_name="torder";
$sql="INSERT INTO $tbl_name
(name,address,contact,email,orderdate)VALUES('$name','$address','$contact','$email',CURDATE())";
$result=mysql_query($sql);
$sql="INSERT INTO $tbl_name (name,address,contact,email,orderdate)VALUES('$name','$address','$contact','$email','$orderdate')";
echo 'success';
}