Jump to content

MySQL syntax error


infopcds

Recommended Posts

Hi everyone. I could use a little help with some coding.

I bastardized our events calendar on this project to make a very simple coupon listing. Thinking it would be easier to use existing code and modify the code just a bit.

Running into various problems depending on what i try to change.

 

If i use this code as posted I get a syntax error near the where coup_id= line

 

<?php
require("connector.php");
$type = $_GET['par_type'];

if($type == 'add')
{
$tmp_dt =$_POST['dt1'];
$yr = substr($tmp_dt,6,4);
$mn = substr($tmp_dt,0,2);	
$dt = substr($tmp_dt,3,2);	
$st = mktime(0,0,0,$mn,$dt,$yr);
$coup_start = date("Y-m-d", $st);

if($coup_start >= date("Y-m-d"))

$tmp_dt2 =$_POST['dt2'];
$yr = substr($tmp_dt2,6,4);
$mn = substr($tmp_dt2,0,2);	
$dt = substr($tmp_dt2,3,2);	
$st = mktime(0,0,0,$mn,$dt,$yr);
$coup_exp = date("Y-m-d", $st);

if($coup_exp >= date("Y-m-d"))
{

	$coup_offer=$_POST['coup_offer'];
	$coup_code=$_POST['coup-code'];
	$coup_desc=$_POST['coup_desc'];
	$coup_contact=$_POST['coup_contact'];
	$coup_link=$_POST['coup_link'];
	$coup_comp=$_POST['coup_comp'];

	$eve_qry = mysql_query("select max(coup_id) from supplier_coupon",$cstr);
	$mx_no = mysql_result($eve_qry,0,"max(coup_id)");
	if(!$mx_no){ $coup_id=1;}else{$coup_id = $mx_no +1;}

}if ($ok == "1") 
{
	{

		$ins_qry=mysql_query("insert into supplier_coupon (coup_id, coup_offer, coup_code, coup_desc, coup_contact, coup_link, coup_exp, coup_start, coup_comp) values ('$coup_id', '$coup_offer', '$coup_code', '$coup_desc', '$coup_contact', '$coup_link', '$coup_exp', '$coup_start', '$coup_comp')",$cstr);

		if (!$ins_qry)
		{
			print '<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr><td> </td></tr>
			  <tr>
			  <td align="center" ><span class="subhead">There was an error saving your coupon.</span><br><span class="txt2">'.mysql_error().'</span><br><br><a href="javascript:history.back();">To try again</a></td></tr>
			  </table>';

		}else
		{
			print '<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr><td> </td></tr>
			  <tr>
			  <td align="center" ><span class="subhead">Your coupon is updated to the database.</span></td></tr>
			  </table>';
		}//insert query check if loop ends here

	}// end of $ok if check loop

}
  	
}else if ($type=="del")
{

	$chk_val = $_GET['chk_val'];
	$coupid =$_GET['cpnnum'];
	if ($chk_val != 1 )
	{

		$cname_qry=mysql_query("select coup_code from supplier_coupon where coup_id='$coupid' ",$cstr);
		$coup_code = mysql_result($cname_qry,0,"coup_code");


		$pr_dtxt ='<table width="80%" border="0" cellspacing="0" cellpadding="0" align="center"><tr><td> </td></tr><tr><td width="17%"> </td>
		<td colspan="2" align="left" class="txtbold">Do you really want to delete the coupon '.ucwords($coup-code).' ? </td>
		</tr><tr><td> </td>
		<td width="22%" align="right">
		<form action="editcoupon.php" method="get">
		<input type="hidden" value="'.$coupid.'" name="cpnnum">	
		<input type="hidden" name="chk_val" value="1" />
		<input type="hidden" value="del" name="par_type">
		<input type="submit" value="Yes"/>
		</form></td>';

            $pr_dtxt .='<td width="61%" align="left" >
		<form action="couponnew.php" method="get">
		<input type="submit" value="Cancel"/></form></td></tr></table>';


			print $pr_dtxt;

	}elseif ($chk_val == 1)
	{
		$del_qry = mysql_query("delete from supplier_coupon where coup_id='$coupid'",$cstr);

		if (!$del_qry)
		{
			print '<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr><td> </td></tr>
			  <tr>
			  <td align="center" ><span class="subhead">There was error in deleting the event coupon details from the database</span><br><span class="txt2">'.mysql_error().'</span><br><br><a href="javascript:history.back();">To try again</a></td></tr>
			  </table>';

		}else
		{
			print '<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr><td> </td></tr>
			  <tr>
			  <td align="center" ><span class="subhead">The event details have been removed from the database</span></td></tr>
			  </table>';
		}//delete query check if loop ends here

	} // 
}elseif($type == 'edit')
{
$tmp_dt =$_POST['dt1'];
$yr = substr($tmp_dt,6,4);
$mn = substr($tmp_dt,0,2);	
$dt = substr($tmp_dt,3,2);	
$st = mktime(0,0,0,$mn,$dt,$yr);
$coup_start = date("Y-m-d", $st);

}if($coup_start >= date("Y-m-d"))
{
$tmp_dt2 =$_POST['dt2'];
$yr = substr($tmp_dt2,6,4);
$mn = substr($tmp_dt2,0,2);	
$dt = substr($tmp_dt2,3,2);	
$st = mktime(0,0,0,$mn,$dt,$yr);
$coup_exp = date("Y-m-d", $st);

}if($coup_exp >= date("Y-m-d"))
{

	$coup_id=$_POST['coupid'];
	$coup_offer=$_POST['coup_offer'];
	$coup_code=$_POST['coup-code'];
	$coup_desc=$_POST['coup_desc'];
	$coup_contact=$_POST['coup_contact'];
	$coup_link=$_POST['coup_link'];
	$coup_comp=$_POST['coup_comp'];

	{
		$ok = "1";
		$ins_txt="update supplier_coupon set coup_offer='$coup_offer', coup_code='$coup_code', coup_desc= '$coup_desc', coup_contact='$coup_contact', coup_link='$coup_link', coup_exp='$coup_exp', coup_start='$coup_start', coup_comp='$coup_comp', where coup_id='$coup_id'";

	}
	{

		$ins_qry=mysql_query($ins_txt,$cstr);


		if (!$ins_qry)
		{
			print '<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr><td> </td></tr>
			  <tr>
			  <td align="center" ><span class="subhead">There was error saving the updated coupon details</span><br><span class="txt2">'.mysql_error().'</span><br><br><a href="javascript:history.back();">To try again</a></td></tr>
			  </table>';

		}else
		{

			print '<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr><td> </td></tr>
			  <tr>
			  <td align="center" ><span class="subhead"> The coupon details has been updated into the database</span></td></tr>
			  </table>';
		}//insert query check if loop ends here
	}// $ok if check loop

}else
{

	print '<table width="100%" border="0" cellspacing="0" cellpadding="0">
	<tr><td> </td></tr>
	  <tr>
	  <td align="center" ><span class="txtbold">Please select a valid date for the coupon. Current date selected by you is '.$tmp_dt.' </span></td></tr>
	  		  <tr>
	  <td align="center" ><a href="javascript:history.back();" class="mainmenu1">Click here to go back to Previous page</a></td></tr></table>';	
	  
}// end of date check if loop

