2 Replies - 597 Views - Last Post: 01 October 2012 - 02:51 PM Rate Topic: -----

#1 CrankyB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-October 12

Error sql syntax, Check manual, line 1 'C'|| This is driving m

Posted 01 October 2012 - 02:09 PM

First of all, hello everyone, this is my first post and most probably not my last.
I'm a bit new to PHP and everything was going super smooth until this:

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

I've looked over my code over and over again but I can't seem to understand why its not working.
Code:
<?

$db_name = "ijdb";
$connection = @mysql_connect("localhost", "root", "1234") or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());

$sql = "CREATE TABLE $_POST[table_name] (";

for($i=0; $i < count($_POST['field_name']); $i++)
{
	$sql .= $_POST['field_name'][$i]." ".$_POST['field_type'][$i];
	
	if($_POST['field_length'][$i] != "")
	{
		$sql .= "(".$_POST['field_length'][$i].",";
	}
	else
	{
		$sql .= ",";
	}
}

$sql = substr($sql, 0, 1);
$sql .= ")";

$result = mysql_query($sql, $connection) or die(mysql_error());
if($result)
{
	$msg = "<p>".$_POST['table_name']." has been created!</p>";
}
?>
<html>
<head>
<title>
Create a Database Table: Step 3
</title>
</head>
<body>
<h1>Adding table to <? echo "$db_name"; ?>...</h1>
<? echo "$msg"; ?>
</body>
</html>



So please if you can help me understand what I'm doing wrong I'd greatly appreciate it.
I hope it's not something stupid like a misplaced quotation mark, but then again I don't
think that would be far off from everyones' first post. Anyway HELP!

Is This A Good Question/Topic? 0
  • +

Replies To: Error sql syntax, Check manual, line 1 'C'|| This is driving m

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4399
  • View blog
  • Posts: 12,255
  • Joined: 18-April 07

Re: Error sql syntax, Check manual, line 1 'C'|| This is driving m

Posted 01 October 2012 - 02:26 PM

Before line 26 in your example above, put in a few lines like...

echo "$sql";
exit;



And re-run your code. This should print out what your SQL statement looks like. I am also finding your substr() line odd. Are you sure you want to be using "1" instead of "-1"? You want to pull off the last comma right?

Also keep in mind that your for loop may never execute and $_POST may never be set if this is the first time the page is ran. You should always check if $_POST variables are set before you attempt to use them...

if (isset($_POST['somevalue'])) {
    // now you can use $_POST['somevalue'] here
    // etc...
}



But check your $sql value and make sure it is coming out to what you think it does. :)
Was This Post Helpful? 1
  • +
  • -

#3 CrankyB  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 01-October 12

Re: Error sql syntax, Check manual, line 1 'C'|| This is driving m

Posted 01 October 2012 - 02:51 PM

echo sql
exit
was a good idea, I'll be using that a lot from now on.

The culprit was the -1
its always good to have a fresh set of eyes looking at the the code.

thank you very much for your help. This made it clear to me that I have to be more tedious with my coding.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1