16

My html :

 <form action="rent.php" method="post"><pre>
        Email : <input  type="text" name="email">
        Message : <input type="text" name="msg_text">
                <input type="submit" value="Rent it">
    </pre></form>

My rent.php file :

<?php
 require_once 'login.php';
   $conn = new mysqli($hn, $un, $pw, $db);
   if ($conn->connect_error) {
    die($conn->connect_error);
}
    $query = "SET NAMES utf8";
    $result = $conn->query($query);
    if (!$result) {
        die($conn->error);
    }

    $req = $conn->prepare('INSET INTO renter (email, msg_text) VALUES(?, ?)');
    $req->execute(array($_POST['email'], $_POST['msg_text']));

    header('Location: menu.php');

My error when I try to submit, is : Fatal error: Call to a member function execute() on boolean in C:...\rent.php on line 18

email, msg_text are in varchar type

Saty
  • 22,443
  • 7
  • 33
  • 51
idkn
  • 422
  • 2
  • 7
  • 13

5 Answers5

24

mysqli->prepare can also return FALSE (check http://php.net/manual/en/mysqli.prepare.php) if an error occurred. Your problem is that you have INSET instead of INSERT.

tomas.lang
  • 489
  • 3
  • 10
8

This may help someone: I was facing same issue. In my case, i have missed to close first prepared statement before executing second prepared statement.

When i have added $select_stmt_type->close(); before executing second prepare statement, it fixed the issue.

Dhaval Bhimani
  • 980
  • 1
  • 13
  • 24
5

Just to add. Similar error comes when the mysql column name is incorrect in php. I found below error for my condition when i printed the $conn->error.

errno: 1054, error: Unknown column 'xyz' in 'field list'

Vaibs
  • 2,018
  • 22
  • 29
1

To catch errors do something like this

    $req = $conn->prepare('INSERT INTO renter (email, msg_text) VALUES(?, ?)');
    if(!$req){
       echo "Prepare failed: (". $conn->errno.") ".$conn->error."<br>";
    }

since prepare returns a boolean. Please use when debugging you code and refer to error reporting as stated in the comment.

Thagana
  • 27
  • 3
  • 1
    Please don't. Echoing the error message right away is wrong on so many levels. Please kindly read my [PHP error reporting](https://phpdelusions.net/articles/error_reporting) – Your Common Sense Sep 08 '19 at 13:46
0

Your SQL prepared statement needs to be corrected as shudent said, but I think you also have to set the values with ->bind_param() before calling ->execute() (which does not take any parameters).

$stmt = $conn->prepare('INSERT INTO renter (email, msg_text) VALUES(?, ?)');
$stmt->bind_param("ss", $_POST['email'], $_POST['msg_text']);
$stmt->execute();

See documentation and example on the PHP official site : http://php.net/manual/fr/mysqli.prepare.php

Patrick Janser
  • 3,318
  • 1
  • 16
  • 18