Reputation: 3986
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.
Upvotes: 1
Views: 267
Reputation: 2433
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.
Upvotes: 1