gpt4 book ai didi

php if语句(如何查看用户输入重复记录)

转载 作者:行者123 更新时间:2023-11-29 06:29:11 25 4
gpt4 key购买 nike

如何检查用户和数据库之间的输入是否重复?这是用户向数据库输入数据的表单并且无论保存相同或不同的记录,此代码的结果都将有效。

    $con = mysql_connect("localhost","root","usbw");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("booking", $con);

$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$time = $_POST['time'];
$classroom = $_POST['classroom'];
$subject = $_POST['subject'];
$s=implode(" ",$subject);
$sql1 = "SELECT * FROM `test` WHERE `year` LIMIT 0, 30 ";
$result=mysql_query("$sql1");
$row =mysql_fetch_row($result);




$sql = "INSERT INTO `test`(`year`, `month`, `day`, `time`, `classroom`,`subject`) VALUES ('$year','$month','$day','$time','$classroom','$s')";



?>

我写了一段代码,我认为可以确定相同的记录如果相同,则用户无法输入。如果不是同一个用户可以输入。代码表示如下:

if($row[1]==$year){ 
  echo'the data was repeated';

任何人都可以帮助解决我的 if 语句问题吗?

更新:

if($row[1]==$year){ 
  echo'the data was repeated';} else{
$sql = "INSERT INTO `test`(`year`, `month`, `day`, `time`, `classroom`,`subject`) VALUES ('$year','$month','$day','$time','$classroom','$s')";}

I wrote this syntax error....(Parse error: syntax error, unexpected '@' in )我应该使用 While 循环来重写吗?你能告诉我如何更正代码吗?

最佳答案

$con = mysql_connect("localhost","root","usbw");
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("booking", $con);

$year = $_POST['year'];
$month = $_POST['month'];
$day = $_POST['day'];
$time = $_POST['time'];
$classroom = $_POST['classroom'];
$subject = $_POST['subject'];
$s=implode(" ",$subject);

$sql = "INSERT INTO `test`(`year`, `month`, `day`, `time`, `classroom`,`subject`) VALUES ('$year','$month','$day','$time','$classroom','$s')";
$qr = mysql_query($sql);

if (!$qr) {
echo mysql_error();
}

还有 mysql....你需要改变 typs...

CREATE TABLE `test` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`year` int(11) DEFAULT NULL,
`month` int(11) DEFAULT NULL,
`day` int(11) DEFAULT NULL,
`time` int(11) DEFAULT NULL,
`classroom` int(11) DEFAULT NULL,
`subject` int(11) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `year` (`year`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

关于php if语句(如何查看用户输入重复记录),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28958467/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com