Insert into only a few column (PHP prepared statement)

Hello folks!

I have a table with several columns. For step one, when the registration happens, I want the user fill only just a few of them like ‘city, phone, email, password’.
I set most of my sql table columns to NULL (except the primary key) and the rest of them I gave default values but I’m keep having the following error message:

Fatal error : Uncaught Error: Call to undefined method PDOStatement::bind_param() in C:\xampp\htdocs\pr_mm\eps\newReg.php:23 Stack trace: #0 {main} thrown in C:\xampp\htdocs\pr_mm\eps\newReg.php on line 23

<?php
session_start();
include '../inc/init.php';
include 'inc/signupHeader.php';

$db = pdo_connect_mysql();
if (!isset($_POST['email'], $_POST['password'])) {
	$status = "4"; // Minden mező
}
if (empty($_POST['email']) || empty($_POST['password'])) {
	$status = "3"; // Minden mező
}
if ($stmt = $con->prepare('SELECT id, password FROM locations WHERE email = ?')) {
	$stmt->bind_param('s', $_POST['email']);
	$stmt->execute();
	$stmt->store_result();
	if ($stmt->num_rows > 0) {
		$status = "2"; // Felhasználónév foglalt
	} else {
    if ($stmt = $db->prepare('INSERT INTO locations VALUES email = :email, password = :password, name = :name, l_phone = :l_phone, city = :city, address = :address')) {
	      $password = password_hash($_POST['password'], PASSWORD_DEFAULT);
        $uniqid = uniqid();
        $stmt->bind_param('ssssss', $_POST['email'], $password, $_POST['name'], $_POST['phone'], $_POST['city'], $_POST['address']);
	      $stmt->execute();
        $status = "0"; // Siker
}
	}
	$stmt->close();
} else {
	$status = "1"; // Hiba
}
$db->close();
?>

https://www.php.net/manual/en/pdostatement.bindparam.php

This method calls bindParam(), with cammel case instead of underline.

bind_param is the mysqli syntax. It won’t work in PDO.
With PDO, I much prefer to pass an array into the execute, I never use bindParam, but that’s just my preference.

The error is because you are making the database connection using the PDO extension, but all the rest of the code is based on the mysqli extension. You cannot mix statements from the two different extensions. You will either need to change the connection code to use the mysqli extension or change the rest of the code to use the PDO extension. The PDO extension is simpler and easier to use. It would also help if you named the variable holding the connection as to which extension it is, $pdo or $mysqli.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.