数据库连接通过。 PHP

时间:2012-02-06 09:40:12

标签: php javascript mysql html

我的网站包含html表单中的调查。

在调查结束时,有一个提交类型按钮,用于“调用”.php文件,其中数据被插入到数据库中。这一切都运行良好,但我需要做的是在调查的第一页填写后调用另一个php文件,以检查数据库中是否已存在具有该信息的元组。

但是看到我最后已经有了提交类型按钮,我不能正确使用它? 那么我该如何解决这个问题呢?任何想法?

2 个答案:

答案 0 :(得分:0)

你必须提供一些你尝试过的代码..

您可以使用mysql_num_rows()

if(mysql_num_rows($sql)){
    // record exists
    // show some error and don't save
    }
    else
    {
    //no record exists
    // save in db
    }

答案 1 :(得分:0)

你可以看看数据库中的东西是否已经存在,然后你把它放在那里..你可以用简单的php做到这一点

$name = $_POST['name']; // save the name from the form to the var.
$last_name = $_POST['lastname']; // save the last name from the form to the var.
$sql = mysql_query("SELECT * FROM `data` WHERE `name` = '".$name."' AND `last_name` = '".$last_name."' "); // look in the database if there is alreddy a record with that name.
if(mysql_num_rows($sql) > 0){ // if its bigger than 0 it exists
  // give an error back to the user that they alreddy exist
}
else{ // if not exists
  // user your code that puts it to the database
}
相关问题