B
B
BoriHagen2020-04-26 15:18:22
PHP
BoriHagen, 2020-04-26 15:18:22

Why does mysqli_query() get NULL as first parameter?

database.php :

$ip = "127.0.0.1";
$user = "root";
$password = "";
$db = "crm";

$connection = mysqli_connect($id, $user, $password, $db);
global $connection;
if ($connection == false) {
  echo "Произошла ошибка";
}


saveOrder.php :

include "database.php";
function saveOrder (){
  mysqli_query($connection, "UPDATE `orders` `order_status`=".$_POST['orderStatus'].",`customer_surname`=".$_POST['customerSurname'].",`customer_name`=".$_POST['customerName'].",`customer_patronymic`=".$_POST['customerPatronymic'].",`email`=".$_POST['email'].",`phone_number`=".$_POST['phoneNumber'].",`second_phone_number`=".$_POST['secondPhoneNumber'].",`address`=".$_POST['address']." WHERE `order_number`=".$order_number);
  mysqli_query($connection, "UPDATE `order_items` SET `item1` = ".$_POST['vendorCode'][0].", `item2` = ".$_POST['vendorCode'][1].", `item3` = ".$_POST['vendorCode'][2].", `item4` = ".$_POST['vendorCode'][3].", `item5` = ".$_POST['vendorCode'][4].", `item6` = ".$_POST['vendorCode'][5].", `item7` = ".$_POST['vendorCode'][6].", `item8` = ".$_POST['vendorCode'][7].", `item9` = ".$_POST['vendorCode'][8].", `item10` = ".$_POST['vendorCode'][9].", `item11` = ".$_POST['vendorCode'][10].", `item12` = ".$_POST['vendorCode'][11].", `item13` = ".$_POST['vendorCode'][12].", `item14` = ".$_POST['vendorCode'][13].", `item15` = ".$_POST['vendorCode'][14].", `item16` = ".$_POST['vendorCode'][15].", `item17` = ".$_POST['vendorCode'][16].", `item18` = ".$_POST['vendorCode'][17].", `item19` = ".$_POST['vendorCode'][18].", `item20` = ".$_POST['vendorCode'][19]." WHERE `order_items`.`order_number` =".$order_number);
}
saveOrder();


Gives error mysqli_query() expects parameter 1 to be mysqli, null given

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Sisyukin, 2020-04-26
@BoriHagen

Global $connection; - in your function
It's better not to write like that, get bogged down in functionality and a mess of global variables

F
FanatPHP, 2020-04-26
@FanatPHP

Remove lines

global $connection;
function saveOrder (){
}
saveOrder();

And try not to continue to write meaningless squiggles just because they look beautiful

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question