gpt4 book ai didi

php - 检查记录是否存在并自动填充 mysql 表中的 id

转载 作者:行者123 更新时间:2023-11-29 10:45:57 27 4
gpt4 key购买 nike

  1. 当用户填写表单时,如果 LinkedIn Id 已存在,则应抛出错误,指出 LinkedIn Id 已存在。

  2. 如果用户未输入 ContactID,表单不会保存数据。 ContactID是MySQL表中的一个自增字段。由于多个用户同时处理同一个表单,我希望使用 MySQL 表中的下一个可用数字自动填充 ContactID

这里是我的 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 代码:

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

最佳答案

尝试改变:

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

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

另外:

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

在您的插入内容中不要包含

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

关于php - 检查记录是否存在并自动填充 mysql 表中的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44564018/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com