0

Undefined $conn variable

require 'config.php'; if (isset($_POST["submit"])) { $PLAYERID = NULL; $PLAYERNAME = $_POST ["playerName"]; $stmt = $conn->prepare("INSERT INTO PLAYER (:PLAYERID, :PLAYERNAME) VALUES (?, ?)"); $stmt->execute(["PLAYERID" => $PLAYERID, "PLAYERNAME" => $PLAYERNAME]); echo "Player added!"; echo "<a href='game.html'> Ga naar spel </a>"; } What am I doing wrong? This is a function to insert data by the way how do I define $conn please help me out

5th Apr 2021, 1:21 AM
7 RTG
9 Antworten
+ 1
I'm trying to submit via a html form but this is an error I get when I put a var dump it shows no errors everything is correct
5th Apr 2021, 12:00 PM
7 RTG
0
A variable must be assigned with a value. If the variable had a value unassigned, then this will display an error.
5th Apr 2021, 1:35 AM
Dino Wun (Use the search bar plz!)
Dino Wun (Use the search bar plz!) - avatar
0
$conn is undefined here..is it exist inside config.php?
5th Apr 2021, 1:39 AM
durian
durian - avatar
0
Yes it is defined in the config.php that's my db connection
5th Apr 2021, 11:37 AM
7 RTG
0
is this ur full code?and can u share code for config.php ?
5th Apr 2021, 11:41 AM
durian
durian - avatar
0
Yes of course $servername = "localhost"; $dbname = "HANGMAN"; $username = "root"; $password = "root"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password); $conn->SetAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // echo "Connectie gelukt <br />"; } catch (PDOException $e) { echo "connectie mislukt: " . $e->getMessage(); }
5th Apr 2021, 11:44 AM
7 RTG
0
seems fine.
5th Apr 2021, 11:47 AM
durian
durian - avatar
0
is ur code that u are asking inside a function?
5th Apr 2021, 11:48 AM
durian
durian - avatar
0
if the code is nested inside a function, then declare global $conn; first, then try to use it..
5th Apr 2021, 12:16 PM
durian
durian - avatar