使用PHP防止重复记录到表 [英] Prevent duplicate records to a table using PHP

查看:64
本文介绍了使用PHP防止重复记录到表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想防止使用PHP从表单向数据库表中重复值.

I want to prevent duplicate values into a database table from a form using PHP.

我创建了以下内容:

一个具有名为 clients 的表的数据库:

A database with a table named clients:

CREATE TABLE clients(
 firstName varchar(20),
 lastName varchar(20),
 primary key(firstName, lastName));

名为 form.html

<h2>Enter your First and Last Name</h2>
<form action="frm_script.php" method="post">
<p><strong>First Name:</strong><br /> <input type="text" name="firstName" /></p>
<p><strong>Last Name:</strong><br /> <input type="text" name="lastName"/></p>
<input type="submit" name="submit" value="Add Customer" />
</form>

名为 frm_script.php

<?php
if(isset($_POST['submit']))
{

//get the name and comment entered by user
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];

//connect to the database
$dbc = mysqli_connect('host', 'username', 'password', 'dbname') or die('Error connecting to MySQL server');

//insert results from the form input
$query = "INSERT IGNORE INTO clients(firstName, lastName) VALUES('$firstName', '$lastName')";

$result = mysqli_query($dbc, $query) or die('Error querying database.');

mysqli_close($dbc);
}
echo "Customer Added";
?>

到目前为止,使用我的 frm_script.php 文件,上述方法有效,并且为客户添加的唯一记录显示.但是,对于重复记录,它将引发错误查询数据库".

So far with my frm_script.php file the above works and for a unique record displays customer added. However, for a duplicate record it throws "Error Querying Database".

如何为以下内容更新 frm_script.php 脚本?

How can I update the frm_script.php script for the following?

如果在输入名字/姓氏组合时发现重复的行,则应在显示该记录的同时显示消息已列出客户".

If a duplicate row is found on entering a First / Last name combination it should display the message "Client already listed" along with that record.

如果在表单上输入名字/姓氏组合时未发现重复的行,则应将条目插入数据库并显示消息已添加客户"

If no duplicate row is found on entering a First / Last name combination on the form it should insert the entry to the database and display the message "customer added"

我读到应该先运行SELECT,然后再运行INSERT,但是我不确定如何将其实现到现有代码中.

I have read that a SELECT should be run first then an INSERT but I am not sure how to implement this into my existing code.

推荐答案

  <?php
    if(isset($_POST['submit'])) {

    //get the name and comment entered by user
    $firstName = $_POST['firstName'];
    $lastName = $_POST['lastName'];

    //connect to the database
    $dbc = mysqli_connect('host', 'username', 'password', 'dbname') or die('Error connecting to MySQL server');
    $check=mysqli_query($dbc,"select * from clients where firstname='$firstname' and lastname='$lastname'");
    $checkrows=mysqli_num_rows($check);

   if($checkrows>0) {
      echo "customer exists";
   } else {  
    //insert results from the form input
      $query = "INSERT IGNORE INTO clients(firstName, lastName) VALUES('$firstName', '$lastName')";

      $result = mysqli_query($dbc, $query) or die('Error querying database.');

      mysqli_close($dbc);
    }
    echo "Customer Added";
    };
  ?>

只要检查数据库中是否有名字和姓氏的行(如果存在),则回显-其他信息 插入

just check for rows in your db for firstname and lastname if exists echo- your message else insert

这篇关于使用PHP防止重复记录到表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