How can I form a statement like this -

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?

After thinking a bit, I came up with this -

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…

Please somebody… help!

I think you could do it with,
PHP: substr_replace - Manual

Having a hard time understanding the issue, though the extra comma can be taken care of with rtrim.


rtrim($value, ",");

You may consider wrapping it into a function, and using func_num_args/func_get_arg(s) to allow them to enter as many as they would like and your function can handle it.

Instead of echoing, put all the pieces of the query in a variable ($query). Remember you have to execute the query to actually insert something.