Jump to content

How can I form a statement like this -


ragavbpl

Recommended Posts

Before I explain my problem here are the files I have created -

 

create.php

 

<form action="#" method="post"> 
Name:<input type="text" name="tab"/>Number of Fields:<input tabindex="text" name="colnum"/> 
<input type="submit" /> 
</form> 
<?php 

if(isset($_POST['tab']) and isset($_POST['colnum'])){ 
    if($_POST['colnum']>0) 
    { 
        echo '<form action="createn.php" method="post">'; 
        foreach(range(1,$_POST['colnum']) as $index) 
        { 
            echo $index; 
            echo ') ColName<input type="text" name="colname'.$index.'"> '; 
            echo 'Type<input type="text" name="type'.$index.'">'; 
            echo '<input type="hidden" name="tab" value="'.$_POST['tab'].'"/>'; 
            echo '<input type="hidden" name="colnum" value="'.$_POST['colnum'].'"/>'; 
            echo '<br/>'; 
             
        } 
        echo '<input type="submit"/>'; 
        echo '</form>'; 
    } 
    else 
    echo "Number of fields should be greater then 0"; 
} 
else 
{ 
    echo "Fill in both the fields"; 
} 
?>

 

createn.php

<?php 
$conn=oci_connect('scott','tiger'); 
$tabname=$_POST['tab']; 
foreach(range(1,$_POST['colnum']) as $index) 
{ 
    echo $_POST["colname".$index]; 
    echo $_POST["type".$index]; 
        echo '<br/>'; 
} 

echo "Table name =".$tabname; 

$query='create table '.$tabname.'('.$_POST["colname1"].' '.$_POST["type1"].')'; 
$statement=oci_parse($conn,$query); 
if(oci_execute($statement)) 
{ 
    echo "Table created"; 
} 
else 
{ 
    echo 'no table created'; 
} 
?>

 

Create.php explanation ->

On running it gives a form asking for table name and number of fields:

Suppose I enter table name as "EMP" and no. of fields as "2".. and press submit.. now on the same page I'll get two sets of field each containing 2 boxes (ie Name and Type)..

When I fill in both the field set with let's say -

1) Name: -"empno" Type: -"number"

2) Name: -"ename" Type: -"varchar2"

and press submit it the form submits the values to "createn.php"

 

Createn.php explanation ->

On this file I want to form a statement to create a new table with all the inputed data... ie something as follows -

"create table <table_name>(<column 1> <type1>,<column 2> <type2>,....)"

 

The code I have written in this file puts only 1 column and it's type as I have manually entered it. But suppose I don't know how many column is user going to put in.. then in that case how can I code the above stated statement in bold?

Link to comment
https://forums.phpfreaks.com/topic/233190-how-can-i-form-a-statement-like-this/
Share on other sites

<form action="#" method="post"> 
Name:<input type="text" name="tab"/>Number of Fields:<input tabindex="text" name="colnum"/> 
<input type="submit" /> 
</form> 
<?php 

if(isset($_POST['tab']) and isset($_POST['colnum'])){ 
    if($_POST['colnum']>0) 
    { 
        echo '<form action="createn.php" method="post">'; 
        foreach(range(1,$_POST['colnum']) as $index) 
        { 
            echo $index; 
            echo ') ColName<input type="text" name="colname'.$index.'"> '; 
            echo 'Type<input type="text" name="type'.$index.'">'; 
            echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; 
            echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; 
           echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; 
            echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; 
           echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; 
            echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; 
           echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; 
            echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; 
            echo '<br/>'; 
             
        } 
        echo '<input type="submit"/>'; 
        echo '</form>'; 
    } 
    else 
    echo "Number of fields should be greater then 0"; 
} 
else 
{ 
    echo "Fill in both the fields"; 
} 
?>

 

<?php 
$conn=oci_connect('scott','tiger'); 
$i=0
$col = $_POST['tab'];
foreach ($_POST['tab'] as $tab) {
$tabname=$tab; $colnum= $col[$i];
foreach(range(1,$_POST['colnum']) as $index) 
{ 
    echo $_POST["colname".$index]; 
    echo $_POST["type".$index]; 
        echo '<br/>'; 
} 

echo "Table name =".$tabname; 

$query='create table '.$tabname.'('.$_POST["colname1"].' '.$_POST["type1"].')'; 
$statement=oci_parse($conn,$query); 
if(oci_execute($statement)) 
{ 
    echo "Table created"; 
} 
else 
{ 
    echo 'no table created'; 
} $i++;}
?>

Ted

<form action="#" method="post"> 
Name:<input type="text" name="tab"/>Number of Fields:<input tabindex="text" name="colnum"/> 
<input type="submit" /> 
</form> 
<?php 

if(isset($_POST['tab']) and isset($_POST['colnum'])){ 
    if($_POST['colnum']>0) 
    { 
        echo '<form action="createn.php" method="post">'; 
        foreach(range(1,$_POST['colnum']) as $index) 
        { 
            echo $index; 
            echo ') ColName<input type="text" name="colname'.$index.'"> '; 
            echo 'Type<input type="text" name="type'.$index.'">'; 
            echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; 
            echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; 
           echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; 
            echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; 
           echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; 
            echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; 
           echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>'; 
            echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>'; 
            echo '<br/>'; 
             
        } 
        echo '<input type="submit"/>'; 
        echo '</form>'; 
    } 
    else 
    echo "Number of fields should be greater then 0"; 
} 
else 
{ 
    echo "Fill in both the fields"; 
} 
?>

 

<?php 
$conn=oci_connect('scott','tiger'); 
$i=0
$col = $_POST['tab'];
foreach ($_POST['tab'] as $tab) {
$tabname=$tab; $colnum= $col[$i];
foreach(range(1,$_POST['colnum']) as $index) 
{ 
    echo $_POST["colname".$index]; 
    echo $_POST["type".$index]; 
        echo '<br/>'; 
} 

echo "Table name =".$tabname; 

$query='create table '.$tabname.'('.$_POST["colname1"].' '.$_POST["type1"].')'; 
$statement=oci_parse($conn,$query); 
if(oci_execute($statement)) 
{ 
    echo "Table created"; 
} 
else 
{ 
    echo 'no table created'; 
} $i++;}
?>

Ted

 

What changes you made to script? Could you please point them....

After thinking a bit, I came up with this -

PHP Code:

echo 'create table '.$tabname.'(';foreach(range(1,$_POST['colnum']) as $index)
{
    echo $_POST["colname".$index].' '.$_POST["type".$index].',';
}
echo ')';  

But this will just output the string in the form I want i.e.-

create table emp(empno number,ename varchar2,)

with an extra ',' at last..

 

But I want this full string to be inside a variable like -

$query = create table emp(empno number,ename varchar2)

and without that extra comma(',')..

 

Waiting for your replies people....

Since you have infinite number of columns, the form submits the information much like an array:

 

          echo '<input type="hidden" name="tab[]" value="'.$_POST['tab'].'"/>';

            echo '<input type="hidden" name="colnum[]" value="'.$_POST['colnum'].'"/>';

 

and to get the information one by one from the array:

foreach ($_POST['tab'] as $tab) {...}

foreach loop is needed.

Ted

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.