Web designing newb. Creating a PHP forum from scratch!

Soldato
Joined
14 Feb 2006
Posts
4,644
Location
Surrey, UK
Oh yes, sorry. :o Apologies, it was late :D

Code:
$sql2 = "INSERT INTO `" . $tbl_name . "` ('reply_id', 'a_id', 'a_name', 'a_reply', 'a_datetime') VALUES ('" . $id . "', '" . $Max_id . "', '" . $a_name . "', '" . $a_reply . "', '" . $datetime . "')";

And yep, make sure your input is sanitised :)

Jon
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,869
Location
UK
I get the following:
ERROR: The SQL Server said:
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 ''reply_id', 'a_id', 'a_name', 'a_reply', 'a_datetime') VALUES ('

Rob, any reason in particular? Or is it just good practice?
 
Associate
Joined
6 Nov 2003
Posts
991
Location
Redditch
The error you're seeing is because you have quotes around the column names.

Change
Code:
('reply_id', 'a_id', 'a_name', 'a_reply', 'a_datetime')

to
Code:
(reply_id, a_id, a_name, a_reply, a_datetime)
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,869
Location
UK
The error you're seeing is because you have quotes around the column names.

Change
Code:
('reply_id', 'a_id', 'a_name', 'a_reply', 'a_datetime')

to
Code:
(reply_id, a_id, a_name, a_reply, a_datetime)
It's the values that I have quotes around:

Code:
$sql2="INSERT INTO $tbl_name(reply_id, a_id, a_name, a_reply, a_datetime)VALUES('$id', '$Max_id', '$a_name', '$a_reply', '$datetime')";
$result2=mysql_query($sql2);
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,869
Location
UK
I pretty darn certain the problem is minor, but I can't get my head around it! I've spent a few hours on it trying different kinds of code but alas not getting very far.
 
Last edited:
Soldato
Joined
6 Feb 2004
Posts
20,603
Location
England
put back ticks around the table like so....

Code:
$result2 = mysql_query("INSERT INTO `$tbl_name` (reply_id, a_id, a_name, a_reply, a_datetime) VALUES('$id', '$Max_id', '$a_name', '$a_reply', '$datetime')");
 
Associate
Joined
21 May 2003
Posts
1,365
Personally i'd build the query string first as a variable and pass that to mysql_query() - does the same thing but I find it easier to read and debug.
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,869
Location
UK
put back ticks around the table like so....

Code:
$result2 = mysql_query("INSERT INTO `$tbl_name` (reply_id, a_id, a_name, a_reply, a_datetime) VALUES('$id', '$Max_id', '$a_name', '$a_reply', '$datetime')");
I just tried that and I got "Parse error: parse error in xyzdir/etc/etc.php"

I don't understand this error: "Duplicate entry '1' for key 1" :(
 
Associate
Joined
21 May 2003
Posts
1,365
I just tried that and I got "Parse error: parse error in xyzdir/etc/etc.php"

I don't understand this error: "Duplicate entry '1' for key 1" :(

That usually means you have a "unique" restriction on one of your fields and you're trying to insert the same data twice.

I.e. if your Id field is a primary key, and you insert an id of 1234, and then later try and insert the same id, you will get that error.

Ideally you would make your primary key auto-increment, and then you wouldn't have to insert an id because the database will handle that field for you.
 
Associate
Joined
21 May 2003
Posts
1,365
Ahhhhhhhh, and you Sir deserve a badge! :D

I made the ID a primary key for strange reason :confused:

:D:D:D

In most cases you'd want a primary key in your table, and it's usually called "id" or something similar

i.e.

user table
=======
id << this should be a primary key and unique
name
email
etc...
country_id << this is a foreign key and should be indexed but not unique

country table
==========
id << this should be a primary key and unique
title
country_code
etc...
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,869
Location
UK
I'll look into that when I'm closer to completion. I did a lot of reading last night and because I was computer-less at work, I used the ancient means of using a pen and paper! :eek:

I've pretty much figured out how I will register users and then allow them to log in. I also read a chapter on cookies/session and I think I'll be using sessions. I've got a vague idea of how I will encrypt passwords and incorporate a link for "forgotten password".

Whilst reading a dummies guide to php & mysql, one of the code examples included ob_start. Now after googling around it's an output buffer but I have no idea what it's used for. Could anyone please elaborate on what it is and what it's used for? Cheers :)
 
Associate
Joined
21 May 2003
Posts
1,365
Whilst reading a dummies guide to php & mysql, one of the code examples included ob_start. Now after googling around it's an output buffer but I have no idea what it's used for. Could anyone please elaborate on what it is and what it's used for? Cheers :)

It allows you to control when the output from your script is sent to the browser, rather than it getting sent piecemeal as and when it executes an echo / print.

You may already have output buffering switched on via your php.ini, you can set it to off, on, or a maximum size in bytes.
 
Soldato
OP
Joined
14 Apr 2004
Posts
11,869
Location
UK
I've just ran into a another problem. I'm creating a form for members to register. I've created a number of files such as registeration form > create user > registration success > check login details form > login user > login success.

I'm experiencing problems with my create user, I have the following:

Code:
$sql="INSERT INTO $tbl_name(myusername, mypassword, email)VALUES('$myusername', '$mypassword', '$email')"; 
$result=mysql_query($sql);

if($result){
echo "Successful<BR>";
echo "<a href=resgistrationsuccess.php>Your Registration Was Successful!</a>";
}
else {
echo "ERROR: The SQL Server said:<br />" . mysql_error();
}
mysql_close();
?>

I did originally have 'username' and 'password' variables but that kept inserting my own MySql details. So I changed it myusername mypassword. However, when I check my table the data entered is empty. It will have a new ID number with no details inserted :confused:
 
Back
Top Bottom