重复 php 函数
Duplicates php function
我的代码有问题。它总是重复输入。这是我的函数:
public function insert()
{
$con=mysqli_connect("127.0.0.1","root","","dbms_project");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$sql="INSERT INTO customers (First_Name,Last_Name,Address,Phone)
VALUES
('{$_POST['first_name']}','{$_POST['last_name']}','{$_POST['address']}','{$_POST['phone']}')";
mysqli_query($con, $sql);
if (!mysqli_query($con,$sql))
{
//redirect('home/customer');
}
$this->load->view('customer_view');
mysqli_close($con);
}
这是网站中的代码。我还尝试存储下一个值以将其用作 customer_id:
的值
//using insert function
<form action ="<?= site_url('home/insert')?>" class="formInsert" method="post">
<td><button class="btn btn-xs btn-primary btn-block" type="submit" value="Login">+</button></td>
<td>
//i'm trying to put the output and use it to be the value of the customer id
<?php $con=mysqli_connect("127.0.0.1","root","","dbms_project");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT max(Customer_ID) FROM customers");
$lastvalue = mysqli_fetch_row($result);
$resultlastvalue = $lastvalue[0] + 1;
echo " ". $resultlastvalue . " " ;?>
</td>
<td><input type="text" class="form-control" name="first_name" placeholder="First Name" required maxlength="40" autofocus /></td>
<td><input type="text" class="form-control" name="last_name" placeholder="Last Name" required maxlength="40" autofocus /></td>
<td><input type="text" class="form-control" name="address" placeholder="Address" required maxlength="40" autofocus /></td>
<td><input type="text" class="form-control" name="phone" placeholder="Phone" required maxlength="40" autofocus /></td>
</form>
每次我刷新页面时,它都会复制重复的值。
这将始终在页面刷新时插入重复记录。你需要稍微改变一下逻辑。
- 取名字、姓氏和phone三个变量
- 连接到数据库
- 使用 SELECT 查询
检查这些是否存在
- 如果是,请不要运行 INSERT 查询
- 如果没有,插入
我们需要这样的东西
$select = " SELECT * FROM customers WHERE First_Name = '$_POST['first_name']' AND Last_Name = '$_POST['last_name']' AND Phone = '$_POST['phone']' ";
$query = mysqli_query($select);
if(mysqli_num_rows($query) > 0){
echo "User already exists";
}
else {
$sql="INSERT INTO customers (First_Name,Last_Name,Address,Phone)
VALUES ('{$_POST['first_name']}','{$_POST['last_name']}','{$_POST['address']}','{$_POST['phone']}')";
}
我的代码有问题。它总是重复输入。这是我的函数:
public function insert()
{
$con=mysqli_connect("127.0.0.1","root","","dbms_project");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$sql="INSERT INTO customers (First_Name,Last_Name,Address,Phone)
VALUES
('{$_POST['first_name']}','{$_POST['last_name']}','{$_POST['address']}','{$_POST['phone']}')";
mysqli_query($con, $sql);
if (!mysqli_query($con,$sql))
{
//redirect('home/customer');
}
$this->load->view('customer_view');
mysqli_close($con);
}
这是网站中的代码。我还尝试存储下一个值以将其用作 customer_id:
的值 //using insert function
<form action ="<?= site_url('home/insert')?>" class="formInsert" method="post">
<td><button class="btn btn-xs btn-primary btn-block" type="submit" value="Login">+</button></td>
<td>
//i'm trying to put the output and use it to be the value of the customer id
<?php $con=mysqli_connect("127.0.0.1","root","","dbms_project");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT max(Customer_ID) FROM customers");
$lastvalue = mysqli_fetch_row($result);
$resultlastvalue = $lastvalue[0] + 1;
echo " ". $resultlastvalue . " " ;?>
</td>
<td><input type="text" class="form-control" name="first_name" placeholder="First Name" required maxlength="40" autofocus /></td>
<td><input type="text" class="form-control" name="last_name" placeholder="Last Name" required maxlength="40" autofocus /></td>
<td><input type="text" class="form-control" name="address" placeholder="Address" required maxlength="40" autofocus /></td>
<td><input type="text" class="form-control" name="phone" placeholder="Phone" required maxlength="40" autofocus /></td>
</form>
每次我刷新页面时,它都会复制重复的值。
这将始终在页面刷新时插入重复记录。你需要稍微改变一下逻辑。
- 取名字、姓氏和phone三个变量
- 连接到数据库
- 使用 SELECT 查询 检查这些是否存在
- 如果是,请不要运行 INSERT 查询
- 如果没有,插入
我们需要这样的东西
$select = " SELECT * FROM customers WHERE First_Name = '$_POST['first_name']' AND Last_Name = '$_POST['last_name']' AND Phone = '$_POST['phone']' ";
$query = mysqli_query($select);
if(mysqli_num_rows($query) > 0){
echo "User already exists";
}
else {
$sql="INSERT INTO customers (First_Name,Last_Name,Address,Phone)
VALUES ('{$_POST['first_name']}','{$_POST['last_name']}','{$_POST['address']}','{$_POST['phone']}')";
}