Cannot find the answer after my research, help me with mysql [duplicate]

52
May 26, 2022, at 1:20 PM
This question already has answers here:
How to make mysqli throw exceptions using MYSQLI_REPORT_STRICT? [duplicate] (2 answers)
Closed 4 months ago.

The Error is:

Fatal error: Uncaught mysqli_sql_exception: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ':rafli_namaUser, :rafli_username, :rafli_password)' at line 2 in C:\xampp\htdocs\rafli_kasir\rafli_register.php:16 Stack trace: #0 C:\xampp\htdocs\rafli_kasir\rafli_register.php(16): mysqli->prepare('INSERT INTO raf...') #1 {main} thrown in C:\xampp\htdocs\rafli_kasir\rafli_register.php on line 16

cannot find any solution after research, this is my code:

<?php
require_once("rafli_koneksi.php");
if(isset($_POST['rafli_register'])){
    $rafli_namaUser = filter_input(INPUT_POST, 'rafli_namaUser', FILTER_SANITIZE_STRING);
    $rafli_username = filter_input(INPUT_POST, 'rafli_username', FILTER_SANITIZE_STRING);
    $rafli_password = password_hash($_POST["rafli_password"], PASSWORD_DEFAULT);
    $sql = "INSERT INTO rafli_user (rafli_namaUser, rafli_username, rafli_password) 
            VALUES (:rafli_namaUser, :rafli_username, :rafli_password)";
    $stmt = $rafli_conn->prepare($sql);
    $params = array(
        ":rafli_namaUser" => $rafli_namaUser,
        ":rafli_username" => $rafli_username,
        ":rafli_password" => $rafli_password
    );
    $saved = $stmt->execute($params);
    if($saved) header("Location: rafli_login.php");
}
?>

rafli_koneksi.php

<?php
$rafli_host = "localhost";
$rafli_username = "root";
$rafli_password = "";
$rafli_database = "rafli_kasir";
$rafli_conn = new mysqli( $rafli_host, $rafli_username, $rafli_password, $rafli_database );
    echo 'Gagal terhubung ke database';
} else {
    // Koneksi berhasil
}

?>

Answer 1

Did you forget to define $rafli_conn in this piece of code?

bool means Boolean, i.e. true or false.

You set $stmt = $rafli_conn->prepare($sql); and later it says that you cannot call ->execute() on a bool — so there is a high likelihood that $stmt is returning false.

Can you print_r($stmt) and test what its value is? Once you have that working, you should be fine.

READ ALSO
Escaping the separator in str_getcsv

Escaping the separator in str_getcsv

str_getcsv("test\\a

91
Retrieve results only where ALL keywords match Laravel

Retrieve results only where ALL keywords match Laravel

I am trying to pull users based on their tags where ALL keywords match

54
Getting an PHP enum from dynamic name

Getting an PHP enum from dynamic name

I´m trying to create an php 81 enum from a dynamic name

44
PHP categories showing only one item

PHP categories showing only one item

I'm trying to create a categories list, where user will be able to choose item with certain category, but as soon as I'm opening any of the categories, where "soon = '2'", there is only one item coming upEverything else works

72