简体   繁体   English

PHP mySQL数据库检查

[英]PHP mySQL database check

I am having endless troubles with duplicate entries, so I need to check the database, and if a user has already entered that day, their entry will not be submitted and they will be redirected to a landing page that tells them they have already entered that day, and that they may only enter again tomorrow. 我在重复条目方面遇到了无尽的麻烦,因此我需要检查数据库,如果用户当天已经输入,则不会提交他们的条目,他们将被重定向到一个登陆页面,告诉他们他们已经输入了一天,直到明天他们才可以再次进入。

The field I would like to check is the id_number field in the database, since each user has a unique id number, so basically, if a user with the same id number submitted on the same date they should be redirected to a landing page, how would I go about doing this? 我想检查的字段是数据库中的id_number字段,因为每个用户都有一个唯一的ID号,所以基本上,如果在同一日期提交了具有相同ID号的用户,应该将其重定向到登录页面,我会去做吗? I am still new to a lot of this, so please be forgiving. 我对其中的许多内容还是陌生的,所以请原谅。

Thanx in advance. 提前感谢。

before you submit your sql query to add a new record you can read the DB first to make sure the ID is not already in there. 在提交sql查询以添加新记录之前,您可以先阅读数据库以确保ID不在其中。 Get the users id into a variable and do this query. 将用户ID放入变量中,然后执行此查询。

$query = "select * from mytable where `id` = '$id'";
$result = mysql_query($query);

then you can count the number of records returned and do different things depending on the result, checking that its zero before adding them. 那么您可以计算返回的记录数,并根据结果执行不同的操作,并在添加记录之前检查其为零。 If its not zero then redirect. 如果其不为零,则重定向。

if(mysql_num_rows($result) > 0)
{
 //add them
}
else
{
 //redirect them
}

Hope this helps you out 希望这可以帮助你

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM