我想在表中插入一些数据,然后直接从该表中检索所有数据。但奇怪的是,当我这样做时,我收到了错误。
所以我的问题是,是否可以构建一个通过 $_POST 插入一些数据的函数,然后直接从表中检索所有数据,包括新插入的 $_POST 数据?
错误:
Notice: Undefined variable: mysqli in /mnt/webr/c3/76/54476376/htdocs/includes/functions.php on line 293 Fatal error: Call to a member function prepare() on null in /mnt/webr/c3/76/54476376/htdocs/includes/functions.php on line 293
/**********************************************************************************************************************************
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;
}
最佳答案
我认为问题是你不能执行多个 mysqli_execute
而不释放每个结果的结果。
也许您应该尝试使用 $stmt->free_result()
而不是 $stmt->close()
,看看是否有效。
不管怎样,我建议你总是打印 mysqli 错误来看看什么没有真正起作用。
<?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;
}
您不需要将 PHP 文件包含在函数中,只能在文件开头包含一次,只需记住将 $mysqli
变量调用为 global
(如您所见)。
插入查询后,您存储了结果,但这没有任何意义,因为您不需要从SQL INSERT
获取任何数据,可能只是一些插入的行或只是为了检查如果语句已完成,但是,这两个作用域中的每一个都不需要 store_result()
。
另一件事,在您的 customer_info
函数中,您绑定(bind)来自 SQL SELECT
的结果,该结果返回表中的所有列。要使用 bind_result()
函数,您需要指定需要哪些列,因此您将使用如下所示的 SELECT:
SELECT col1,col2,col3 FROM tablename WHERE col1=?
然后:
$stmt->bind_result(col1,col2,col3);
P.S:也许您应该尝试查看您的语句抛出的确切mysqli_error
,那些未定义
错误永远不会有太大帮助。
最后一件事,您正在执行大量 mysqli 函数而不检查任何内容,例如 mysqli_execute 是否有效,或类似的内容。这可以为您提供更多帮助,而不是像链一样使用所有功能
关于PHP、MYSQL --> 在 INSERT INTO 语句后直接选择 *,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29063216/