Jump to content

You have an error in your SQL syntax - (Noob question)


MadnessRed

Recommended Posts

OK, I am VERY new to SQL, I read the w3schools pages on it yesterday and started a script.

 

My script didn't work so I though I would add what I expected the script to say into phpmyadmin and this is what I get

 

INSERT INTO madnessred_sites (Name, url, Image, Rating) VALUES (Google, http://www.google.co.uk/, none, 5)

 

Thats what I am adding. And the result

 

There seems to be an error in your SQL query. The MySQL server error output below, if there is any, may also help you in diagnosing the problem

ERROR: Unknown Punctuation String @ 77
STR: ://
SQL: INSERT INTO madnessred_sites (Name, url, Image, Rating) VALUES (Google, http://www.google.co.uk/, none, 5)

SQL query:

INSERT INTO madnessred_sites (Name, url, Image, Rating) VALUES (Google, http://www.google.co.uk/, none, 5)

MySQL said: Documentation
#1064 - 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 '://www.google.co.uk/, none, 5)' at line 1 

 

I don't know what other information you need, but I'll give what I think will help

 

   	 Field   	Type  	Collation  	Attributes  	Null  	Default  	Extra  	Action
 ID  	int(3) 	  	  	No  	  	auto_increment  	Change 	Drop 	Primary 	Index 	Unique 	Fulltext
 Name  	varchar(50) 	latin1_swedish_ci 	  	Yes  	NULL  	  	Change 	Drop 	Primary 	Index 	Unique 	Fulltext
 url  	varchar(100) 	latin1_swedish_ci 	  	Yes  	NULL  	  	Change 	Drop 	Primary 	Index 	Unique 	Fulltext
 Image  	varchar(100) 	latin1_swedish_ci 	  	Yes  	NULL  	  	Change 	Drop 	Primary 	Index 	Unique 	Fulltext
 Rating  	int(5) 	  	  	Yes  	NULL  	  	Change 	Drop 	Primary 	Index 	Unique 	Fulltext
 Ratings  	int(5) 	  	  	Yes  	1  	  	Change 	Drop 	Primary 	Index 	Unique 	Fulltext
 Broken  	int(1) 	  	  	Yes  	0  	  	Change 	Drop 	Primary 	Index 	Unique 	Fulltext

 

phpmyadmin 2.6.4-pl3

 

Link to comment
Share on other sites

grr no modify buttons

 

edit (thats odd, I can edit this post but no any of the ones above it, prehaps its the solved mod as it solved then unsolved this topic after finding this error)

 

any i would have edits so please excuse the double post

 

anyway here is my code

 

// Site info
$name = $_GET["name"];
$url = $_GET["url"];
$img = $_GET["img"];
$rate = $_GET["rate"];

// Lets connect
$con = mysql_connect($servername,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Lets select the database
mysql_select_db("$database", $con);

// Lets select the table
$tblsites = $prefix.'sites';

// And add to it
$query1 = 'INSERT INTO madnessred_sites (Name, url, Image, Rating) VALUES ($name, $url, $img, $rate)';
mysql_query($query1,$con);

// Now lets leave the database
mysql_close($con);

 

well part of it

 

How do I put the values into quotes? so $name becomes "$name"

 

have tried the following

 

// Site info
$name = '"'.$_GET["name"].'"';
$url = '"'.$_GET["url"].'"';
$img = '"'.$_GET["img"].'"';
$rate = '"'.$_GET["rate"].'"';

 

and also doing " to the

$query1 = 'INSERT INTO madnessred_sites (Name, url, Image, Rating) VALUES ($name, $url, $img, $rate)';

line

 

neither worked.

Link to comment
Share on other sites

Use double-quotes around your query string (php) and use single-quotes around any string values in it (mysql) -

 

$query1 = "INSERT INTO madnessred_sites (Name, url, Image, Rating) VALUES ('$name', '$url', '$img', '$rate')";

 

This does two things. The overall double-quoted string will cause php to parse variables in the string and replace them with their value. The single-quotes around string values in the query are what mysql expects.

Link to comment
Share on other sites

thanks, I would have spent days trying to work that out.

 

just one problem, it works, but it adds the entry twice

 

<?php
// DB info
$servername = "~~~";
$database = "~~~";
$username = "~~~";
$password = "~~~";
$prefix = "~~~";

// Lets check noones cheating on the rating
if ($_GET["rate"] > 5){
$ratecheck = "5";
} elseif ($_GET["rate"] < 1){
$ratecheck = "1";
} else {
$ratecheck = $_GET["rate"];
}

// Site info
$name = $_GET["name"];
$url = $_GET["url"];
$img = $_GET["img"];
$rate = $ratecheck;

// Lets connect
$con = mysql_connect($servername,$username,$password);
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

// Lets select the database
mysql_select_db("$database", $con);

// Lets select the table
$tblsites = $prefix.'sites';

// And add to it
$query1 = "INSERT INTO '$tblsites' (Name, url, Image, Rating) VALUES ('$name', '$url', '$img', '$rate')";
mysql_query($query1, $con);

// Now lets leave the database
mysql_close($con);

// Lets calculate stars

$fo = '<font color="#ff0000">';
$nt = '</font>';

if ($_GET["rate"] == "1") {
$rating = $fo.'*'.$nt.' * * * *';
} elseif ($_GET["rate"] == "2") {
$rating = $fo.'* *'.$nt.' * * *';
} elseif ($_GET["rate"] == "3") {
$rating = $fo.'* * *'.$nt.' * *';
} elseif ($_GET["rate"] == "4") {
$rating = $fo.'* * * *'.$nt.' *';
} elseif ($_GET["rate"] == "5") {
$rating = $fo.'* * * * *'.$nt;
} else {
$rating = $fo.'-';
}

// We need to create a page now for the user.
echo '<html><head><title>MadnessRed :: Add site</title></head><body bgcolor="#ff0000"><center><br /><br /><br /><table width="800" border="0" bgcolor="#cccccc"><tr><td><h3 align"cetner">You site has been added. Thankyou. Please note that we will remove any unsuitable sites.</h3><br /><br /><a href="javascript: history.go(-2)"><font color="#ff0000">Go Back</font></a><br /><br />Name: '.$name.'<br />URL: '.$url.'<br />Rating: '.$rating.'<br />Image: <img src="'.$img.'" border="0" width="128" height="102" /></td></tr></table></center></body></html>';

// DONE :-)
?>

Link to comment
Share on other sites

Your code is unconditionally executing any time the page is requested and it is being requested twice.

 

Some things that can cause this -

 

url_rewriting

a browser requesting a favicon

javascript/form that sends two requests

 

We would need to know more about how your page is being requested to be able to help.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.