简体   繁体   中英

PHP: Insert NULL value in mysqli prepare

I am in a strange situation. In my database table fields are defined as NOT NULL . When i run below query it doesn't show the output of echo 'Data Added';

But if I change field defintions via phpmyadmin to NULL=yes then it is showing Data Added .

$results = $mysqli->prepare("INSERT INTO posting (emp_id, title, open, description, keywords, min, max, ip_add) VALUES (?, ?, ?, ?, ?, ?, ?, ?)");
$results ->bind_param("isisssss", $emp_id, $title, $open, $description, $keywords, $min, $max, $ip_add );

$results->execute();

if($results->affected_rows == 1){

            echo 'Data Added';
        }

I want to know do i need to change all field in mysql table and make them Null = Yes or is there any other way to achieve this?

Am i doing something wrong here. please advise.

When the column in PHPmyAdmin is set 'not null' it means that you need to have some value in that field, otherwise it fails, so in short your INSERT query would fail and you won't see echo 'Data Added'; since no rows were affected due to the previous error in MySQL.

As to what all you should make NULL depends on your setup, some fields might be mandatory, for example you may want your 'emp_id' to be present all times in a record so you would make it 'NOT NULL'.

Set the field attributes and make changes in your code accordingly, if a field is mandatory in the database you can take measures to ensure that the field is present before being sent to the database.

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