如何检查用户和数据库之间的输入是否重复? 这是用户将数据输入数据库的表单 并且无论是否保存相同或不相同的记录,此代码的结果都将起作用。
$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')";}
我写了这个语法错误....(解析错误:语法错误,意外' @' in) 我应该使用While循环重写吗? 你能告诉我如何更正代码吗?
答案 0 :(得分:2)
$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 ....你需要改变典型......
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;
答案 1 :(得分:0)
您可以在mysql中的年份列上放置唯一索引。这将是所有用户的独特年份。
您可能需要2个字段(年+用户ID)的唯一索引