I'm making a PHP forum script

But, when ever i try to add a new topic it gives me this error:

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 'desc) VALUES (Test, General Chat, This is a test)' at line 1

Here is where the query is made

<?php
$sub = $_POST['submit'];
if($sub){
?>
<table border='0' cellpadding='5'>
<tr><th colspan='2'>New Topic</th></tr>
<form action='?id=<?php echo $id; ?>' method='POST'>
<tr><td>Topic Name: </td><td><input type='text' name='sub_name'></td><tr>
<tr><td>Topic Description: </td><td><input type='text' name='sub_desc'></td></tr>
<tr><td colspan='2' align='right'><input type='submit' value='Create Topic' name='submit2'></td></tr>
</form>
</table>
<?php
}
$result2 = mysql_query("SELECT * FROM forum_cats WHERE id = '".$id."'");
while($row = mysql_fetch_array($result2)){
$cat_name = $row['cat_name'];
}
$sub2 = $_POST['submit2'];
$name = $_POST['sub_name'];
$desc = $_POST['sub_desc'];
if($sub2){
mysql_query("INSERT INTO forum_sub_cats (sub_cat_name, forum_cat_name, desc) VALUES ($name, $cat_name, $desc)") or die(mysql_error());
echo "Topic Added!";
}

Anyone help?

You are missing quotes around the string columns in your query.

mysql_query("INSERT INTO forum_sub_cats (sub_cat_name, forum_cat_name, desc) VALUES ('$name', '$cat_name', '$desc')") or die(mysql_error());

Note that you may also need to use mysql_real_escape_string() if you use single quotes in your texts.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.