0

Getting this error message when clicking the submit button to send data to MySQL database:

Fatal error: Uncaught Error: Call to a member function bind_param() on bool in C:\xampp\htdocs\weinspire\blog\index.php:38 Stack trace: #0 {main} thrown in C:\xampp\htdocs\weinspire\blog\index.php on line 38

This is the code I'm using:

$table = 'suggestions';
$id = $_SESSION['id'];
$optionOne = '';
$optionTwo = '';

$suggestions = selectAll($table);

if (isset($_POST['new-suggestion'])) {
  global $conn;
  $optionOne = $_POST['optionOne'];
  $optionTwo = $_POST['optionTwo'];
  $sql = "INSERT INTO $table (option_1, option_2) VALUES (:optionOne, :optionTwo)";

  if (!empty($optionOne) && !empty($optionTwo)) {
    $stmt = $conn->prepare($sql);
    $stmt->bind_param('optionOne', $optionOne);
    $stmt->bind_param('optionTwo', $optionTwo);
    $stmt->execute();

  } else {
    echo "All options must be entered";
  }

}

      <form class="suggestion-form" action="index.php" method="POST">
        <h2>Let us suggest your next travel destination...</h2><br>
        <p>Choose from the options and we'll give you ideas for your next trip!</p>
        <p>I love
          <select id="select-one" class="suggestion-dropbox" name="optionOne" onchange="change(event)">
            <option id="views" value="views">stunning views</option>
            <option id="beaches" value="beaches">glorious white beaches</option>
          </select>
        </p>
        <p>I haven't ever been to
          <select class="suggestion-dropbox" name="optionTwo" onchange="placeChange(event)">
            <option value="europe">Europe</option>
            <option value="australia">Australia</option>
          </select>
        before...</p>
          <p>Submit to see our suggestions!</p>
          <button type="submit" name="new-suggestion">Submit</button>

Not too sure where I've gone wrong. Thanks

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
ARick12
  • 35
  • 5
  • 4
    it looks like PDO but you are using a `mysqli` method (`bind_param`) - for PDO you would use `bindParam` – Professor Abronsius Jul 08 '20 at 10:48
  • I've changed that thanks, but the error message still comes up, it's just changed the method in the error message – ARick12 Jul 08 '20 at 10:51
  • 1
    If you're using mysqli, then you can't have named parameters `:optionOne` (unsupported) and need to use `?` instead. That would explain why your prepare fails and gives you the error you're getting. Please show us where and how you define `$conn` (the database connection) so we can see what database extension you're using. – M. Eriksson Jul 08 '20 at 10:51
  • 1
    can you add the db connection code - with sensitive data redacted – Professor Abronsius Jul 08 '20 at 10:52
  • All that error message is telling you is that the prepare() function failed in some way (because prepare() returns false (a boolean, as the error says!) when it fails, and clearly you can't call a function on a boolean). You need to find out **why** it fails. You can set mysqli (or PDO if that's what you're using) up to throw exceptions when things go wrong. Here are 2 setup guides for that (depending on which library you're using): https://stackoverflow.com/a/14578644/5947043 (mysqli exception handling) https://www.php.net/manual/en/pdo.error-handling.php (PDO exception handling) – ADyson Jul 08 '20 at 10:56
  • Once you've done that you should see a more detailed error message appearing specifying what the SQL issue was. – ADyson Jul 08 '20 at 10:56
  • Parameter 1 for `bind_param` is the data type, not the binding name. Names are not allowed in `mysqli` (in object oriented style, which is being used here). – user3783243 Jul 08 '20 at 10:57
  • $host = 'localhost'; $user = 'root'; $pass = ''; $db_name = 'blog'; $conn = new MySQLi($host, $user, $pass, $db_name); – ARick12 Jul 08 '20 at 10:58
  • ok so that clearly means you're using mysqli. https://phpdelusions.net/mysqli contains good clear examples of using prepared statements and parameters with mysqli. I suggest you study the examples carefully, and also check the mysqli manual pages. One obvious problem from your code is that you can't use named parameters in mysqli, you have to use `?` placeholders. Another is you're using bind_param incorrectly. I suspect you've seen some tutorials for PDO and got confused into thinking you can use the same syntax with mysqli. They are different libraries with different functions and syntax. – ADyson Jul 08 '20 at 10:59
  • Nice one, I'll take a look at that now, cheers – ARick12 Jul 08 '20 at 11:02

0 Answers0