Fatal error: Uncaught Error: Call to a member function

mysqli has nothing to do with header redirects, I am not sure what you mean. Is the query that was giving you an error now working?

no error

Ah, OK, so you’re trying to sort something different out now? That would be good to mention. Can you explain in a bit more detail what problem you are having now, with the failing code?

1 Like

after insert data in db cant on next page every time refreshing page new data go in db.

There is no redirect in your code.

Also, bind_values is better than bind_params as it’s easy to create bugs with bind_params

$mysqli = new mysqli('localhost', 'root', '', 'cars123'); 

/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 
// prepare and bind 
/*<?php ?> $start_date=$_POST['start_date']; 
$end_date=$_POST['end_date']; 
$cars=$_POST['cars']; 
$Location_of_renting=$_POST['Location_of_renting']; 
$Location_of_returning=$_POST['Location_of_returning']; 
<?php ?>*/ 


global $connect; 
$stmt = $mysqli->prepare("INSERT INTO  cars321 VALUES (?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 
$stmt->bind_param('ssssssssssss', $id,$start_date, $end_date, $cars, $Location_of_renting,$Location_of_returning,$vr_uzi,$vr_vraca,$paid,$price ,$numberdays    ,$Numbers_of_cars);      

$start_date = '2018-09-20'; 
$end_date= '2018-09-21'; 
$cars = "1"; 
$Location_of_renting= '2018-09-16 13:50:27 
'; 
$Location_of_returning= '2018-09-16 13:50:29 
'; 
$vr_uzi= 'mostar'; 
$vr_vraca= 'modstar'; 
$paid= '1'; 
$price= '111'; 
$numberdays= '11'; 
$Numbers_of_cars= '1'; 


/* execute prepared statement */ 
$stmt->execute(); 

printf("%d Row inserted.\n", $stmt->affected_rows); 

/* close statement and connection */ 
$stmt->close(); 

/* Clean up table CountryLanguage */ 


/* close connection */ 

  
  
  
  
  $get_max_id =$link->prepare('SELECT start_date, end_date, cars FROM cars321 WHERE id = (SELECT MAX(ID) FROM cars321);');   
  echo 'Error: ' . mysqli_error($link); 
   
   
  $query="UPDATE cars321  SET vr_uzi=NOW() WHERE vr_uzi=0000-00-00 00:00:00"; 
   $query="UPDATE cars321  SET vr_vraca=NOW() WHERE vr_vraca=0000-00-00 00:00:00";     
   
   
  $get_max_id->execute(); 
  
  $get_max_id->fetch(); 
  $get_max_id->close(); 
  $insertGoTo = 'korak22.php?id='; 
/* 
  if (isset($_SERVER['QUERY_STRING'])) { 
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; 
    $insertGoTo .= $_SERVER['QUERY_STRING']; 
  } 
*/ 
 $mysqli->close();
```CREATE TABLE `cars321` ( 
  `id` int(11) NOT NULL, 
  `start_date` date NOT NULL, 
  `end_date` date NOT NULL, 
  `cars` int(11) NOT NULL, 
  `Location_of_renting` text NOT NULL, 
  `Location_of_returning` text NOT NULL, 
  `vr_uzi` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
  `vr_vraca` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, 
  `paid` int(11) NOT NULL DEFAULT '1', 
  `price` int(11) NOT NULL, 
  `numberdays` int(11) NOT NULL, 
  `Numbers_of_cars` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1  

cant insert in db also go on next page:thinking:

<button id=“btn_room_search” action=“http://localhost/carbooking3/16%20y/korak22.php"class="sear-btn” type=“submit”>Search

with this code i have on next page but cant insert in db

               <button id="btn_room_search" class="sear-btn" type="submit"><a href="http://localhost/carbooking3/16%20y/korak22.php?id=">Search</a></button>

just one insert could do after that nothing

$db = new PDO("mysql:host=localhost;dbname=$cars123", "", "");
//Add session_start to top of each page//
}



if (isset($_POST['btn_room_search'])){
	$query = $db->prepare("INSERT INTO cars321 (id,start_date,end_date,cars,Location_of_renting,Location_of_returning,vr_uzi,vr_vraca,paid,price,numberdays,Numbers_of_cars) 
	
	
	
	VALUES(:id,:start_date,:end_date,:cars,:Location_of_renting,:Location_of_renting,:vr_uzi,:vr_vraca,:paid,:price,:numberdays,:Numbers_of_cars))");
	$query->bindParam(":id", $_POST['id']);
	$query->bindParam(":start_date", $_POST['start_date']);
	$query->bindParam(":end_date", $_POST['end_date']);
	$query->bindParam(":cars", $_POST['cars']);
		$query->bindParam(":Location_of_renting", $_POST['Location_of_renting']);
	$query->bindParam(":Location_of_returning", $_POST['Location_of_returning']);
	$query->bindParam(":vr_uzi", $_POST['vr_uzi']);
	$query->bindParam(":vr_vraca", $_POST['vr_vraca']);
	
	$query->bindParam(":paid", $_POST['paid']);
	$query->bindParam(":price", $_POST['price']);
	$query->bindParam(":numberdays", $_POST['numberdays']);
	$query->bindParam(":Numbers_of_cars", $_POST['Numbers_of_cars']);
	
	
	$query->execute();
}

i tried like that no inserting

VALUES(:id,:start_date,:end_date,:cars,:Location_of_renting,:Location_of_renting,:vr_uzi,:vr_vraca,:paid,:price,:numberdays,:Numbers_of_cars))");

Typo in this part: You have used the same parameter name twice - you can’t do that, even if you wanted to insert the same value twice…

$mysqli = new mysqli('localhost', 'root', '', 'cars123'); 

/* check connection */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 
// prepare and bind 
/*<?php ?> $start_date=$_POST['start_date']; 
$end_date=$_POST['end_date']; 
$cars=$_POST['cars']; 
$Location_of_renting=$_POST['Location_of_renting']; 
$Location_of_returning=$_POST['Location_of_returning']; 
<?php ?>*/ 


global $connect; 
$stmt = $mysqli->prepare("INSERT INTO  cars321 VALUES (?,?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"); 
$stmt->bind_param('ssssssssssss', $id,$start_date, $end_date, $cars, $Location_of_renting,$Location_of_returning,$vr_uzi,$vr_vraca,$paid,$price ,$numberdays    ,$Numbers_of_cars);      

$start_date = '2018-09-20'; 
$end_date= '2018-09-21'; 
$cars = "1"; 
$Location_of_renting= '2018-09-16 13:50:27 
'; 
$Location_of_returning= '2018-09-16 13:50:29 
'; 
$vr_uzi= 'mostar'; 
$vr_vraca= 'modstar'; 
$paid= '1'; 
$price= '111'; 
$numberdays= '11'; 
$Numbers_of_cars= '1'; 


/* execute prepared statement */ 
$stmt->execute(); 

printf("%d Row inserted.\n", $stmt->affected_rows); 

/* close statement and connection */ 
$stmt->close(); 

/* Clean up table CountryLanguage */ 


/* close connection */ 

  
  
  
  
  $get_max_id =$link->prepare('SELECT start_date, end_date, cars FROM cars321 WHERE id = (SELECT MAX(ID) FROM cars321);');   
  echo 'Error: ' . mysqli_error($link); 
   
   
  $query="UPDATE cars321  SET vr_uzi=NOW() WHERE vr_uzi=0000-00-00 00:00:00"; 
   $query="UPDATE cars321  SET vr_vraca=NOW() WHERE vr_vraca=0000-00-00 00:00:00";     
   
   
  $get_max_id->execute(); 
  
  $get_max_id->fetch(); 
  $get_max_id->close(); 
  $insertGoTo = 'korak22.php?id='; 
/* 
  if (isset($_SERVER['QUERY_STRING'])) { 
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?"; 
    $insertGoTo .= $_SERVER['QUERY_STRING']; 
  } 
*/ 
 $mysqli->close();
```CREATE TABLE `cars321` ( 
  `id` int(11) NOT NULL, 
  `start_date` date NOT NULL, 
  `end_date` date NOT NULL, 
  `cars` int(11) NOT NULL, 
  `Location_of_renting` text NOT NULL, 
  `Location_of_returning` text NOT NULL, 
  `vr_uzi` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, 
  `vr_vraca` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00' ON UPDATE CURRENT_TIMESTAMP, 
  `paid` int(11) NOT NULL DEFAULT '1', 
  `price` int(11) NOT NULL, 
  `numberdays` int(11) NOT NULL, 
  `Numbers_of_cars` int(11) NOT NULL 
) ENGINE=InnoDB DEFAULT CHARSET=latin1

cant insert in db what about this one

Is that going to work, with a href inside a button definition? The button would submit the form, but if it’s processing the href instead of the button, then it’s just going to open korak.php with no value on the id parameter as a page, and not send the form contents. That’s a HTML thing really - do you get values in $_POST in your PHP code?

no i am not

if (isset($_POST[‘submit’])) {

added this and got an error


Notice : Undefined variable: mysqli in C:\xampp\htdocs\carbooking3\16 y\korak1.php on line 248
Fatal error : Uncaught Error: Call to a member function close() on null in C:\xampp\htdocs\carbooking3\16 y\korak1.php:248 Stack trace: #0 {main} thrown in C:\xampp\htdocs\carbooking3\16 y\korak1.php on line 248

What’s on line 248 of that file?

If that’s an answer to whether you are getting anything in the $_POST array, and you are not, then I would suggest you remove the <a href> part of the button code, and specify the action parameter in your opening form tag instead.

$mysqli->close();

If that’s generating the error message, then it suggests that it didn’t open in the first place. That suggests that your code to open the database connection is inside your if(), but your code to close the database is outside it. You should have both either inside or outside, so either they both execute, or neither does.

i am not sure what to do