Jump to content


Photo

No data supplied for parameters in prepared statement


Best Answer gizmola, 05 May 2018 - 01:20 AM

Obviously if you don't have all the data required for the insert, you are going to get an error. One issue that you have made for yourself unnecessarily is:
 

$user_id="";
$date_created = date( 'Y-m-d H:i:s' );
$date_edited ="";

So i'm going to assume that you have an auto_increment for user_id, and date_edited can be null, and since this is a new row you want that to be NULL on insert.

In this case, you should not be passing values for these. MySQL figures out how to auto_increment, and it makes no sense to pass a NULL date string for a date parameter that you will never set in this context.


Change the insert so that those are not included:
 

$stmt = $connection->prepare("INSERT INTO users values(?,?,?,?,?,?)");
$stmt->bind_param("s,s,s,s,s,s", $firstname, $lastname, $username, $hashed_password, $date_created, $status_id);
Go to the full post


  • Please log in to reply
5 replies to this topic

#1 ramashema

ramashema
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 04 May 2018 - 08:39 PM

Am just a beginner in PHP, am trying to implement user registration using bellow codes. But when am testing to register a user am getting "No data supplied for parameters in prepared statement" error please anyone with an idea please help..

 

 

 

session_start();
require_once ( "../include/config.php" );
require_once ( "../functions/functions.php" );
$message = array();

//check if the form has been submitted
if ( isset($_POST['submitted']) )
{
$firstname = sanitize( $connection, $_POST['firstname'] );
$lastname = sanitize( $connection, $_POST['lastname'] );
$username = sanitize( $connection, $_POST['username'] );
$password = sanitize( $connection, $_POST['password'] );
$confirm_password = sanitize( $connection, $_POST['confirm_password'] );
$status_id = sanitize( $connection, $_POST['status'] );

//check if username exist in the database
$stmt = $connection->prepare( "SELECT * FROM users WHERE username=?" );
$stmt->bind_param( "s",$username );
$stmt->execute();

$results = $stmt->get_result();

if ( $results->num_rows > 0 )
{
$message['error'] = "Username is not available!<br>Choose another and try again";
$_SESSION['error'] = $message;
header( "Location:../../public_html/user_registration.php" );
}

else
{
//check password matching
if ( $password !== $confirm_password )
{
$message['error'] = "Passwords do not match!<br>Make sure password and confirm password fields match";
$_SESSION['error'] = $message;
header( "Location:../../public_html/user_registration.php" );
}

else
{
//encrypt and salt the password
$presalt = "@&^3*!";
$postsalt = "%&*$!";

$salted_password = $presalt.$password.$postsalt;
$hashed_password = hash( 'ripemd128',$salted_password );

//inserting the user
$user_id="";
$date_created = date( 'Y-m-d H:i:s' );
$date_edited ="";

$stmt = $connection->prepare("INSERT INTO users values(?,?,?,?,?,?,?,?)");
$stmt->bind_param("i,s,s,s,s,s,s,s",$user_id,$firstname,$lastname,$username,$hashed_password,$date_created,$date_edited,$status_id);

$result = $stmt->execute();

if ( $result )
{
echo "<i>Successful registered a user..</i>";
header( "refresh:1;url=../../public_html/user_registration.php" );
}

else
{
$message['error'] = "User registration failed!<br>".$stmt->error;
$_SESSION['error'] = $message;
header( "Location:../../public_html/user_registration.php" );
}
}
}


}



#2 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,722 posts
  • LocationLos Angeles, CA USA

Posted 05 May 2018 - 01:20 AM   Best Answer

Obviously if you don't have all the data required for the insert, you are going to get an error. One issue that you have made for yourself unnecessarily is:
 

$user_id="";
$date_created = date( 'Y-m-d H:i:s' );
$date_edited ="";

So i'm going to assume that you have an auto_increment for user_id, and date_edited can be null, and since this is a new row you want that to be NULL on insert.

In this case, you should not be passing values for these. MySQL figures out how to auto_increment, and it makes no sense to pass a NULL date string for a date parameter that you will never set in this context.


Change the insert so that those are not included:
 

$stmt = $connection->prepare("INSERT INTO users values(?,?,?,?,?,?)");
$stmt->bind_param("s,s,s,s,s,s", $firstname, $lastname, $username, $hashed_password, $date_created, $status_id);


#3 gizmola

gizmola
  • Administrators
  • Advanced Member
  • 4,722 posts
  • LocationLos Angeles, CA USA

Posted 05 May 2018 - 07:33 AM

Also status_id sounds like a foreign key.  If that's an integer value, you should be passing it as such, not as a string.



#4 mac_gyver

mac_gyver
  • Staff Alumni
  • Staff Alumni
  • 4,148 posts

Posted 05 May 2018 - 11:38 AM

there would be a related and helpful php error, if php's error_reporting/display_errors were set properly, that would help identify that the problem is most likely due to the wrong format for the 1st bind_param() parameter. this should be a string of just the format specifiers, without commas between them.

 

next, whatever your sanitize() function code is doing, it is either one or all of - ineffective, insecure, or redundant and is probably not needed. could you post the code for the sanitize() function.

 

the current code will allow empty value(s) to be used for all the fields (an empty username won't match any existing user and an empty password will be equal to an empty confirmation password.)

 

you should validate all the inputs before using them and validate all the inputs at once. you are only validating some of the data and you are stopping at the first validation error, so it will take multiple form submissions to validate all of the data, but since you are redirecting back to the form, the user must keep filling in all the form values after each error, which will increase the likely-hood of introducing more errors.

 

you should put the form processing code and the form on the same page so that you can display all the validation errors when you re-display the form and you can populate the form fields with the existing values so that the user doesn't need to keep re-tying in all the values when there is a validation error.

 

edit, you should also list the column names in the INSERT query so that your code will be self-documenting and will still work if the columns get rearranged.


Edited by mac_gyver, 05 May 2018 - 11:51 AM.

multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting.

#5 mac_gyver

mac_gyver
  • Staff Alumni
  • Staff Alumni
  • 4,148 posts

Posted 05 May 2018 - 12:03 PM

edit, you should also list the column names in the INSERT query so that your code will be self-documenting and will still work if the columns get rearranged.

 

^^^ and which is required if you leave out a column value.


multi-purpose programming fool. well written source-code should be self-documenting. well written code should be self-troubleshooting.

#6 ramashema

ramashema
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 05 May 2018 - 04:43 PM

 

Obviously if you don't have all the data required for the insert, you are going to get an error. One issue that you have made for yourself unnecessarily is:
 

$user_id="";
$date_created = date( 'Y-m-d H:i:s' );
$date_edited ="";

So i'm going to assume that you have an auto_increment for user_id, and date_edited can be null, and since this is a new row you want that to be NULL on insert.

In this case, you should not be passing values for these. MySQL figures out how to auto_increment, and it makes no sense to pass a NULL date string for a date parameter that you will never set in this context.


Change the insert so that those are not included:
 

$stmt = $connection->prepare("INSERT INTO users values(?,?,?,?,?,?)");
$stmt->bind_param("s,s,s,s,s,s", $firstname, $lastname, $username, $hashed_password, $date_created, $status_id);

Thanks for the useful comment, i will revise and remove unnecessary variable; although the problem was due to commas on type specifier on bind param()

 

changed to;

$stmt = $connection->prepare("INSERT INTO users (firstname,lastname,username,password,date_created,status_id) VALUES (?,?,?,?,?,?)");
$stmt->bind_param("sssssi", $firstname, $lastname, $username, $hashed_password, $date_created, $status_id);

 

and it worked!..






0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users