PHP,MYSQL - > INSERT INTO语句后直接SELECT *

时间:2015-03-15 16:37:34

标签: php mysqli

我想在表中插入一些数据,然后直接从该表中检索所有数据。但奇怪的是,当我这样做时,我收到了一个错误。

所以我的问题是,是否可以构建一个通过$ _POST插入一些数据的函数,之后直接从表中检索所有数据,包括新的新插入的$ _POST数据?

错误:

  

注意:未定义的变量:第293行/mnt/webr/c3/76/54476376/htdocs/includes/functions.php中的mysqli致命错误:在/ mnt / webr /中调用null上的成员函数prepare()第293行的c3 / 76/54476376 / htdocs / includes / functions.php

/**********************************************************************************************************************************

2. Customer new ******************************************************************************************************************

***********************************************************************************************************************************/

function customer_new($user_name) {

    //DB settings
     include_once "config/config_fl.php" ;
           $query_insert = ("INSERT INTO customers (user_name               , 
                                                    customer_name               ,
                                                                              customer_legal_sort         ,
                                                                            customer_vat_applicable ,
                                                                            customer_payment_terms  ,
                                                                            customer_contactperson  ,
                                                                            customer_email          ,
                                                                            customer_telphone       ,
                                                                            customer_address_visit  ,
                                                                            customer_number_visit   ,
                                                                            customer_num_add_visit  ,
                                                                            customer_postal_visit   ,
                                                                            customer_city_visit     ,
                                                                            customer_country_visit  ,
                                                                            customer_visit_vs_post  ,
                                                                            customer_address_post     ,
                                                                            customer_number_post      ,
                                                                            customer_num_add_post       ,
                                                                            customer_postal_post    ,
                                                                            customer_city_post      ,
                                                                            customer_country_post   ) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");

          $stmt = $mysqli->prepare($query_insert);
          $stmt->bind_param("sssssssssissssssissss",  $user_name                        ,
                                                                            $_POST['customer_name']           ,
                                                                            $_POST['customer_legal_sort']     ,
                                                                            $_POST['customer_vat_applicable'] ,
                                                                            $_POST['customer_payment_terms']  ,
                                                                            $_POST['customer_contactperson']  ,
                                                                            $_POST['customer_email']          ,
                                                                            $_POST['customer_telphone']       ,
                                                                            $_POST['customer_address_visit']  ,
                                                                            $_POST['customer_number_visit']   ,
                                                                            $_POST['customer_num_add_visit']  ,
                                                                            $_POST['customer_postal_visit']   ,
                                                                            $_POST['customer_city_visit']     ,
                                                                            $_POST['customer_country_visit']  ,
                                                                            $_POST['customer_visit_vs_post']  ,
                                                                            $_POST['customer_address_post']   ,
                                                                            $_POST['customer_number_post']    ,
                                                                            $_POST['customer_num_add_post']   ,
                                                                            $_POST['customer_postal_post']    ,
                                                                            $_POST['customer_city_post']      ,
                                                                            $_POST['customer_country_post']   );
          $stmt->execute();
          $stmt->store_result();
   }



/**********************************************************************************************************************************

3. Customer info ******************************************************************************************************************

***********************************************************************************************************************************/

function customer_info($user_name) {
  unset($mysqli);
  //DB settings
  include_once "config/config_fl.php" ;

  $query_select = ("SELECT * FROM customers where user_name = ? ");
  $stmt = $mysqli->prepare($query_select);
  $stmt->bind_param("s", $user_name);
  $stmt->execute();
  $stmt->store_result();
  $count = $stmt->num_rows();

   $stmt->bind_result ($customer_number         ,
                       $user_name               , 
                       $customer_name               ,
                       $customer_legal_sort       ,
                       $customer_vat_applicable ,
                       $customer_payment_terms  ,
                       $customer_contactperson  ,
                       $customer_email          ,
                       $customer_telphone       ,
                       $customer_address_visit  ,
                       $customer_number_visit   ,
                       $customer_num_add_visit  ,
                       $customer_postal_visit   ,
                       $customer_city_visit     ,
                       $customer_country_visit  ,
                       $customer_visit_vs_post  ,
                       $customer_address_post     ,
                       $customer_number_post      ,
                       $customer_num_add_post       ,
                       $customer_postal_post    ,
                       $customer_city_post      ,
                       $customer_country_post   ); 


      $stmt->fetch();

    //  $customer_data=array();


      $customer_data = array ( 'customer_number'        =>$customer_number         ,
                               'user_name'              =>$user_name               , 
                               'customer_name'          =>$customer_name                 ,
                               'customer_legal_sort'    =>$customer_legal_sort       ,
                               'customer_vat_applicable'=>$customer_vat_applicable ,
                               'customer_payment_terms '=>$customer_payment_terms    ,
                               'customer_contactperson' =>$customer_contactperson  ,
                               'customer_email'         =>$customer_email          ,
                               'customer_telphone'      =>$customer_telphone         ,
                               'customer_address_visit' =>$customer_address_visit  ,
                               'customer_number_visit'  =>$customer_number_visit     ,
                               'customer_num_add_visit' =>$customer_num_add_visit    ,
                               'customer_postal_visit'  =>$customer_postal_visit   ,
                               'customer_city_visit'    =>$customer_city_visit     ,
                               'customer_country_visit' =>$customer_country_visit    ,
                               'customer_visit_vs_post' =>$customer_visit_vs_post    ,
                               'customer_address_post'  =>$customer_address_post     ,
                               'customer_number_post'   =>$customer_number_post    ,
                               'customer_num_add_post'  =>$customer_num_add_post     ,
                               'customer_postal_post'   =>$customer_postal_post    ,
                               'customer_city_post'     =>$customer_city_post      ,
                               'customer_country_post'  =>$customer_country_post   ); 

  $stmt->close();
  $mysqli->close();
  return $customer_data; 

}

