PDA

View Full Version : SQL syntax error


shazzi2k
02-21-2009, 09:26 PM
Hi again :S

I am on the tutorial part10 Deleting data. I got to the part where i am testing. I come to hitting the delete button to remove a record and i get this message.

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 ')' at line 1

Line 1 just has <?php

davidj
02-22-2009, 02:44 AM
there is an error in your SQL mate

you have to read the error messages as they give a good clue

post the SQL

shazzi2k
02-22-2009, 01:43 PM
In the SQL database or the script? Sorry only started this the other day by watching your tutorials

<?php

require_once("../Connections/connection.php"); //database connection script



////////////////////////
$data2 = $_POST[ 'data2' ] ;
$data3 = $_POST[ 'data3' ] ;
$data4 = $_POST[ 'data4' ] ;
$Submit = $_POST[ 'Submit'] ;
$del = $_GET [ 'del' ] ;
////////////////////////

if ($del) {
////////////////////////
$query = sprintf ("DELETE FROM table1 where field1_id='del')");
mysql_query ($query) or die (mysql_error () );
////////////////////////
}

///////////////////////

if ($Submit && $data2 && $data3 && $data4) {

////////////////////////

$query = sprintf ("INSERT into table1 (field2, field3, field4) values ('$data2', '$data3', '$data4') ");
mysql_query ($query) or die (mysql_error () );

}elseif ($Submit) {
echo "A required field is empty!" ;

}

////////////////////////
$query = sprintf (" select * FROM table1");
$result = mysql_query ($query);
$row = mysql_fetch_array ($result);
////////////////////////

/*do {
echo $row ['field3']. "<br>";

} while ($row = mysql_fetch_array($result) )
*/
?>


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>ShazCorp Insert data</title>
</head>

<body>
<p>&nbsp;</p>
<form id="form1" name="form1" method="post" action="<?php $_SERVER [ 'PHP_SELF'] ; ?>">
<table width="437" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="146" height="28"><div align="center">data2</div></td>
<td width="145"><div align="center">data3</div></td>
<td width="146"><div align="center">data4</div></td>
</tr>
<tr>
<td height="36"><label>
<input name="data2" type="text" id="data2" />
</label></td>
<td><input name="data3" type="text" id="data3" /></td>
<td><input name="data4" type="text" id="data4" /></td>
</tr>
<tr>
<td height="47" colspan="3"><label>
<div align="center">
<input type="submit" name="Submit" value="Submit" />
<input name="Submit" type="hidden" id="Submit" value="1" />
</div>
</label></td>
</tr>
</table>
<p>&nbsp;</p>
</form>
<p>&nbsp;</p>
<table width="603" border="1" cellspacing="1" cellpadding="1">
<tr>
<td width="99" height="27">ID1</td>
<td width="135">data2</td>
<td width="135">data3</td>
<td width="135">data4</td>
<td width="71">&nbsp;</td>
</tr>
<?php do { ?>
<tr>
<td height="30"><?php echo $row ['field1_id']; ?></td>
<td><?php echo $row ['field2']; ?></td>
<td><?php echo $row ['field3']; ?></td>
<td><?php echo $row ['field4']; ?></td>
<td><input type="button" name="Submit2" value="Delete" onclick="document.location.href='shazcorp.php?del= <?php echo $row ['field1_id']; ?> '" /></td>
</tr>
<?php }while ($row = mysql_fetch_array($result)); ?>
</table>
<label></label>
</body>
</html>

davidj
02-22-2009, 02:00 PM
you have a rogue ) here


$query = sprintf ("DELETE FROM table1 where field1_id='del')");


after 'del'