PHP values not going to the database - php

session_start();
include('dbconn.php');
if(isset($_POST['registerform']) || isset($_POST['loginform'])) {
if(isset($_POST['registerform']) && $_POST['registerform']){
//register
if(isset($_POST['username']) && !empty($_POST['username'])
&& isset($_POST['password']) && !empty($_POST['password'])
&& isset($_POST['fname']) && !empty($_POST['fname'])
&& isset($_POST['lname']) && !empty($_POST['lname'])
&& isset($_POST['email']) && !empty($_POST['email'])){
//ean einai gemata me kati ta paidia
$myName=trim($_POST['username']);
$myPass= md5(trim($_POST['password']));
$fName=trim($_POST['fname']);
$lName=trim($_POST['lname']);
$eMail=trim($_POST['email']);
if($stmt = $mysqli->prepare("INSTERT INTO users(username,password,fname,lname,email) VALUES (?, ?, ?, ?, ?)")) {
$stmt->bind_param('sssss',$name,$pass,$fname,$lname,$email);
$name = $myName;
$pass = $myPass;
$fname = $fName;
$lname = $lName;
$email = $eMail;
$stmt-> execute();
$stmt-> close();
}
}
}
}else{
echo "no form submited";
}
and this is the db connection
<?php
$host = "localhost";
$username = "root";
$password = "";
$db_name = "phpwebsite";
$mysqli = new mysqli("$host","$username","$password","$db_name");
// Check connection
if ($mysqli->connect_error) {
die("Connection failed: " . $mysqli->connect_error);
}
?>
I think the problem is the insert statment because everything else seems to be working fine. When i press the signup button no errors appear . The only thing is that the values are not going to the database

The error is in your SQL query I think its a type INSTERT
if($stmt = $mysqli->prepare("INSTERT INTO users(username,password,fname,lname,email) VALUES (?, ?, ?, ?, ?)"))
Try correcting it to
if($stmt = $mysqli->prepare("INSERT INTO users(username,password,fname,lname,email) VALUES (?, ?, ?, ?, ?)"))
Edit :
For your second issue :
try { $stmt = $mysqli->prepare("INSERT INTO users(username,password,fname,lname,email) VALUES (?, ?, ?, ?, ?)")
$stmt->bind_param('sssss',$name,
$pass,$fname,
$lname,$email);
$name = $myName;
$pass = $myPass;
$fname = $fName;
$lname = $lName;
$email = $eMail;
$stmt-> execute();
$stmt-> close();
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
die("Failed to run query: " . $ex->getMessage());
}

Related

How do i create multiple mysql table using php?

