2015-02-24 88 views
0

我遇到了我的代码问题。它总是重复输入。这里是我的功能:重复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> 

我每次刷新页面,它复制了重复的值。

+1

*“它总是重复输入。”* - 哪一个?另外,你将MySQL API与'mysql_error()'混合在一起。 *“每次刷新页面都会复制重复的值!”* - 错误...我不明白。在数据库中输出或输入重复? – 2015-02-24 14:16:49

+0

我正要编辑它。无论如何,我每次刷新它与这个链接http:// localhost/ci_intro/home /插入它重复更多。 – Jonaii 2015-02-24 14:18:36

+0

重复如何?它在数据库中插入重复条目? – 2015-02-24 14:25:25

回答

-1

这将始终在页面刷新上插入重复记录。你需要改变一下逻辑。

  1. 取名字,姓氏和电话在3个变量
  2. 连接到数据库
  3. 做一个检查,如果存在那些使用SELECT查询
  4. 如果是的话,请不要运行INSERT查询
  5. 如果没有,请插入

我们需要有这样的事情

$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']}')"; 
     } 
+1

''$ _POST ['first_name']''等会产生语法错误。它应该像OP现在有''{$ _POST ['first_name']}'等。和INSERT中的下面一样。但是,使用这种方法对SQL注入是开放的。 – 2015-02-24 14:41:36

+0

我们只是提供了逻辑(插入前检查)。您是否期望为您编写完整的代码? – 2015-02-24 14:44:27

+1

这不是“我的”问题。我是那个试图帮助并改进答案的人。现在哪个,不行。 – 2015-02-24 14:45:15