Question:
PHP/MySQL help, column "name" cannot be null?
Veyrong
2012-06-12 19:06:43 UTC
I'm trying to connect my html form with the database but I'm geting this error message:
"column 'name' cannot be null"

what I'mm doing wrong?


Yahoo cut the code so here is in Pastebin: http://pastebin.com/embed_iframe.php?i=xAiGizhU
Three answers:
TnT
2012-06-12 19:29:40 UTC
Found your mistakes.



You need to add a name for the input values. So to get $_POST['name'] you must have an input with a value of 'name'.







This was causing a NULL value to be passed (Do you have errors showing?) and since the columns can not be null you were getting that error.



A couple things you'll find out later.

- Always check to make sure the values are set before accessing them with $_POST. Failing to do this will cause errors when someone doesn't enter in all the fields in the form.



- It'll be easier to call the sanitize function in this group of code ...

$name = $_POST['name'];

$email = $_POST['email'];

$url = $_POST['url'];

$comment = $_POST['comment'];

$ip = gethostbyname($_SERVER['REMOTE_ADDR']);

Sanitizing the function before passing it into a variable you named will make sure that it's always sanitized allowing you to be sure it's safe.



- In your sanitizing function, I suggest not using addslashes. Instead remove the slashes if magic_quotes is on and then use mysql_real_escape_string (Should use mysqli though)

http://php.net/manual/en/function.mysql-real-escape-string.php

Reason why http://shiflett.org/blog/2006/jan/addslashes-versus-mysql-real-escape-string
Cool
2012-06-12 23:20:21 UTC
The field named "name" in MySql is set to not null by default. If you don't mind having a null name in your table, you can alter the table's structure by following this:



ALTER TABLE `Datebase Name`.`Table Name` MODIFY COLUMN `name` VARCHAR(255) CHARACTER SET latin1 COLLATE latin1_swedish_ci DEFAULT NULL;
Sangita D
2012-06-12 19:27:50 UTC
u r probably having a column called "name" which doesn't feel happy with nulls


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...