Fatal error: Uncaught Error: Call to a member function


#1

Fatal error : Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\carbooking3\16 y\korak1.php:21 Stack trace: #0 {main} thrown i

$stmt = $mysqli->prepare("INSERT INTO  cars321 VALUES (?, ?, ?, ?, ?)");
$stmt->bind_param('sssss', $start_date, $end_date, $cars, $Location_of_renting,$Location_of_returning);

How to fix this


#2

Well if $stmt is a bool then prepare has returned a boolean. Let’s see it there is something in the manual on that.

OK, it says on https://secure.php.net/manual/en/mysqli.prepare.php:

mysqli_prepare() returns a statement object or FALSE if an error occurred.

Okay, so false was returned meaning an error occurred.

I can think of two things:

  • you’re not connected to MySQL
  • the query is invalid, which can happen in this case if the table has more columns than you have ? in your query

#3

this is db

-- Table structure for table `cars321`
--

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(1) NOT NULL,
  `price` int(11) NOT NULL,
  `numberdays` int(11) NOT NULL,
  `Numbers_of_cars` int(11) NOT NULL,
  `kindofcar` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


#4

Spelling of your table column versus in your insert statement


#5

@capljina2 So you are trying to insert 5 values into a table with 13 columns (most of which are defined as NOT NULL). How exactly do you think mysql is going to know which columns get which values? And while a bit off-topic, what values should the NOT NULL columns receive?

INSERT INTO table (col1,col2) VALUES (?,?);

https://dev.mysql.com/doc/refman/8.0/en/insert.html

And one final, completely unasked for, hint: start small and work you way up. Maybe use a table with say two columns until you master the basics.


#6
$stmt = $mysqli->prepare("INSERT INTO  cars321 VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param('sssssss', $start_date, $end_date, $cars, $Location_of_renting,$Location_of_returning,$vr_uzi,$vr_vraca);	 

I HAVE 7 COLUMNS HERE

AND 7 COLUMNS IIN DBCREATE 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
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

STILL HAS EN ERROR
Fatal error : Uncaught mysqli_sql_exception: Column count doesn’t match value count at row 1 in C:\xampp\htdocs\carbooking3\16 y\korak1.php:27 Stack trace: #0 C:\xampp\htdocs\carbooking3\16 y\korak1.php(27): mysqli->prepare(‘INSERT INTO ca…’) #1 {main} thrown in C:\xampp\htdocs\carbooking3\16 y\korak1.php on line 27


#7

Your table has 8 columns, not 7. You haven’t supplied a value for the id column.


#8
$statement = $link->prepare("SELECT start_date, end_date FROM cars321 WHERE cars = ? AND start_date < ? AND end_date > ? AND paid = 1;");
$statement->bind_param('sss', $carsCount, $end_date, $start_date);/////////////right here is problem

Fatal error : Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\carbooking3\16 y\korak22.php:467 Stack trace: #0 {main} thrown in C:\xampp\htdocs\carbooking3\16 y\korak22.php on line 467


#9

You don’t have a column named paid


#10

Fatal error : Uncaught mysqli_sql_exception: Column count doesn’t match value count at row 1 in C:\xampp\htdocs\carbooking3\16 y\korak1.php:27 Stack trace: #0 C:\xampp\htdocs\carbooking3\16 y\korak1.php(27): mysqli->prepare(‘INSERT INTO ca…’) #1 {main} thrown in C:\xampp\htdocs\carbooking3\16 y\korak1.php on line 27
Added column paid and have en error paid should be update on the end of program


#11

now i have this Fatal error : Uncaught mysqli_sql_exception: Column count doesn’t match value count at row 1 in C:\xampp\htdocs\carbooking3\16 y\korak1.php:27 Stack trace: #0 C:\xampp\htdocs\carbooking3\16 y\korak1.php(27): mysqli->prepare(‘INSERT INTO ca…’) #1 {main} thrown in C:\xampp\htdocs\carbooking3\16 y\korak1.php on line 27


#13

Show us the code that is throwing this error.


#14
$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('sssssssss', $id,$start_date, $end_date, $cars, $Location_of_renting,$Location_of_returning,$vr_uzi,$vr_vraca,$paid);	 

$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';
/* 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 */
$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;

Fatal error : Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\carbooking3\16 y\korak1.php:28 Stack trace: #0 {main} thrown in C:\xampp\htdocs\carbooking3\16 y\korak1.php on line 28
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
$stmt->bind_param('sssssssss', $id,$start_date, $end_date, $cars, $Location_of_renting,$Location_of_returning,$vr_uzi,$vr_vraca,$paid); line **28**


#15

(I really meant show the code that throws the error and a bit of related, not the entire thing).

Is it correct to specify the “id” field using an “s” marker when you’ve defined it in the table as an integer? Surely you should pass it as an integer. I can’t see where you define $id anywhere, though.

As mentioned before, though, if you’re not going to specify the column names that you’re inserting values into, then you have to give values for every column. You have 12 columns in the table, and 9 values in the query. You’ve said that the columns cannot be null, you haven’t specified a value for them, what can MySQL do other than reject the query?


#16

cant go on next page


#17

Sorry, no idea what you mean by that comment.


#18

$insertGoTo = ‘korak22.php?id=’;


#19

korak1.php I am here cant go on $insertGoTo = ‘korak22.php?id=’;


#20

the code was in mysql i am changing in mysqli how to go on next page in mysqli.


#21

What does this have to do with the error message you posted in post #14? Or have you fixed that one now.

Or do you mean that you pass the value for $id in as part of the URL? If that’s it, you need to extract it from the $_GET array before you use it. But I still don’t think it should be a string.