我想在表中插入一些数据,然后直接从该表中检索所有数据。但奇怪的是,当我这样做时,我会收到一个错误。
所以我的问题是,是否有可能构建一个函数,通过 $_POST 插入一些数据,然后直接从表中检索所有数据,包括新插入的 $_POST 数据?
错误:
注意:未定义的变量:mysqli 在/mnt/webr/c3/76/54476376/htdocs/include/functions.php第 293 行 致命错误:在第 293 行的/mnt/webr/c3/76/54476376/htdocs/include/functions.php 中调用成员函数 prepare()
/**********************************************************************************************************************************
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->close()
$stmt->free_result()
,看看它是否有效。无论如何,我建议您始终打印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
函数中,您绑定的结果来自 SQL SELECT
,该结果返回表中的所有列。要使用bind_result()
函数,您需要指定需要哪些列,因此您将使用如下所示的 SELECT:
SELECT col1,col2,col3 FROM tablename WHERE col1=?
然后:
$stmt->bind_result(col1,col2,col3);
PS:也许你应该尝试查看你的陈述抛出的确切mysqli_error
,那些undefined
错误永远不会有太大帮助。最后一件事,你在执行许多mysqli
函数而不检查任何内容,比如mysqli_execute
是否有效,或者类似的东西。这可以为您提供更多帮助,而不是像链一样使用所有功能。
来回答您的问题:是的,可以先将一行插入到表中,然后使用SELECT检索它。我认为,您的脚本失败是因为您在插入后关闭了 mysql 连接。
还需要考虑一些事项:
-
为了确保检索插入的行(并在表中定义了自动增量 ID),可以使用 http://php.net/manual/en/mysqli.insert-id.php 在 INSERT 之后获取这些 ID
-
进行更多的错误检查可能会有所帮助。下面是一个示例:PHP stmt 准备失败,但没有错误