I want to run the following query, but I’m trying to switch to a prepared statement. This is the query
INSERT INTO SPAM_Accounts (Email, Password, Provider, About) VALUES (‘$_POST[Email]’, ‘$_POST[Password]’, ‘$_POST[Provider]’, ‘$_POST[About]’)
I followed w3schools‘s tutorial and I wrote the following code:
<?php $servername = "localhost"; $username = "xxx"; $password = "xxx"; mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); $mysqli = new mysqli($servername, $username, $password); $mysqli->set_charset('utf8mb4'); $stmt = $mysqli->prepare("INSERT INTO SPAM_Accounts (Email, Password, Provider, About) VALUES (?, ?, ?, ?)"); $stmt->bind_param("ssss", $Email_Value, $Password_Value, $Provider_Value, $About_Value); $Email_Value = $_POST[Email]; $Password_Value = $_POST[Password]; $Provider_Value = $_POST[Provider]; $About_Value = $_POST[About]; $stmt->execute(); echo "New records created successfully"; $stmt->close(); $con->close(); ?>
Well, I tried it, and I got this error:
Fatal error: Uncaught exception ‘mysqli_sql_exception’ with message ‘Access denied for user ‘id14748631_n1coclouds’@’%’ to database ‘information_schema” in /storage/ssd3/631/14748631/public_html/spam-account/insert.php:27
Stack trace: #0 /storage/ssd3/631/14748631/public_html/spam-account/insert.php(27): mysqli->prepare(‘INSERT INTO SPA…’)
#1 {main} thrown in /storage/ssd3/631/14748631/public_html/spam-account/insert.php on line 27
I checked the line 27 and this is the code:
$stmt = $mysqli->prepare(“INSERT INTO SPAM_Accounts (Email, Password, Provider, About) VALUES (?, ?, ?, ?)”);
What I am doing wrong? I’m learning PHP and MySQL, so a good explained answer will help me understand the problem.
Problem I think I have: I made a new PHP file, and I wrote a "normal" code. This is the code: <?php $servername = "localhost"; $username = "xxx"; $password = "xxx"; $con = mysql_connect($servername,$username,$password); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("id14748631_clouds", $con); $sql="INSERT INTO SPAM_Accounts (Email, Password, Provider, About) VALUES ('$_POST[Email]', '$_POST[Password]', '$_POST[Provider]', '$_POST[About]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } mysql_close($con); ?>
Notice: Undefined index: About in /storage/ssd3/631/14748631/public_html/spam-account/insert.php on line 93
And on the line 93 is the following code:
(‘$_POST[Email]’, ‘$_POST[Password]’, ‘$_POST[Provider]’, ‘$_POST[About]’)”;
Advertisement
Answer
Well I found the solution. All I had to do was to change from
$_POST[idk]
to
$_POST[‘idk’]
Credits: Taavi Tiitsu
Then I had to delete the last 3 lines, in my case
$stmt->close(); $con->close(); ?>
You only should close PHP if you are going to write some HTML immediately after. Other than that your PHP files should never end in
Credits: Dharman
And the last mistake was not adding the fourth parameter inside connection function (database name).
$mysqli = new mysqli($servername, $username, $password, $dbname);
You forgot to set the fourth parameter in new mysqli.
That page you linked has a $dbname variable which you are missing in