// end type check if loop

?>		

 

Any help with this would be appreciated. This is what happens when you think you can take the easy way out and borrow your own code. i have a feeling that something is not closed right, but i havnt been able to find it and my head is becoming mush from staring at this same code for far too long.

Link to comment
https://forums.phpfreaks.com/topic/106625-mysql-syntax-error/
Share on other sites

You have a comma before the WHERE and it should not be there

 

$ins_txt="update supplier_coupon set coup_offer='$coup_offer', coup_code='$coup_code', coup_desc= '$coup_desc', coup_contact='$coup_contact', coup_link='$coup_link', coup_exp='$coup_exp', coup_start='$coup_start', coup_comp='$coup_comp' WHERE coup_id='$coup_id'";

 

 

Link to comment
https://forums.phpfreaks.com/topic/106625-mysql-syntax-error/#findComment-546533
Share on other sites

This is the error that  get

 

There was error saving the updated coupon details

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 'where coup_id=''' at line 1

 

 

Thanks removing the comma worked, but now I get a success message with no data added to the database. i think now it's a SQL problem within my tables.

Link to comment
https://forums.phpfreaks.com/topic/106625-mysql-syntax-error/#findComment-546536
Share on other sites

Am I putting too mucyh work into this part of the code?

		$eve_qry = mysql_query("select max(coup_id) from supplier_coupon",$cstr);
	$mx_no = mysql_result($eve_qry,0,"max(coup_id)");
	if(!$mx_no){ $coup_id=1;}else{$coup_id = $mx_no +1;}

 

Just to make a new unique number for each entry?

Link to comment
https://forums.phpfreaks.com/topic/106625-mysql-syntax-error/#findComment-546571
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.