Php 检查记录是否存在,并从mysql表中自动填充id

Php 检查记录是否存在,并从mysql表中自动填充id,php,html,mysql,Php,Html,Mysql,当用户填写表单时,如果LinkedIn Id已经存在,它应该抛出一个错误,说明LinkedIn Id已经存在 若用户未输入ContactID,则表单不保存数据。ContactID是MySQL表中的一个自动递增字段。由于不止一个用户同时在同一个表单上工作,所以我希望ContactID由MySQL表中的下一个可用数字自动填充 这里是我的HTML表单代码,下面是PHP代码。如果您需要调试任何其他详细信息,请询问 <html> <body> <form method="po

当用户填写表单时,如果LinkedIn Id已经存在,它应该抛出一个错误,说明LinkedIn Id已经存在

若用户未输入ContactID,则表单不保存数据。ContactID是MySQL表中的一个自动递增字段。由于不止一个用户同时在同一个表单上工作,所以我希望ContactID由MySQL表中的下一个可用数字自动填充

这里是我的HTML表单代码,下面是PHP代码。如果您需要调试任何其他详细信息,请询问

<html>
<body>
<form method="post" action="demo.php">

<link rel="stylesheet" href="contact_css.css">
<!--Create a table -->

<table>
<tr><td><b>Contact Information</b></td>
</tr>
<tr>
 <div class="leftside">
  <td>ContactID</td>
  <td><input type="text" name="ContactID"></td>
 </div>
 <div class="rightside">
  <td>ContactOwner</td>
<!--  <td><input type="text" name="ContactOwner"></td>-->
    <td><select name="ContactOwner">
 <option value="None">None</option>
 <option value="Malik">Malik</option>
 <option value="Ankit">Ankit</option>
 <option value="Vikrant">Vikrant</option>
 </select></td>
 </div>
    </tr>
  <tr>
  <div class="rightside">
  <td>LeadSource</td>
  <td><select name="LeadSource">
 <option value="None">None</option>
 <option value="Advertisement">Advertisement</option>
 <option value="ColdCall">ColdCall</option>
 <option value="LinkedIn">LinkedIn</option>

<option value="Web">Web</option>

 </select></td>
  <!--<td><input type="text" name="LeadSource"></td>-->
 </div>

<div class="leftside">
  <td>First_name</td>
  <td><input type="text" name="First_name"></td>
 </div>
 </tr>
 <tr>
 <div class="rightside">
 <td>Last_name</td>
 <td><input type="text" name="Last_name"></td>
 <td>AccountName</td>
  <td><input type="text" name="AccountName"></td>
  </tr>
  <tr>
 <td>Title</td>
  <td><input type="text" name="Title"></td>
   <td>EmailID</td>
  <td><input type="text" name="EmailID"></td>
  </tr>
  <tr>
  <td>Industry</td>
  <td><input type="text" name="Industry"></td>
  <td>Department</td>
  <td><input type="text" name="Department"></td>
  </tr>
  <tr>
  <td>Phone</td>
  <td><input type="text" name="Phone" required></td>
  <td>Mobile</td>
  <td><input type="text" name="Mobile"></td>
  </tr>
  <tr>

  <td>Today_date</td>
  <td><input type="date" name="Today_date"></td>

  <td>LinkedIn</td>
  <td><input type="text" name="LinkedIn"></td>
  </tr>
  <tr>
  <td>CallStatus</td>
  <td><select name="CallStatus">
 <option value="None">None</option>
 <option value="AnsweringMachine">AnsweringMachine</option>
 <option value="Callback">Callback</option>
 <option value="NotInterested">NotInterested</option>
 <option value="Prospect">Prospect</option>
 <option value="WrongContact">WrongContact</option>
 <option value="PerformedInternally">PerformedInternally</option>
 <option value="LessThan30Employee">LessThan30Employee</option>
</select></td>
<td>Website</td>
<td><input type="text" name="Website"></td>

</tr>
  </table>

<!-- Second table-->
<table>
<tr><td><b>Address Information</b></td>
</tr>
<tr>
 <div class="leftside">
  <td>Street</td>
  <td><input type="text" name="Street"></td>
 </div>
 <div class="rightside">
  <td>OtherStreet</td>
  <td><input type="text" name="OtherStreet"></td>
 </div>
 </tr>
 <tr>
 <div class="leftside">
  <td>City</td>
  <td><input type="text" name="City"></td>
 </div>
 <div class="rightside">
  <td>State</td>
  <td><input type="text" name="State"></td>
 </div>
 </tr>
 <tr>
  <td>Zip</td>
  <td><input type="text" name="Zip"></td>
  <td>Country</td>
  <td><input type="text" name="Country"></td>
 </tr>
 </table>
