PHP MySQL error code
Moderator: General Moderators
-
copachumpy
- Forum Newbie
- Posts: 11
- Joined: Thu Apr 08, 2004 5:10 am
PHP MySQL error code
Hi,
I am very new to both PHP and MySQL.
I have some code that I am working on, with the help of a text book.
I get this strange error code:
"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"
What does this mean, as I can not work out it, and it's driving me crazy.
What's making it so confusing is that at Line 1, all I have written is the <html> tag. and if I move the HTML code so it is after my PHP code, I still get the same error!
Please help
Thanks
John
I am very new to both PHP and MySQL.
I have some code that I am working on, with the help of a text book.
I get this strange error code:
"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"
What does this mean, as I can not work out it, and it's driving me crazy.
What's making it so confusing is that at Line 1, all I have written is the <html> tag. and if I move the HTML code so it is after my PHP code, I still get the same error!
Please help
Thanks
John
-
malcolmboston
- DevNet Resident
- Posts: 1826
- Joined: Tue Nov 18, 2003 1:09 pm
- Location: Middlesbrough, UK
-
copachumpy
- Forum Newbie
- Posts: 11
- Joined: Thu Apr 08, 2004 5:10 am
Sorry, I didnt include the code.
Here is the code that is causing me this little but very annoying problem:
Here is the code that is causing me this little but very annoying problem:
Code: Select all
<?php
<HTML>
<HEAD>
<TITLE>Create a Database Table: Step 3</TITLE></HEAD>
<BODY>
<h1>Adding table <?php echo "$_POST[table_name]"; ?></h1><?
$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 .= ")";
// create connection
$conn = mysql_connect("host","user","pass") or die(mysql_error());
// select database
$db = mysql_select_db("testDB", $conn) or die(mysql_error());
// execute SQL query and get result
$sql_result = mysql_query($sql,$conn) or die(mysql_error());
//print success message
if ($sql_result) {
echo "<P>$_POST[table_name] has been created!</p>";
}
?>
</BODY>
</HTML>
?>you dont have a ( in the SQL ...
Code: Select all
$sql = "CREATE TABLE $_POST[table_name] ( ";-
copachumpy
- Forum Newbie
- Posts: 11
- Joined: Thu Apr 08, 2004 5:10 am
this line is wrong
needs to be..
That goes for your other SQL statements
Also read Array do's and don'ts - Why is $foo[bar] wrong?
Mark
Code: Select all
$sql = "CREATE TABLE $_POST[table_name] ";Code: Select all
$sql = "CREATE TABLE ".$_POST['table_name']."";Also read Array do's and don'ts - Why is $foo[bar] wrong?
Mark
actually, there are so many errors in the code, i have rewritten it correctly for you.
You may still need to implement Wayne's suggestions.
Mark
Code: Select all
<HTML>
<HEAD>
<TITLE>Create a Database Table: Step 3</TITLE></HEAD>
<BODY>
<h1>Adding table <?php echo $_POST[table_name]; ?></h1>
<?php
$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 .= ")";
// create connection
$conn = mysql_connect("host","user","pass") or die(mysql_error());
// select database
$db = mysql_select_db("testDB", $conn) or die(mysql_error());
// execute SQL query and get result
$sql_result = mysql_query($sql,$conn) or die(mysql_error());
//print success message
if ($sql_result) {
echo "<P>".$_POST['table_name']." has been created!</p>";
}
?>
</BODY>
</HTML>Mark
To help with error reporting, change
It's good practice to do what you did by echoing the error, but by echoing the query, you can see exactly what MySQL is getting as a query, and you can see where the problem is.
Code: Select all
//this
$sql_result = mysql_query($sql,$conn) or die(mysql_error());
//to this
$sql_result = mysql_query($sql,$conn) or die($sql);Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
-
copachumpy
- Forum Newbie
- Posts: 11
- Joined: Thu Apr 08, 2004 5:10 am
OK,
I have implemented all of the suggestions, and thank you very much for all your help.
As I am very new to PHP and I'm trying to get this working for my job, could you help me work out why I'm still getting errors!
my complete code is as follows:
There is still an error on line 4, and I don't understand enough to work it out!
Thanks for all your help
John
I have implemented all of the suggestions, and thank you very much for all your help.
As I am very new to PHP and I'm trying to get this working for my job, could you help me work out why I'm still getting errors!
my complete code is as follows:
Code: Select all
<?php
<?
$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 .= ")";
// create connection
$conn = mysql_connect("devoke.ibb.gov","mysqladmin","risc2me") or die($sql);
// select database
$db = mysql_select_db("testDB", $conn) or die($sql);
// execute SQL query and get result
$sql_result = mysql_query($sql,$conn) or die($sql);
//print success message
if ($sql_result) {
echo "<P>$_POST[table_name] has been created!</p>";
}
?>
<HTML>
<HEAD>
<TITLE>Create a Database Table: Step 3</TITLE></HEAD>
<BODY>
<h1>Adding table
<?php echo $_POST[table_name]; ?>
</h1>
</BODY>
</HTML>
?>Thanks for all your help
John
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
well this isn't a valid statement:I think you may want to replace it with:
Code: Select all
"".$_POST['field_name'][$i] $_POST['field_type'][$i]."";Code: Select all
"$_POST[field_name][$i] $_POST[field_type][$i]";-
copachumpy
- Forum Newbie
- Posts: 11
- Joined: Thu Apr 08, 2004 5:10 am
I changed the code, and now I get a page with NO error codes on, but it does display a message :
CREATE TABL)
and yet no table has been created in the database...
the code is as follows:
Thanks for the help, I guess my next step is start learning this so I don't have to keep asking for help!
CREATE TABL)
and yet no table has been created in the database...
the code is as follows:
Code: Select all
<?
$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 .= ")";
// create connection
$conn = mysql_connect("host","user","pass") or die($sql);
// select database
$db = mysql_select_db("testDB", $conn) or die($sql);
// execute SQL query and get result
$sql_result = mysql_query($sql,$conn) or die($sql);
//print success message
if ($sql_result) {
echo "<P>$_POST[table_name] has been created!</p>";
}
?>
<HTML>
<HEAD>
<TITLE>Create a Database Table: Step 3</TITLE></HEAD>
<BODY>
<h1>Adding table to
<?php echo $_POST[table_name]; ?>
</h1>
</BODY>
</HTML>
<?php
?>- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
well that tells me that:
fails to run. I'd bet that $_POST['field_name'] is not an array.
Code: Select all
for ($i = 0; $i < count($_POST['field_name']); $i++) {-
copachumpy
- Forum Newbie
- Posts: 11
- Joined: Thu Apr 08, 2004 5:10 am