Results 1 to 5 of 5

Thread: table creator

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    england
    Posts
    161

    table creator

    ok, hopefully you can see what this does... anyway, it doesnt work can you tell me why? i've checked and checked but cant see why

    PHP Code:
    <html><head><title>Creating a table</title></head>
    <body>
    <?php

    if( !$_POST[fields] and !$_POST[db] )
    {
    $form ="<form action=\"create_table.php\" method=\"post\">";
    $form.="How many fields are needed in the new table?";
    $form.="<br><input type=\"text\" name=\"fields\">";
    $form.="<input type=\"submit\" value=\"Submit\">";
    echo(
    $form);
    }

    else if( !
    _POST[db] )
    {
    $form ="<form action=\"create_table.php\" method=\"post\">";
    $form.="Database: <input type=\"text\" name=\"db\"><br>";
    $form.="Table Name: <input type\"text\" name=\"table\"><br>";

    for (
    $i $i <$_POST[fields]; $i++) {$form.=
    "Column Name: <input type=\"text\" name=\"name[$i]\">";
    $form.="Type: <select name=\"type[$i]\">";
    $form.="<option value=\"char\">char</option>";
    $form.="<option value=\"int\">int</option>";
    $form.=</select";
    $form.="Size:<input type=\"text\" name=\"size[$i]\">"; }

    $form.="<input type=\"submit\" value=\"Submit\">";
    $form.="</form>"; echo($form);
    }

    else
    {
    $conn = @mysql_connect("localhost""shaun""password")
    or die(
    "Err:Conn");
    $rs = @mysql_select_db($_POST[db], $conn) or die("Err:Db");
    $sql "create table $_POST[table] (";
    for (
    $i 0$i count($_POST[name]); $i++)
    {
    $sql .= "$name[$i] $type[$i]";
    if((
    $_POST[type][$i] =="char") or ($_POST[type][$i] =="varchar"))
    {
    if(
    $_POST[size][$i] != "") ){ $sql.="($_POST[size][$i])"; }
    }
    if((
    $i+1) != count($_POST[name]) ){ $sql.=","; }
    $sql .= ")";

    echo(
    "SQL COMMAND: $sql <hr>");

    $result mysql_query($sql,$conn) or die("Err:Query");

    if (
    $result)
    { echo(
    "RESULT: table \"$_POST[table]\" has been created"); }
    }
    ?> </body> </html>
    Please help, thanks.

  2. #2
    Hyperactive Member Sgt-Peppa's Avatar
    Join Date
    Mar 2003
    Location
    Munich - Germany
    Posts
    476
    this fixed your syntax:

    PHP Code:
    <html>
        <head>
            <title>Creating a table</title>
        </head> 
        <body> 
        <?php 

            
    if( !$_POST['fields'] and !$_POST['db'] ) 
            { 
                
    $form ="<form action=\"create_table.php\" method=\"post\">"
                
    $form.="How many fields are needed in the new table?"
                
    $form.="<br><input type=\"text\" name=\"fields\">"
                
    $form.="<input type=\"submit\" value=\"Submit\">"
                echo(
    $form); 
            } 

            elseif( !
    $_POST['db'] ) 
            { 
                
    $form ="<form action=\"create_table.php\" method=\"post\">"
                
    $form.="Database: <input type=\"text\" name=\"db\"><br>"
                
    $form.="Table Name: <input type\"text\" name=\"table\"><br>"

                for (
    $i $i <$_POST['fields']; $i++) 
                {
                    
    $form.= "Column Name: <input type=\"text\" name=\"name[$i]\">"
                    
    $form.="Type: <select name=\"type[$i]\">"
                    
    $form.="<option value=\"char\">char</option>"
                    
    $form.="<option value=\"int\">int</option>"
                    
    $form.="</select> "
                    
    $form.="Size:<input type=\"text\" name=\"size[$i]\">"
                } 
                
    $form.="<input type=\"submit\" value=\"Submit\">"
                
    $form.="</form>"; echo($form); 
            } 

            else 
            { 
                
    $conn = @mysql_connect("localhost""shaun""password"
                or die(
    "Err:Conn"); 
                
    $rs = @mysql_select_db($_POST['db'], $conn) or die("Err<img src=\"images/smilies/biggrin.gif\" border=\"0\" alt=\"\">b"); 
                
    $sql "create table $_POST[table] ("
                for (
    $i 0$i count($_POST[name]); $i++) 
                { 
                    
    $sql .= "$name[$i] $type[$i]"
                    if((
    $_POST['type'][$i] =="char") or ($_POST['type'][$i] =="varchar")) 
                    { 
                        if(
    $_POST['size'][$i] != ""
                        { 
                            
    $sql.="($_POST[size][$i])"
                        } 
                    } 
                    if((
    $i+1) != count($_POST['name']) )
                    { 
                        
    $sql.=","
                    } 
                } 
                
    $sql .= ")"
                echo(
    "SQL COMMAND: $sql <hr>"); 
                
    $result mysql_query($sql,$conn) or die("Err:Query"); 

                if (
    $result
                { 
                    echo(
    "RESULT: table \"$_POST[table]\" has been created"); 
                } 
            } 
        
    ?> 
        </body> 
    </html>
    Stephan
    Keep Smiling - even if its hard
    Frankie Says Relax, wossname Says Yeah!
    wossname:--Currently I'm wearing a gimp suit and a parachute.
    C# - Base64 Blog

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    england
    Posts
    161
    thankyou very much.

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2003
    Location
    england
    Posts
    161
    argh, it doesnt work right. the first 2 pages are fine, but on the 3rd page...
    say i want an integer, and 2 character fields, i get this as the SQL query:

    SQL COMMAND: create table test ( , (Array[1]), (Array[2]))

    i guess i'm gonna have to tinker about with this code....

    ....can anyone see what's wrong?
    Last edited by shaunyboy; Dec 29th, 2003 at 08:21 AM.

  5. #5
    New Member
    Join Date
    Mar 2008
    Posts
    1

    Re: table creator

    <html>
    <head>
    <title>Creating a table</title>
    </head>
    <body>
    <?php
    $fields=$_POST['fields'];
    $db= $_POST['db'];
    $table=$_POST['table'];
    $name=$_POST['name'];
    $type=$_POST['type'];
    $size=$_POST['size'];

    if( !$fields and !$db)
    {
    $form ="<form action=\"create_table.php\" method=\"post\">";
    $form.="How many fields are needed in the new table?";
    $form.="<br><input type=\"text\" name=\"fields\">";
    $form.="<input type=\"submit\" value=\"Submit\">";
    echo($form);
    }

    elseif( !$db )
    {
    $form ="<form action=\"create_table.php\" method=\"post\">";
    $form.="Database: <input type=\"text\" name=\"db\"><br>";
    $form.="Table Name: <input type\"text\" name=\"table\"><br>";

    for ($i = 0 ; $i <$fields; $i++)
    {
    $form.= "Column Name: <input type=\"text\" name=\"name[$i]\">";
    $form.="Type: <select name=\"type[$i]\">";
    $form.="<option value=\"char\">char</option>";
    $form.="<option value=\"int\">int</option>";
    $form.="</select> ";
    $form.="Size:<input type=\"text\" name=\"size[$i]\">";
    }
    $form.="<input type=\"submit\" value=\"Submit\">";
    $form.="</form>"; echo($form);
    }

    else
    {
    $conn = @mysql_connect("localhost", "root", "")
    or die("Err:Conn");
    $rs = @mysql_select_db($db, $conn) or die("Err<img src=\"images/smilies/biggrin.gif\" border=\"0\" alt=\"\">b");
    $sql = "create table $table (";
    for ($i = 0; $i < count($name); $i++)
    {
    $sql .= "$name[$i] $type[$i]";
    if(($type[$i] =="char") or ($type[$i] =="varchar"))
    {
    if($size[$i] != "")
    {
    $sql.="($size[$i])";
    }
    }
    if(($i+1) != count($name) )
    {
    $sql.=",";
    }
    }
    $sql .= ")";
    echo("SQL COMMAND: $sql <hr>");
    $result = mysql_query($sql,$conn) or die("Err:Query");

    if ($result)
    {
    echo("RESULT: table \"$table\" has been created");
    }
    }
    ?>
    </body>
    </html>


    It works fine with the above code.

Posting Permissions

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



Click Here to Expand Forum to Full Width