PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
bironeb
Forum Commoner
Posts: 59 Joined: Thu Nov 20, 2003 12:02 pm
Post
by bironeb » Thu Apr 01, 2004 11:59 am
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?
Deemo
Forum Contributor
Posts: 418 Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC
Post
by Deemo » Thu Apr 01, 2004 4:01 pm
Code: Select all
<?php
$sql = "CREATE TABLE '{$_POST['table_name']}' (";
?>
you also need braces, so the code above will work
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
bironeb
Forum Commoner
Posts: 59 Joined: Thu Nov 20, 2003 12:02 pm
Post
by bironeb » Thu Apr 01, 2004 5:00 pm
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?
magicrobotmonkey
Forum Regular
Posts: 888 Joined: Sun Mar 21, 2004 1:09 pm
Location: Cambridge, MA
Post
by magicrobotmonkey » Thu Apr 01, 2004 5:16 pm
try
CREATE TABLE $_POST[table_name]
thats what i do and it seems to work fine
Deemo
Forum Contributor
Posts: 418 Joined: Sun Jan 18, 2004 11:48 am
Location: Washington DC
Post
by Deemo » Thu Apr 01, 2004 5:55 pm
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
that shud do it
markl999
DevNet Resident
Posts: 1972 Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)
Post
by markl999 » Thu Apr 01, 2004 6:34 pm
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*
Illusionist
Forum Regular
Posts: 903 Joined: Mon Jan 12, 2004 9:32 pm
Post
by Illusionist » Thu Apr 01, 2004 6:50 pm
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;
Zay
Forum Newbie
Posts: 22 Joined: Tue Mar 23, 2004 3:42 pm
Post
by Zay » Fri Apr 02, 2004 1:12 am
Deemo wrote: Code: Select all
<?php
$sql = "CREATE TABLE '{$_POST['table_name']}' (";
?>
you also need braces, so the code above will work
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
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.
kettle_drum
DevNet Resident
Posts: 1150 Joined: Sun Jul 20, 2003 9:25 pm
Location: West Yorkshire, England
Post
by kettle_drum » Fri Apr 02, 2004 1:15 am
Note: Dont just put $_POST[value] into an sql statment. Check the value first.