简体   繁体   中英

error in insert sql statement

this is sign up form pls help me to solve this error

ERROR: Could not able to execute sql insert statement

INSERT INTO customer(customer_id,customer_name,customer_email,customer_password) VALUES (null,'','','').Column 'customer_id' cannot be null

//check connection
if($link===false){
die("ERROR: could not connect. " . mysqli_connect_error());
}

//Escape user inputs for security
$customer_id = mysqli_real_escape_string($link, $REQUEST['customerid']);
$customer_name = mysqli_real_escape_string($link, $REQUEST['Name']);
$customer_email = mysqli_real_escape_string($link, $REQUEST['Email']);
$customer_password = mysqli_real_escape_string($link, $REQUEST['Password']);

//attempt insert query execution
$sql="INSERT INTO customer(customer_id,customer_name,customer_email,customer_password) VALUES (null,'$customer_name','$customer_email','$customer_password')";

if(mysqli_query($link,$sql)){
echo "records added successfully.";
} else{
echo "ERROR: Could not able to execute $sql.".mysqli_error($link);
}

mysqli_close($link);
?>

It says in your error message that the column customer_id can not be null. Looking at your query VALUES (null,'$customer_name','$customer_email','$customer_password')"; the values start after the parenthesis. So your first column is customer_id and the first value is null .

The error message tells you that your table is not setup to allow for customer_id to be null . So you either have to change to database table structure or your query. As it is about an ID I suggest you change your query.

If customer_id is an auto incremented field you should simply not set it (remove it from your query)

Edit:

It is not usual to have an identifier set to varchar. Seeing something like customer_id lets most people assume that they are dealing with an integer. The easiest approach would be to make that table column an integer with auto increment in your database (in most cases such a field would also be primary key). Seeing that your table requires the field customer_id it can not be left out from your query and you would have to generate / create an adequate identifier for your customer. A workaround for your current table structure:

$sql="
INSERT INTO 
    customer
    (customer_id,customer_name,customer_email,customer_p‌​‌​assword) 
VALUES (
(select count(customer_id) from customer)+1,'$customer_name','$customer_email','$customer_pa‌​ssword‌​')";

Edit 2: Some additional information on database column data types: mysql char vs. varchar and mysql integer

if you later on have a more complex database and query multiple tables at once to gather your data you will have to rely on your indices and realtions (foreign keys) to get the results fast. IE: having a join on your customer_id is rather slow. setting it to char lets it operate faster due to varchar having a variable length. I have not yet tested the performance regarding char and integer

You need to pass customer_id instead of null in the query, eg:

$sql="INSERT INTO customer(customer_id,customer_name,customer_email,customer_password) 
VALUES ('$customer_id','$customer_name','$customer_email','$customer_password')";

You can remove customer_id column like below,

$sql="INSERT INTO customer(customer_name,customer_email,customer_password) 
VALUES ('$customer_name','$customer_email','$customer_password')";

If you set customer_id as auto increment in database there no need to include it in query

If your customer_id is an autoincrement id of your customer table then, no need to put it inside your INSERT statement.

Simply run without customer_id

$sql="INSERT INTO customer(customer_name,customer_email,customer_password) 
VALUES ('$customer_name','$customer_email','$customer_password')";

Ok. Let me explain you somethings.

Case 1 :

If your customer_id is an integer type and you have not put anything in default value then it will not allow you to insert a null into that field. For that, you need to set default value as NULL .

Case 2

If your customer_id is an autoincrement id of customer table then you can't set null into it during a SQL statement.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM