我的代码有问题。它总是复制输入。这是我的职责:
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>
每次刷新页面时,都会复制重复的值。
答案 0 :(得分:-1)
这将始终在页面刷新时插入重复记录。你需要改变一下逻辑。
我们需要这样的东西
$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']}')";
}