我遇到了我的代码问题。它总是重复输入。这里是我的功能:重复php函数
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>
我每次刷新页面,它复制了重复的值。
*“它总是重复输入。”* - 哪一个?另外,你将MySQL API与'mysql_error()'混合在一起。 *“每次刷新页面都会复制重复的值!”* - 错误...我不明白。在数据库中输出或输入重复? – 2015-02-24 14:16:49
我正要编辑它。无论如何,我每次刷新它与这个链接http:// localhost/ci_intro/home /插入它重复更多。 – Jonaii 2015-02-24 14:18:36
重复如何?它在数据库中插入重复条目? – 2015-02-24 14:25:25