Page 1 of 1

Help with Parse Error:

Posted: Thu Apr 01, 2004 11:59 am
by bironeb
Here is the error im getting :

Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING on line 10

Here is my code:

Code: Select all

<?
//indicate the database you want to use
$db_name ="Inventory";

//connect to database
$connection = @mysql_connect("localhost","byron","byronb") 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($sql,0,-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>
Line 10 is :

Code: Select all

$sql = "CREATE TABLE $_POST['table_name'] (";
Any ideas?

Posted: Thu Apr 01, 2004 12:25 pm
by magicrobotmonkey
lose the single quotes in the $_POST

Posted: Thu Apr 01, 2004 4:01 pm
by Deemo

Code: Select all

<?php
$sql = "CREATE TABLE '{$_POST['table_name']}' ("; 
?>
you also need braces, so the code above will work :wink:
Whenever you want to use arrays in sql commands you need them in braces and quotes

also, magicrobotmonkey, i dont see why he should lose the single quotes. thats what i do :wink:

Posted: Thu Apr 01, 2004 5:00 pm
by bironeb
I changed that line to:

Code: Select all

$sql = "CREATE TABLE '{$_POST['table_name']}' (";
And now I get these Errors:

Notice: Undefined index: table_name on line 11

Notice: Undefined index: field_name on line 14
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


Is something wrong with my SQL syntax? and why doesn't it know what table_name is now?

Posted: Thu Apr 01, 2004 5:16 pm
by magicrobotmonkey
try

CREATE TABLE $_POST[table_name]

thats what i do and it seems to work fine

Posted: Thu Apr 01, 2004 5:55 pm
by Deemo
my best guess is to maybe try changing line 10 to this:

Code: Select all

$sql = "CREATE TABLE {$_POST['table_name']} (";
no quotes around the {}, because when you are creating as table, there are no quotes around the name :wink:
that shud do it

Posted: Thu Apr 01, 2004 6:34 pm
by markl999
Notice: Undefined index: table_name on line 11
Notice: Undefined index: field_name on line 14
Sounds like those form variable are not set, as if the form wasn't posted or the elements names are something else. *shrug*

Posted: Thu Apr 01, 2004 6:50 pm
by Illusionist
if that is your full script, then why in the world are you not getting any errors around here:

Code: Select all

<?
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>
I can see at least 3, maybe more.
1) YOu can't just have HTML directly in your PHP code. try closing that begining php with a ?> befor <HTML>
2) you never closed <? echo "$db_name";, with ?> and you need to
3) same as above, only with $msg. You need to close that <? echo $msg;

Posted: Fri Apr 02, 2004 1:12 am
by Zay
Deemo wrote:

Code: Select all

<?php
$sql = "CREATE TABLE '{$_POST['table_name']}' ("; 
?>
you also need braces, so the code above will work :wink:
Whenever you want to use arrays in sql commands you need them in braces and quotes

also, magicrobotmonkey, i dont see why he should lose the single quotes. thats what i do :wink:

Code: Select all

<?php
$sql = "CREATE TABLE ".$_POST['table_name']."(";
?>
should work too...

also...
Notice: Undefined index: table_name on line 11
Notice: Undefined index: field_name on line 14
should indeed be the variables not set.
so either the names of the form and the php code are not the same. or you manually went to the page without submitting any post data.

Posted: Fri Apr 02, 2004 1:15 am
by kettle_drum
Note: Dont just put $_POST[value] into an sql statment. Check the value first.