Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

why is my query failing??????

Status
Not open for further replies.

philstockham

Technical User
Feb 21, 2005
20
GB
i have a form to input data into a mysql database table. when i was inputting only a couple of fields it worked fine, however, now i have added more fields and it is failing. i have checked the code countless time and can't see what is causing this - HELP IS DESPERATELY NEEDED

below is the mysql table the data is going into:
CREATE TABLE report(
id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(50) NOT NULL,
description VARCHAR(200) NOT NULL,
covershot VARCHAR(50) NOT NULL,
mainbody TEXT NOT NULL,
contents TEXT NOT NULL,
keywords TEXT NOT NULL,
PRIMARY KEY(id)
);

Here is the html/php code i am using for the form:
<form method="post">
<table width="448" border="0" cellpadding="2" cellspacing="1" align="left">
<tr>
<td width="100"><font color="#0000FF" face="Geneva, Arial, Helvetica, sans-serif">Title:</font></td>
<td width="337"><input name="title" type="text"></td>
</tr>
<tr>
<td width="100" valign="top"><font color="#0000FF" face="Geneva, Arial, Helvetica, sans-serif">Descritption:</font></td>
<td><textarea name="description" cols="50" rows="3" id="description"></textarea></td>
</tr>
<tr>
<td height="77"><font color="#0000FF" face="Geneva, Arial, Helvetica, sans-serif">Pricing:</font></td>
<td><textarea name="pricing" cols="50" rows="3" id="pricing"></textarea></td>
</tr>
<tr>
<td valign="top"><font color="#0000FF" face="Geneva, Arial, Helvetica, sans-serif">Cover
shot:</font></td>
<td><input name="covershot" type="text" id="covershot"></td>
</tr>
<tr>
<td valign="top"><font color="#0000FF" face="Geneva, Arial, Helvetica, sans-serif">Main
body: </font></td>
<td><textarea name="mainbody" cols="50" rows="10" id="mainbody"></textarea></td>
</tr>
<tr>
<td valign="top"><font color="#0000FF" size="4" face="Geneva, Arial, Helvetica, sans-serif">Contents:</font></td>
<td><textarea name="contents" cols="50" rows="10" id="textarea2"></textarea></td>
</tr>
<tr>
<td valign="top"><font color="#0000FF" size="4" face="Geneva, Arial, Helvetica, sans-serif">Keywords:</font></td>
<td><textarea name="keywords " cols="50" rows="10" id="keywords "></textarea></td>
</tr>
<tr>
<td colspan="2" align="center"><input name="save" type="submit" value="Save Report"></td>
</tr>
</table>
</form>

<?php
if(isset($_POST['save']))
{
$title = $_POST['title'];
$description = $_POST['description'];
$pricing = $_POST['pricing'];
$covershot = $_POST['covershot'];
$mainbody = $_POST['mainbody'];
$contents = $_POST['contents'];
$keywords = $_POST['keywords'];

if(!get_magic_quotes_gpc())
{
$title = addslashes($title);
$description = addslashes($description);
$pricing = addslashes($pricing);
$covershot = addslashes($covershot);
$mainbody = addslashes($mainbody);
$contents = addslashes($contents);
$keywords = addslashes($keywords);
}

include 'config.php';
include 'opendb.php';


$query = "INSERT INTO report (title, description, pricing, covershot, mainbody, contents, keywords) ".
"VALUES ('$title', '$description', '$pricing', ''$covershot', '$mainbody', '$contents', '$keywords')";
mysql_query($query) or die('Error ,query failed');


include 'closedb.php';

echo "Report '$title' added";
}
?>


Any help is much appreciated.

Regards
 
I'm not a SQL guru... but I don't quite understand why you have the following line sitting in your table definition:

Code:
PRIMARY KEY(id)

Try removing that line (and the preceeding comma from the line above) and see how you go.

Just a thought.
Jeff
 
Unless, of course, this is just another way to define the primary key... but not a method I've ever seen before. I guess that's why I'm not a SQL guru [smile]

Jeff
 
hi jeff,

i don't think the table is the problem. it seems to be the INSERT query where the data is passed from the form to table that is the problem.
 
some queries:
what precise error are you getting? where is the script failing? (please turn error_reporting(E_ALL) on)
are the variables coming through from the form ok if you use print_r($_POST)?
try using mysql_escape_string instead of addslashes (unlikely to be the issue)
it is normal to give a form an action attribute. since this is a self-managing form the attribute would have the value of $_SERVER['PHP_SELF']. i don't know whether the xhtml spec assumes this (i don't think it does) and am not sure what doctype you are using.
in the mysql_query statement display the mysql_error() return at the end of the die string
also echo the query to the screen before using it in the query, as a debug exercise.
 
hi

the error i am getting is "Error ,query failed". I will try using the action attribute but feel it is more the SQL that is the problem. the document type is html.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top