2 个答案:

答案 0 :(得分:0)

我认为问题在于,如果不释放每个mysqli_execute的结果,就不能执行多个$stmt->close()。 也许你应该尝试,而不是$stmt->free_result()一个<?php include_once "config/config_fl.php" ; function customer_new($user_name) { global $mysqli; $query_insert = ("INSERT INTO customers ( user_name , customer_name , customer_legal_sort , customer_vat_applicable , customer_payment_terms , customer_contactperson , customer_email , customer_telphone , customer_address_visit , customer_number_visit , customer_num_add_visit , customer_postal_visit , customer_city_visit , customer_country_visit , customer_visit_vs_post , customer_address_post , customer_number_post , customer_num_add_post , customer_postal_post , customer_city_post , customer_country_post ) VALUES ( ?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,? )" ); $stmt = $mysqli->prepare($query_insert); $stmt->bind_param("sssssssssissssssissss", $user_name , $_POST['customer_name'] , $_POST['customer_legal_sort'] , $_POST['customer_vat_applicable'] , $_POST['customer_payment_terms'] , $_POST['customer_contactperson'] , $_POST['customer_email'] , $_POST['customer_telphone'] , $_POST['customer_address_visit'] , $_POST['customer_number_visit'] , $_POST['customer_num_add_visit'] , $_POST['customer_postal_visit'] , $_POST['customer_city_visit'] , $_POST['customer_country_visit'] , $_POST['customer_visit_vs_post'] , $_POST['customer_address_post'] , $_POST['customer_number_post'] , $_POST['customer_num_add_post'] , $_POST['customer_postal_post'] , $_POST['customer_city_post'] , $_POST['customer_country_post'] ); $stmt->execute(); // You don't need to store result $stmt->free_result(); } function customer_info($user_name) { global $mysqli; //No sense of unsetting mysqli variable $query_select = ("SELECT customer_number, customer_name, customer_legal_sort, custom_var_applicable, ... FROM customers where user_name = ? "); $stmt = $mysqli->prepare($query_select); $stmt->bind_param("s", $user_name); $stmt->execute(); $stmt->store_result(); $count = $stmt->num_rows(); // You must specify what columns are you selecting to fetch them, or it won't work $stmt->bind_result ($customer_number , $user_name , $customer_name , $customer_legal_sort , $customer_vat_applicable , $customer_payment_terms , $customer_contactperson , $customer_email , $customer_telphone , $customer_address_visit , $customer_number_visit , $customer_num_add_visit , $customer_postal_visit , $customer_city_visit , $customer_country_visit , $customer_visit_vs_post , $customer_address_post , $customer_number_post , $customer_num_add_post , $customer_postal_post , $customer_city_post , $customer_country_post ); while($stmt->fetch()) { $customer_data = array ( 'customer_number' =>$customer_number , 'user_name' =>$user_name , 'customer_name' =>$customer_name , 'customer_legal_sort' =>$customer_legal_sort , 'customer_vat_applicable'=>$customer_vat_applicable , 'customer_payment_terms '=>$customer_payment_terms , 'customer_contactperson' =>$customer_contactperson , 'customer_email' =>$customer_email , 'customer_telphone' =>$customer_telphone , 'customer_address_visit' =>$customer_address_visit , 'customer_number_visit' =>$customer_number_visit , 'customer_num_add_visit' =>$customer_num_add_visit , 'customer_postal_visit' =>$customer_postal_visit , 'customer_city_visit' =>$customer_city_visit , 'customer_country_visit' =>$customer_country_visit , 'customer_visit_vs_post' =>$customer_visit_vs_post , 'customer_address_post' =>$customer_address_post , 'customer_number_post' =>$customer_number_post , 'customer_num_add_post' =>$customer_num_add_post , 'customer_postal_post' =>$customer_postal_post , 'customer_city_post' =>$customer_city_post , 'customer_country_post' =>$customer_country_post ); } $stmt->free_result(); return $customer_data; } ,看看是否有效。 无论如何,我建议你总是打印mysqli错误,看看哪些不起作用。

$mysqli

您不需要在函数中包含PHP文件,只能在文件开头包含一次,只需记住将global变量称为SQL INSERT(尽可能看到)。 在插入查询后你存储结果,但它没有任何意义,因为你不需要从store_result()获取任何数据,可能只是插入的行数或只是为了检查语句是否有完成,但是,这两个范围中的每一个都不需要customer_info

另一件事,在SQL SELECT函数中,您绑定了bind_result()的结果,该结果返回表中的所有列。要使用SELECT col1,col2,col3 FROM tablename WHERE col1=? 函数,您需要指定您需要哪些columuns,因此您将使用这样的SELECT:

$stmt->bind_result(col1,col2,col3);

然后:

mysqli_error

P.S:也许你应该尝试查看你的陈述所引发的确切undefined,那些mysqli错误将永远不会帮助那么多。 最后一件事,你执行了很多mysqli_execute函数而没有检查任何东西,比如{{1}}是否有效,或类似的东西。这可以为您提供更多帮助,而不是像链条那样使用所有功能

答案 1 :(得分:0)

回答你的问题:是的,可以先将一行INSERT到一个表然后用SELECT检索它。我认为,您的脚本失败是因为您在INSERT之后关闭了mysql连接。

还有一些事情需要考虑: