Current location: Hot Scripts Forums » Programming Languages » PHP » Create table name with user input


Create table name with user input

Reply
  #1 (permalink)  
Old 05-16-11, 08:16 AM
faisals999 faisals999 is offline
Newbie Coder
 
Join Date: May 2011
Location: Sargodha, Pakistan
Posts: 27
Thanks: 4
Thanked 0 Times in 0 Posts
Create table name with user input

hi all,,,
I am trying to make a table in php Mu SQL database.. and i want to name it by using user input. means user has given some input and i have stored it in a variable flight. now i am using this query

'CREATE TABLE $flight (`name` VARCHAR(30) NOT NULL, `num_pass` INT(2) NOT NULL, `address` VARCHAR(30) NOT NULL, `contact` VARCHAR(21) NOT NULL,`flight_no` VARCHAR(30) NOT NULL,`pass_id` INT( 9 ) NOT NULL AUTO_INCREMENT PRIMARY KEY)'

but it is not working...... please someone help me urgently........
Reply With Quote
  #2 (permalink)  
Old 05-16-11, 10:02 AM
smithygotlost smithygotlost is offline
Aspiring Coder
 
Join Date: Jul 2006
Location: United Kingdom
Posts: 413
Thanks: 12
Thanked 3 Times in 3 Posts
This will do it

PHP Code:

<?php
// REMOVE AFTER TESTING AND SET USING INPUT DATA
$fight "test1";


if (isset(
$fight)) {
mysql_query("CREATE TABLE $fight (`name` VARCHAR(30) NOT NULL, `num_pass` INT(2) NOT NULL, `address` VARCHAR(30) NOT NULL, `contact` VARCHAR(21) NOT NULL,`flight_no` VARCHAR(30) NOT NULL,`pass_id` INT( 9 ) NOT NULL AUTO_INCREMENT PRIMARY KEY)")or die(mysql_error());
} else {
    echo
" Error";
}
?>
Tested and works.

Last edited by smithygotlost; 05-16-11 at 10:02 AM. Reason: edit
Reply With Quote
  #3 (permalink)  
Old 05-16-11, 12:24 PM
faisals999 faisals999 is offline
Newbie Coder
 
Join Date: May 2011
Location: Sargodha, Pakistan
Posts: 27
Thanks: 4
Thanked 0 Times in 0 Posts
thanx but unfortunately it also is not working..........
Reply With Quote
  #4 (permalink)  
Old 05-16-11, 12:53 PM
smithygotlost smithygotlost is offline
Aspiring Coder
 
Join Date: Jul 2006
Location: United Kingdom
Posts: 413
Thanks: 12
Thanked 3 Times in 3 Posts
then you are setting it wrong, Ive tested this code.. something is wrong...

Has the mysql user got the right permissions ?
Is your database connect working correctly ?
What errors are you getting ?

To test for errors use this

PHP Code:


<?php
// REMOVE AFTER TESTING AND SET USING INPUT DATA
$fight "test1";


if (isset(
$fight)) {
mysql_query("CREATE TABLE $fight (`name` VARCHAR(30) NOT NULL, `num_pass` INT(2) NOT NULL, `address` VARCHAR(30) NOT NULL, `contact` VARCHAR(21) NOT NULL,`flight_no` VARCHAR(30) NOT NULL,`pass_id` INT( 9 ) NOT NULL AUTO_INCREMENT PRIMARY KEY)")or die(mysql_error());
  echo
" Created";
} else {
    echo
" Error Did not insert";
}
?>
if it shows created, then there is something wrong with database permissions.

Let me know outcome

Mike
Reply With Quote
  #5 (permalink)  
Old 05-16-11, 07:19 PM
faisals999 faisals999 is offline
Newbie Coder
 
Join Date: May 2011
Location: Sargodha, Pakistan
Posts: 27
Thanks: 4
Thanked 0 Times in 0 Posts
Sir basically i am trying to implement following code


PHP Code:

$flight=$_POST['flight'];

$name=$_POST['name'];
$pass=$_POST['no'];
$address=$_POST['address'];
$contact=$_POST['contactnumber'];



$query=mysql_query('CREATE TABLE \''.$flight.'\' (`name` VARCHAR(30) NOT NULL, `num_pass` INT(2) NOT NULL, `address` VARCHAR(30) NOT NULL, `contact` VARCHAR(21) NOT NULL,`flight_no` VARCHAR(30) NOT NULL,`pass_id` INT( 9 ) NOT NULL AUTO_INCREMENT PRIMARY KEY)');

if(!
$query)
{
    
$insert=mysql_query('INSERT INTO \''.$flight.'\' VALUES ("$name","$pass","$address","$contact","$flight","")');
}
else
{
    
$insert=mysql_query('INSERT INTO \''.$flight.'\' VALUES ("$name","$pass","$address","$contact","$flight","1")');


but it also is not working... it is creating a table with name "$flight".. kindly do it right if possible.........
I will wait for your answer
Reply With Quote
  #6 (permalink)  
Old 05-16-11, 08:25 PM
faisals999 faisals999 is offline
Newbie Coder
 
Join Date: May 2011
Location: Sargodha, Pakistan
Posts: 27
Thanks: 4
Thanked 0 Times in 0 Posts
the code you have mentioned earlier is giving following error

Quote:
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 '-345 (`name` VARCHAR(30) NOT NULL, `num_pass` INT(2) NOT NULL, `address` VARCHAR' at line 1
Reply With Quote
  #7 (permalink)  
Old 05-17-11, 02:10 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,075
Thanks: 11
Thanked 88 Times in 83 Posts
Please, for the love of God, escape your user input.

PHP Code:

$flight mysql_real_escape_string($_POST['flight']); 

READ THIS: PHP: mysql_real_escape_string - Manual
Reply With Quote
  #8 (permalink)  
Old 05-17-11, 06:52 AM
faisals999 faisals999 is offline
Newbie Coder
 
Join Date: May 2011
Location: Sargodha, Pakistan
Posts: 27
Thanks: 4
Thanked 0 Times in 0 Posts
I have tried this and its working............
thanx
Reply With Quote
Reply

Bookmarks

Tags
create, input, php, table, user


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
Error when trying to create MySQL table via PHP HasansWeb PHP 5 05-19-11 06:59 AM
Create table via PHP ruteckycs PHP 8 01-02-10 02:50 PM
create new table using sql from ASP web tlgan77 ASP 0 03-07-06 07:48 AM
Preventing multiple user from loging in using the same username and password digioz PHP 2 09-14-05 01:53 PM
How come Account_Type and Organization cannot accept user input? toezaurus81 C/C++ 1 07-30-04 01:59 PM


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