数据库连接通过.php


database connection via. php

我有一个网站,在html表单中包含一个调查。

在调查的最后,有一个提交类型按钮,它"调用"一个.php文件,在该文件中,数据被插入到数据库中。这一切都很好,但我需要做的是在填写调查的第一页后调用另一个php文件,以检查数据库中是否已经存在包含该信息的元组。

但鉴于我最终已经有了一个提交类型按钮,我不能使用它,对吧?那么我该如何解决这个问题呢?有什么想法吗?

您必须提供一些您尝试过的代码。。

您可以使用mysql_num_rows()。

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

您可以在将tings放入数据库之前查看数据库中是否已经存在。。你可以用简单的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
}