SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How can I form a statement like this -

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

    create.php
    PHP Code:
    <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 Code:
    <?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?
    Working on a PHP-Oracle Admin Panel..

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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....
    Working on a PHP-Oracle Admin Panel..

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2009
    Posts
    29
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please somebody.. help!
    Working on a PHP-Oracle Admin Panel..

  4. #4
    SitePoint Wizard lorenw's Avatar
    Join Date
    Feb 2005
    Location
    was rainy Oregon now sunny Florida
    Posts
    1,094
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I think you could do it with,
    PHP: substr_replace - Manual
    What I lack in acuracy I make up for in misteaks

  5. #5
    SitePoint Zealot Zurev's Avatar
    Join Date
    Feb 2009
    Posts
    171
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Having a hard time understanding the issue, though the extra comma can be taken care of with rtrim.
    PHP Code:
    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.

  6. #6
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,401
    Mentioned
    147 Post(s)
    Tagged
    4 Thread(s)
    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.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •