开发者

PHP mySQL database check

开发者 https://www.devze.com 2022-12-27 08:18 出处:网络
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 lan

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? 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. Get the users id into a variable and do this query.

$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

0

精彩评论

暂无评论...
验证码 换一张
取 消