I always get a fatal error from my $stmt->bind_param
or $stmt->execute
, I think it should be that the connection to my database failed, yet I cant seem to find the problem.
formhandler.php:
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$nameSurname = htmlspecialchars($_POST["nameSurname"]);
$zipcodeLocation = htmlspecialchars($_POST["zipcodeLocation"]);
$streetAddress = htmlspecialchars($_POST["streetAddress"]);
$phoneNumber = htmlspecialchars($_POST["phoneNumber"]);
$email = htmlspecialchars($_POST["email"]);
if (empty($nameSurname) || empty($phoneNumber)) {
header("Location: ../index.php");
exit();
}
try {
require_once $_SERVER['DOCUMENT_ROOT'] . '/adressbuch/pages/adodbcon.php';
$query = "INSERT INTO addressbuch (nameSurname, zipcodeLocation, streetAddress, phoneNumber, email) VALUES (?, ?, ?, ?, ?);";
$stmt = $db->prepare($query);
$stmt->bind_param("sssss", $nameSurname, $zipcodeLocation, $streetAddress, $phoneNumber, $email);
$stmt->execute();
$db = null;
$stmt = null;
} catch (mysqli_sql_exception $e) {
die("MySQLi Error: " . $e->getMessage());
}
header("Location: ../index.php");
} else {
header("Location: ../index.php");
}
adodbd connection:
<?php
ini_set('display_errors', '1');
ini_set('display_startup_errors', '1');
error_reporting(E_ALL);
require_once $_SERVER['DOCUMENT_ROOT'] . '/adressbuch/lib/adodb/adodb.inc.php';
$dbhost = 'localhost';
$dbusername = '***';
$dbpassword = '***';
$dbname='***';
$db = adoNewConnection('mysqli');
$db->debug = true;
$db->connect($dbhost, $dbusername, $dbpassword, $dbname);
This is the specific error I get:
Fatal error: Uncaught Error: Call to a member function bind_param() on string in /var/www/ch/kxa/ygy/adressbuch/pages/formhandler.php:31 Stack trace: #0 {main} thrown in pages/formhandler.php on line 31
It looks as if you took some code you found online and tried to combine it with some existing code in your application. You are mixing two completely different things that do not work together. ADOdb and mysqli are not the same thing. One is a PHP library from early 2000s and the other is a native PHP extension.
If your application was written using the ADOdb library, you need to follow their way of doing things. Their documentation offers some simple guides on how to do things their way: https://adodb.org/dokuwiki/doku.php?id=v5:userguide:learn_bind:bind_vars
I don't know why prepare
returns a string, but if you did it this way it should work:
$stmt = $db->prepare($query);
$result = $db->execute($stmt, [$nameSurname, $zipcodeLocation, $streetAddress, $phoneNumber, $email]);
Please also make sure to remove htmlspecialchars
as this will damage your data that goes into the database. This function should only be used when displaying something in HTML context.
And please remove the unnecessary and redundant try-catch. If you have no plan on recovering from an exception, you shouldn't catch it.