Saturday, May 11, 2024
 Popular · Latest · Hot · Upcoming
0
rated 0 times [  0] [ 0]  / answers: 1 / hits: 9170  / 1 Year ago, tue, february 28, 2023, 8:23:44

I try to build my own website, and at my website I ask people to enter their name and write a little story. When they are done, they hit submit, and it sends the information to my database. I work with MySQL, and I want to create a table where I can store the information in. This is my MySQL code:



CREATE TABLE names (
name PRIMARY KEY NOT NULL,
message NOT NULL,
IPv4 NOT NULL,
browser NOT NULL,
date NOT NULL,
time NOT NULL,
);


But when I enter this in my command prompt, it says:



ERROR 1064 (42000): You have an error in your SQL syntax; 
check the manual that corresponds to your MySQL server version for the
right syntax to use near 'PRIMARY KEY NOT NULL, message NOT NULL,
IPv4 NOT NULL, browser NOT NULL, date NO' at line 1


Can you tell me what I did wrong and how to fix this? I don't know if it's helpful, but I've included my PHP script too. It can be a little messy with names because I had to translate the variables to English. My PHP code is (I created it before I created the table, and as you see I did manage to set up my database 'names', also I want to call my table 'names'):



$name = $_POST['name'];
$message = $_POST['message'];
$user = 'user';
$password = 'Login';
$host = 'localhost:3036';
$browser = $_SERVER['HTTP_USER_AGENT'];
$ip = $_SERVER["REMOTE_ADDR"];
date_default_timezone_set('UTC+1');
$date = date("Y-m-d");
$time = date("H:i:s");
$connect = mysql_connect($host, $user, $password);
if (! $connect)
{
$error = mysql_error();
print "Sorry, could not connect: $error";
}
print "Connected!";
$database = "USE names";
$table = "INSERT INTO names (name, message, IPv4, browser, date, time) VALUES ($name, $message, $ip, $browser, $date, $time)";
if (! $database)
{
$error=mysql_error();
echo "
Could not find database! Error: $mysql_error";
}
if (! $table)
{
$error=mysql_error();
echo "
Could not write to database! Error: $error";
}
$close = mysql_close($connect);
if (! $close )
{
$error=mysql_error();
echo "
Could not close connection! Error: $error";
}
echo "
Bedankt $name!";

More From » mysql

 Answers
6

Yes your last line shouldn't contain a comma , since it's the last line and no more variables will come after it. so the code should look like this:



CREATE TABLE names (
name char(40) PRIMARY KEY NOT NULL,
message varchar(100) NOT NULL,
IPv4 char(50) NOT NULL,
browser char(20) NOT NULL,
date datetime NOT NULL,
time int NOT NULL
);


Note that the last line, time NOT NULL doesn't have a comma at the end.



MYSQL aren't very helpful with there error messages they mostly tell you that you need to read your code again until you get it right.



Also you haven't told MYSQL what sort of data will go into these fields. Is it a char or a number? You need to specify this like, data int primary key or data2 char(40) not null. On int's you don't need to specify the length of the variable but on chars you do.


[#25094] Thursday, March 2, 2023, 1 Year  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
homerurhyth

Total Points: 338
Total Questions: 113
Total Answers: 105

Location: Moldova
Member since Sat, Aug 6, 2022
2 Years ago
;