<!--Third table-->
<table>
<tr><td><b>Description Information</b></td>
</tr>
<tr>
 <td>Description</td>
  <td><input type="text" name="Description" class="Description"></td>

 </table>
 <button type="button">Cancel</button>
 <button type="button" class="button2" onclick="window.location.href='fetch_data.php'"  />View</button>
 <button type="button" class="button3" onclick="window.location.href='exm_list.php'"  />Edit</button>
 <button type="submit">Add</button>

 </form>
 < /body>
 </html>
PHP代码:

尝试更改:

$check="SELECT COUNT(*) FROM contact where ContactID='$_POST[ContactID]' ";

此外:

如果用户未输入联系人ID,则表单不保存数据。***联系人ID 是mysql表中的自动增量字段

在您的插入中,不包括

ContactID,
因为这是您提到的自动递增字段。您应该做的是:

//创建连接

$connect=mysqli_connect('localhost','root','','contacts');
$check="SELECT COUNT(*) FROM contact where ContactID='$ContactID' ";
$result=mysqli_query($connect,$check);
$data=mysqli_fetch_array($result, MYSQLI_NUM);

if($data[0] > 1){
          echo "LinkedIn id already exists";
}else {

    $newUser="INSERT INTO contact(ContactOwner,LeadSource,First_name,Last_name,AccountName,Title,EmailID,Industry,Department,Phone,Today_date,LinkedIn,CallStatus,Website,Street,OtherStreet,City,State,Zip,Country,Description)
            VALUES('$ContactOwner','$LeadSource','$First_name','$Last_name','$AccountName','$Title','$EmailID','$Industry','$Department','$Phone','$Today_date','$LinkedIn','$CallStatus','$Website','$Street','$OtherStreet','$City','$State','$Zip','$Country','$Description')";
    if (mysqli_query($connect,$newUser))
    {
        echo "Information Added<br/>";
    }
}

请使用以防止SQL注入。@JasmineLalani发生了什么事?您希望发生什么事?:您编辑和共享的代码,我试图运行它,但它无法按我的要求运行。即。如果我没有输入ContactID,则表单不会提交,它会在数据库中添加用户时出错,ContactID存在。我已从insert语句中删除ContactID您是否遵循$newUser=插入contactContactOwner、LeadSource、First\u name、Last\u name、AccountName、Title、EmailID、行业、部门、电话、今日日期、LinkedIn、CallStatus、,网站,街道,其他街道,城市,州,邮编,国家,描述值“$ContactOwner”、“$LeadSource”、“$First_name”、“$Last_name”、“$AccountName”、“$Title”、“$EmailID”、“$Industry”、“$Department”、“$Phone”、“$Today_date”、“$LinkedIn”、“$CallStatus”、“$Website”、“$Street”、“$OtherStreet”、“$City”、“$State”、“$Zip”、“$Country”、“$Description”;?另外,请注意,由于$check变量中正在使用ContactID,因此需要输入ContactID
ContactID,
$connect=mysqli_connect('localhost','root','','contacts');
$check="SELECT COUNT(*) FROM contact where ContactID='$ContactID' ";
$result=mysqli_query($connect,$check);
$data=mysqli_fetch_array($result, MYSQLI_NUM);

if($data[0] > 1){
          echo "LinkedIn id already exists";
}else {

    $newUser="INSERT INTO contact(ContactOwner,LeadSource,First_name,Last_name,AccountName,Title,EmailID,Industry,Department,Phone,Today_date,LinkedIn,CallStatus,Website,Street,OtherStreet,City,State,Zip,Country,Description)
            VALUES('$ContactOwner','$LeadSource','$First_name','$Last_name','$AccountName','$Title','$EmailID','$Industry','$Department','$Phone','$Today_date','$LinkedIn','$CallStatus','$Website','$Street','$OtherStreet','$City','$State','$Zip','$Country','$Description')";
    if (mysqli_query($connect,$newUser))
    {
        echo "Information Added<br/>";
    }
}