Current location: Hot Scripts Forums » Programming Languages » PHP » Error in syntax, I cant figure out where.

Error in syntax, I cant figure out where.

Reply
  #1 (permalink)  
Old 06-30-06, 01:09 AM
breenthemachine breenthemachine is offline
New Member
 
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Error in syntax, I cant figure out where.

Here is the code where I am recieving my error.

do_createtable.php

PHP Code:
<?
//indicate the database you want to use
$db_name "testDB";

//connect to database
$connection = @mysql_connect("localhost","breen","area") or die(mysql_error());
$db = @mysql_select_db($db_name$connection) or die(mysql_error());

//start creating the SQL statement
$sql "CREATE TABLE $_POST[table_name]  (";

//continue the SQL statement for each new field
for ($i 0$i count($_POST[field_name]); $i++)  {
    
$sql .= $_POST[field_name][$i]." ".$_POST[field_type][$i];

    if (
$_POST[auto_increment][$i] == "Y")  {
$additional "NOT NULL auto_increment";
} else {
    
$additional "";
}

if (
$_POST[primary][$i] == "Y") {
$additional .= ", primary key (".$_POST[field_name][$i].")";
 } else {
    
$additional "";
}



    if (
$_POST [field_length][$i] != "") {
        
$sql .= " (".$_POST[field_length][$i].") $additional ,";
    } else {
        
$sql .= $additional ,";
    }
}

//clean up the end of the string
$sql substr($sql0, -1);
$sql .= ")";

//execute the query
$result mysql_query($sql$connection) or die(mysql_error());

//get a good message for display upon success
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>
The error I get is:

You have an error in your SQL syntax near ')' at line 1

Obviously, my code is dying. What it should be doing is adding a table to "testDB". I use a form page where I enter in the field name, the field type, the field length, and whether it is a primary key and/or auto increment. Then I click the submit button where it should create my table no problem. I get this error instead.

If anyone could point me in the right direction, that would be great.

Thanks.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 06-30-06, 02:21 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 2,861
Thanks: 0
Thanked 9 Times in 9 Posts
try echoing the query before entering it into mysql. most of te time you'll see what's wrong

just a small note: if you are going to allow user's to create a table, i don't think that's very wise: it is better to do it yourself, for security reasons.

Show me the query, and i'll see what's wrong
UnrealEd
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

If you want to add me on any IM, pm me first
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 06-30-06, 11:04 AM
breenthemachine breenthemachine is offline
New Member
 
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by UnrealEd
try echoing the query before entering it into mysql. most of te time you'll see what's wrong

just a small note: if you are going to allow user's to create a table, i don't think that's very wise: it is better to do it yourself, for security reasons.

Show me the query, and i'll see what's wrong
UnrealEd
I totally understand the severity in security. Actually, this is not for users to use, its for my use. Im trying to teach myself PHP, since that is one language on my list to learn, and I have a book telling me to enter this code, and obviously, the book is wrong. I have checked over the code mulitple times to verify if i have what the book has, and I even tried using previous programming knowledge to see if I could figure out the error. I dont know if this is what you wanted but I put this line

PHP Code:
echo "$sql"
and commented out the execution statement. This is the result I get when its ran.

CREATE TABLE)
Adding table to testDB...

I double checked everything to try to see if the syntax is correct and I guess if there is a mistake, im just not that PHP savvy to find it. Thanks for your help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 06-30-06, 11:25 AM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
UnrealEd: Hope you don't mind if I jump in here.

It appears that the $_POST variables are empty, probably due to a mismatch in the names used or some problem in the form code. If you post the form code it would help.

You can also add the following, right after the <? opening PHP tag, to get PHP to tell you more info about what is going on and to show you the actual POST variables -
PHP Code:
error_reporting(E_ALL); // show all notices, warnings, and errors
echo "<pre>";
print_r($_POST); // show the actual post variables
echo "</pre>"
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 06-30-06, 09:13 PM
breenthemachine breenthemachine is offline
New Member
 
Join Date: Jun 2006
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks. That worked. I entered in the code you told me to enter, and looked at the do_showfielddef.php and noticed this error:

PHP Code:
<INPUT TYPE=\"hidden \" NAME=\"table_name \" VALUE=\"$_POST[table_name]\"> 
The extra spaces were what was giving me troubles. Thanks for all of your help.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Share on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
MySQL Syntax error gigafare PHP 4 04-19-06 02:03 AM
MySQL Syntax error when using a variable for table name lppa2004 PHP 4 08-04-05 12:24 AM
Can't find error in sql syntax. Dr.Jamescook PHP 7 06-14-05 11:45 AM
You have an error in your SQL syntax. Help! SevEre PHP 7 09-08-04 05:05 AM
eval variables syntax banquet PHP 1 07-09-03 04:49 AM


All times are GMT -5. The time now is 09:08 AM.
vBulletin® Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.