Web designing newb. Creating a PHP forum from scratch!

Backquote table names, don't single-quote them. This:

Code:
'" . $tbl_name . "'

should be:

Code:
`" . $tbl_name . "`

Be sure to escape your input, too.
 
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
 
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?
 
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)
 
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);
 
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:
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')");
 
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.
 
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.

Thats how I do it. Much easier to debug as you can echo out the string and see if it reads correctly.
 
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" :(
 
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.
 
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...
 
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 :)
 
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.
 
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