简体   繁体   中英

Check if record exists and autofill the id from mysql table

  1. When user fills the form, if LinkedIn Id already exists, it should throw an error saying LinkedIn Id already exists. .

  2. Form is not saving data if user does not enter ContactID . ContactID is an auto-increment field in MySQL table. Since more than one user works on the same form at the same time, I want ContactID to be auto-filled by the next available number from MySQL table.

Here, is my HTML form code and following, the PHP code. If you require any other details to debug, please ask.

<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 Code:

<?php

// create a variable
if (isset($_POST)){

$ContactID=$_POST['ContactID'];
$ContactOwner=$_POST['ContactOwner'];
$LeadSource=$_POST['LeadSource'];
$First_name=$_POST['First_name'];
$Last_name=$_POST['Last_name'];
$AccountName=$_POST['AccountName'];
$Title=$_POST['Title'];
$EmailID=$_POST['EmailID'];
$Industry=$_POST['Industry'];
$Department=$_POST['Department'];
$Phone=$_POST['Phone'];
$Mobile=$_POST['Mobile'];

$Today_date=$_POST['Today_date'];
$LinkedIn=$_POST['LinkedIn'];
$CallStatus=$_POST['CallStatus'];
$Website=$_POST['Website'];
$Street=$_POST['Street'];
$OtherStreet=$_POST['OtherStreet'];
$City=$_POST['City'];
$State=$_POST['State'];
$Zip=$_POST['Zip'];
$Country=$_POST['Country'];
$Description=$_POST['Description'];
}
//create connection 
$connect=mysqli_connect('localhost','root','','contacts');
$check="SELECT COUNT(*) FROM contact where ContactID='$_POST[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(ContactID,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('$ContactID','$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/>";
}
else
{
    echo "Error adding user in database, ContactID exists.<br/>";
}
}

Try changing:

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

to

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

Also:

Form is not saving data if user do not enters ContactID. *** ContactID is an autoincrement field in mysql table.

In your insert do not include

ContactID,

since this is an autoincrement field as you mention. What you should do instead is to do this:

//create connection

$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/>";
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM