Ich versuche, eine Insert-Anweisung über PDO mit Variablen zu erstellen, aber ich immer Fehler folgendePDO Insert-Anweisung mit Variablen
Fatal error: Uncaught Error: Call to a member function prepare() on null in /Applications/XAMPP/xamppfiles/htdocs/Jobber/user.php:9 Stack trace: #0 /Applications/XAMPP/xamppfiles/htdocs/jobber/usercheck.php(3): Users->__construct('steve', '[email protected]', 'test', 1) #1 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/Jobber/user.php on line 9
dies ist der Code, den ich für sie
mitclass Users{
private $connect ;
function __construct($name,$email,$password,$usertype){
include("connect.php");
$startDb = new Connect();
$this->connect = $startDb->getDb();
$sqlquery = $this->connect->prepare("INSERT INTO `user`(`Username`, `Email`, `Active`, `Password`, `usertype`) VALUES (" + $name + "," +$email +",0," + $password + "," + $usertype + ")");
$sqlquery->execute();
}
}
Gibt es ein Problem mit der Verbindung oder ist es etwas anderes mit den Variablen?
dies ist die Verbindungsklasse
class Connect{
private $host = "localhost:8080";
private $user = "root";
private $password = "test";
private $dbName = "jobber";
private $db;
function __construct(){
try{
$connection = new PDO('mysql:host=localhost;dbname=jobber',$this->user,$this->password);
}catch(PDOException $ex){
echo $ex;
}
}
function getDb(){
if($this->db instanceof PDO){
return $this->db;
}
}
}
Code für die 'Connect'-Klasse anzeigen. –
'$ startDb-> getDb()' gibt null zurück. Hast du das Debuggen probiert? – trincot
Wie der Fehler msg sagt, verwendet Ihr Code etwas ($ this-> connect, da dies der einzige Ort ist, an dem Sie prepare() aufrufen) als ein Klassenobjekt, das wiederum eine Methode namens prepare haben soll. Aber $ this-> connect enthält tatsächlich null. –