/    Sign up×
Community /Pin to ProfileBookmark

query(‘INSERT INTO …’) not working

Hello everyone, I am learning php and mysql, and coding with brackets on a local server with XAMPP. So far everything was working, but INSERT INTO seems to make my web page froze with the code HTTP error 500. This usually happens when I miss a “;” but here everything seems to be fine.
Everything is correct (customers, first_name and last_name are correct and have been used with lines of code that works) so I don’t know what is causing it. Maybe someone can help me ?

Here’s the code (I deleted the php tags at the end and start because I couldn’t post otherwise) :
PHP:

“`
try {
$database = new PDO(‘mysql:host=localhost;dbname=store’,’root’,”, array(PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION));
}
catch(Exception $e)
{
die(‘Error: ‘.$e->getMessage());
}

$database->query(“INSERT INTO customers(first_name, last_name) VALUES (‘Clark’, ‘Kent’)”);
“`

to post a comment
PHP

15 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 10.2022 — Single backticks (probably created by the button `&lt;/&gt;</C>) won't work reliably when posting code. You better use code tags: <C>your code here` or triple backticks. I edited your posting accordingly.
Copy linkTweet thisAlerts:
@FredericauthorJan 10.2022 — @Sempervivum#1641346 Thanks again 😁
Copy linkTweet thisAlerts:
@ginerjmJan 10.2022 — Do you have error checking enabled so that the script might give you some error help?

error_reporting(E_ALL);<br/>
ini_set('display_errors', '1');
Copy linkTweet thisAlerts:
@FredericauthorJan 10.2022 — Thank you for your answer, I just wrote it and it gave me this "Fatal error: Uncaught PDOException: SQLSTATE[HY000]: General error: 1364 Field 'email' doesn't have a default value in /Applications/XAMPP/xamppfiles/htdocs/test.php:14 ".

'email' is a column in the table I was trying to insert a new line. But in my online course, the teacher just wrote the line of code I presented you and it worked. He teached that INSERT INTO works even if we don't write all column objects into the code, it will just appeared empty on the new line created. I use the same files as he provided for his example. So I don't understand ...
Copy linkTweet thisAlerts:
@sibertJan 10.2022 — > @Frederic#1641344 I don't know what is causing it.

It must be a PHP thing. The query works perfect:

https://www.db-fiddle.com/f/eaQG8H4yqY9hnQBZjzJgz/62
Copy linkTweet thisAlerts:
@SempervivumJan 10.2022 — IMO the error message says it all:
>Field 'email' doesn't have a default value

You didn't specify the field 'email', this is possible only when a default value is specified for that column.

Unfortunately when I perform the same operation (inserting without specifying every column) I don't get an error. I researched a bit and learned that an error is thrown only when strict mode is enabled for the database. Maybe this is an explanation why I don't get an error.

Edit: I checked sql_mode and my database was not in strict mode. After enabling it I got the same error:
>Uncaught PDOException: SQLSTATE[HY000]: General error: 1364 Field 'a' doesn't have a default value
Copy linkTweet thisAlerts:
@NogDogJan 10.2022 — Sounds like the email column is defined in your test database as NOT NULL. In that case, you would have to include a value for it, _unless_ the table definition also includes a default value for that column.
Copy linkTweet thisAlerts:
@ginerjmJan 10.2022 — As everyone has told you, the error message gave you exactly what the issue was. Take a look at your phpadmin or whatever you are using to create your tables and update that option for the table. If you have not checked that to allow Null then you therefore have to provide a value for every new record or else mysql slaps your hand.
Copy linkTweet thisAlerts:
@FredericauthorJan 10.2022 — Thanks everyone for your help, it worked. Have a great day.
Copy linkTweet thisAlerts:
@ginerjmJan 10.2022 — HTH!

BTW - that snippet of codeI showed you that gave you that error message - be sure you turn it off or remove it before putting your script(s) into production. You don't want to be showing any detailed error info to your users. Only show messages that are designed to let them know there is an issue but don't be giving them script names and line numbers.
Copy linkTweet thisAlerts:
@NogDogJan 10.2022 — @ginerjm#1641361

Yep, or you can just change the last arg to ini_set to false (or 0), and it will just log the errors instead.
Copy linkTweet thisAlerts:
@alanmarkJan 11.2022 — It must be a PHP thing

[io games](https://iogames-free.com)
Copy linkTweet thisAlerts:
@FredericauthorJan 12.2022 — Thank you, gentlemen.
Copy linkTweet thisAlerts:
@Johndavidson9Jan 12.2022 — Thanks everyone for your help, it worked. [get-9apps.com](https://get-9apps.com) [cartoon hd](https://cartoonhd.onl/index/)
Copy linkTweet thisAlerts:
@orhan34Apr 21.2022 — In my online course, the teacher just wrote the line of code I presented you and it worked.
×

Success!

Help @Frederic spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...