so this is my code and it only add the data to a table called 'registration' i want it to create its own table using $firstName
<?php
$firstName = $_POST['firstName'];
$lastName = $_POST['lastName'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$password = $_POST['password'];
$number = $_POST['number'];
// Database connection
$conn = new mysqli('localhost','root','','test');
if($conn->connect_error){
echo "$conn->connect_error";
die("Connection Failed : ". $conn->connect_error);
} else {
$stmt = $conn->prepare("insert into registration(firstName, lastName, gender, email, password, number) values(?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssi", $firstName, $lastName, $gender, $email, $password, $number);
$execval = $stmt->execute();
echo $execval;
echo "Registration successfully...";
$stmt->close();
$conn->close();
}
?>

No data supplied for parameters in prepared statement - error php MySQLi [duplicate]

This question already has an answer here:
"No data supplied for parameters in prepared statement"
(1 answer)
Closed 1 year ago.
i'm very new to php. So, I tried to make a simple form to order sandwiches, but when I click the submit button i get this error "No data supplied for parameters in prepared statement".
Btw, I copied most of the code from a YouTube video, and I don't know what some parts of the code actually do.
that's my code:
<?php
if (isset($_POST['submit'])) {
if (isset($_POST['nombre']) && isset($_POST['apellido']) &&
isset($_POST['bocadillo']) && isset($_POST['extra']) &&
isset($_POST['comentario']) && isset($_POST['comentario'])) {
$nombre = $_POST['nombre'];
$apellido = $_POST['apellido'];
$bocadillo = $_POST['bocadillo'];
$extra = $_POST['extra'];
$comentario = $_POST['comentario'];
$host = "localhost";
$dbUsername = "------";
$dbpassword = "------";
$dbName = "------";
$conn = new mysqli($host, $dbUsername, $dbpassword, $dbName);
if ($conn->connect_error) {
die('Could not connect to the database.');
}
else {
$Select = "SELECT extra FROM pedidos WHERE extra = ? LIMIT 1";
$Insert = "INSERT INTO pedidos(nombre, apellido, bocadillo, extra, comentario) values(?, ?, ?, ?, ?)";
$stmt = $conn->prepare($Select);
$stmt->execute();
$stmt->bind_result($resultemail);
$stmt->store_result();
$stmt->fetch();
$rnum = $stmt->num_rows;
if ($rnum == 0) {
$stmt->close();
$stmt = $conn->prepare($Insert);
if ($stmt->execute()) {
echo "New record inserted sucessfully.";
}
else {
echo $stmt->error;
}
}
else {
echo "Someone already registers using this email.";
}
$stmt->close();
$conn->close();
}
}
else {
echo "All field are required.";
die();
}
}
else {
echo "Submit button is not set";
}
?>
You're missing the bind_param statements for both queries
$Select = "SELECT extra FROM pedidos WHERE extra = ? LIMIT 1";
$stmt = $conn->prepare($Select);
$stmt->bind_param("s", $extra);
$stmt->execute();
and then in the insert
$Insert = "INSERT INTO pedidos(nombre, apellido, bocadillo, extra, comentario) values(?, ?, ?, ?, ?)";
$stmt = $conn->prepare($Select);
$stmt->bind_param("sssss", $nombre, $apellido, $bocadillo, $extra, $comentario);
$stmt->execute();
https://www.w3schools.com/php/php_mysql_prepared_statements.asp

HTML form returns PHP script instead of success message

There is an html form which I want to store data from in a database. After filling the form, it returns the php script instead of success message. And no data is getting stored in the database. Can you please help?
<?php
if (isset(['submit'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$email = $_POST['email'];
}
if (!empty($username) || !empty($password) || !empty($gender) || !empty($email) {
$host = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbname = "cakeshop";
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if (mysqli_connect_error()) {
die('Connect Error('. mysqli_connect_error().)')'. mysqli_connect_error());
}else {
$SELECT = "SELECT email From register Where email = ? Limit 1"
$INSERT = "INSERT INTO register (username, password, gender, email) values(?, ?, ?, ?)";
$stmt = $conn->prepare($SELECT);
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($email);
$stmt->store_result();
$rnum = $stmt->num_rows;
if ($rnum==0){
$stmt->close();
$stmt = $conn->prepare($INSERT);
$stmt->bind_param("ssss", $username, $password, $gender, $email);
$stmt->execute();
echo "New record inserted successfully"
}else {
echo "Someone already registered";
}
$stmt->close();
$conn->close();
}
}
?>

Understanding ssssii in line 32 [duplicate]

This question already has answers here:
How to use mysqli prepared statements?
(3 answers)
Closed 2 years ago.
I'm Reviewing this code I got from YouTube to help my understanding about connecting PHP to MySQL
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$phoneCode = $_POST['phoneCode'];
$phone = $_POST['phone'];
if (!empty($username) || !empty($password) || !empty($gender) || !empty($email) || !empty($phoneCode) || !empty($phone)) {
$host = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbname = "youtube";
//create connection
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if (mysqli_connect_error()) {
die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
} else {
$SELECT = "SELECT email From register Where email = ? Limit 1";
$INSERT = "INSERT Into register (username, password, gender, email, phoneCode, phone) values(?, ?, ?, ?, ?, ?)";
//Prepare statement
$stmt = $conn->prepare($SELECT);
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($email);
$stmt->store_result();
$stmt->store_result();
$stmt->fetch();
$rnum = $stmt->num_rows;
if ($rnum==0) {
$stmt->close();
$stmt = $conn->prepare($INSERT);
$stmt->bind_param("ssssii", $username, $password, $gender, $email, $phoneCode, $phone); //line 32
$stmt->execute();
echo "New record inserted sucessfully";
} else {
echo "Someone already register using this email";
}
$stmt->close();
$conn->close();
}
} else {
echo "All field are required";
die();
}
?>
My question is what "ssssii" on line 32 do?
and also what values (?,?,?,?,?,?) On $insert for?
Source: Code and Coins YouTube.
This is prepared statement. S is string i is integer.
The (?,?,?,?) is for the script to know where to bind the params to

php not adding data to database by using file.php?data=value

I have a form that I want to submit it without reloading the page with jquery. I am trying to make a form that will send the data to MySQL database with PHP.
This is the code in submit button onclick:
e.preventDefault();
var data = JSON.stringify($("form").serialize());
$.post("insert.php?" + data);
and this is the PHP file :
<?php
$username = $_POST['username'];
$password = $_POST['password'];
$gender = $_POST['gender'];
$email = $_POST['email'];
$phoneCode = $_POST['phoneCode'];
$phone = $_POST['phone'];
if (!empty($username) || !empty($password) || !empty($gender) || !empty($email) || !empty($phoneCode) || !empty($phone)) {
$host = "localhost";
$dbUsername = "use";
$dbPassword = "user_pass";
$dbname = "test";
//create connection
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if (mysqli_connect_error()) {
die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());
} else {
$SELECT = "SELECT email From register Where email = ? Limit 1";
$INSERT = "INSERT Into register (username, password, gender, email, phoneCode, phone) values(?, ?, ?, ?, ?, ?)";
//Prepare statement
$stmt = $conn->prepare($SELECT);
$stmt->bind_param("s", $email);
$stmt->execute();
$stmt->bind_result($email);
$stmt->store_result();
$rnum = $stmt->num_rows;
if ($rnum==0) {
$stmt->close();
$stmt = $conn->prepare($INSERT);
$stmt->bind_param("ssssii", $username, $password, $gender, $email, $phoneCode, $phone);
$stmt->execute();
echo "New record inserted sucessfully";
} else {
echo "Someone already register using this email";
}
$stmt->close();
$conn->close();
}
} else {
echo "All field are required";
die();
}
?>
But it's not adding data to the database. I tried also writing the URL then the data like this: insert.php?data=value.But it didn't work. I am new to server-side and PHP, so I appreciate any help

Categories