creating table with variables

Discussion in 'PHP' started by ingilizdili, Nov 16, 2010.

  1. #1
    Hello;
    I prepared a html form whereby I am trying to create a mysql table.
    The php page, which gets the variables from the html form, has the following coding

    
    //mysql connection and the related coding here
    
    //create table as posted via variable $table_name
    mysql_query("CREATE TABLE $table_name(
    id INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(id),
    $areaA $typeA($charA), //all variable coming from the form
    $areaB $typeB($charB)")
    
    or die(mysql_error());
    
    echo "table created";
    
    PHP:
    But it doesn't work. Can you tell me what the problem is?
    Thanks in advance.
     
    ingilizdili, Nov 16, 2010 IP
  2. imocoder

    imocoder Member

    Messages:
    45
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    43
    #2
    As I don't see the complete code ... the question is: is there any error?
    Is the PHP error reporting turned on? If not turn on and allow to display even Notice errors too.
     
    imocoder, Nov 16, 2010 IP
  3. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #3
    What you have written seems fine, but the code just sits there ?, to run the code it would be something like this:

    
    
    $sql =  "mysql_query("CREATE TABLE $table_name(  //$sql =  "  open the query statement
    id INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(id),
    $areaA $typeA($charA),
    $areaB $typeB($charB)")  "; // we close the query here
    
    mysql_query($sql, $your_connection_code); // run the query
    
    echo "table created";
    
    PHP:
    Might need tweaking...
     
    Last edited: Nov 16, 2010
    MyVodaFone, Nov 16, 2010 IP
  4. xpertdev

    xpertdev Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    First check for all your variables coming from form. are all variable are correct as you want?
     
    xpertdev, Nov 16, 2010 IP
  5. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Well, here is the whole code. It echoes "table created" when worked. However, no table is created.

    <?
    include('baglan.php');   //make a mysql connection
    
    $tablo_adi = $_POST['tablo_adi'];
    $alanA = $_POST['alanA'];
    $turuA = $_POST['turuA'];
    $karakterA = $_POST['karakterA'];
    $alanB = $_POST['alanB'];
    $turuB = $_POST['turuB'];
    $karakterB = $_POST['karakterB'];
    $alanC = $_POST['alanC'];
    $turuC = $_POST['turuC'];
    $karakterC = $_POST['karakterC'];
    
    
    $tablo_adi = stripslashes($tablo_adi);
    $tablo_adi = mysql_real_escape_string($tablo_adi);
    $alanA = stripslashes($alanA);
    $alanA = mysql_real_escape_string($alanA);
    $turuA = stripslashes($turuA);
    $turuA = mysql_real_escape_string($turuA);
    $karakterA = stripslashes($karakterA);
    $karakterA = mysql_real_escape_string($karakterA);
    $alanB = stripslashes($alanB);
    $alanB = mysql_real_escape_string($alanB);
    $turuB = stripslashes($turuB);
    $turuB = mysql_real_escape_string($turuB);
    $karakterB = stripslashes($karakterB);
    $karakterB = mysql_real_escape_string($karakterB);
    $alanC = stripslashes($alanC);
    $alanC = mysql_real_escape_string($alanC);
    $turuC = stripslashes($turuC);
    $turuC = mysql_real_escape_string($turuC);
    $karakterC = stripslashes($karakterC);
    $karakterC = mysql_real_escape_string($karakterC);
    
    
    $con = mysql_connect("localhost","username","password");
    $db_selected = mysql_select_db('database', $con);
    
    $sql = "mysql_query(CREATE TABLE $tablo_adi(
    id INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(id),
    $alanA $turuA($karakterA),
    $alanB $turuB($karakterB)) ";
    $result = "mysql_query($sql, $db_selected)";
    
    if ($result) {
    
    echo "table created";
    
    }
    
    ?>
    PHP:
     
    ingilizdili, Nov 16, 2010 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    
    $result = "mysql_query($sql, $db_selected)";
    
    if ($result) {
    
    PHP:
    That doesn't really mean anything it will always be true and it doesn't indicate to php to run your command, try running just

    mysql_query($sql, $db_selected);
    PHP:
    ...to see if it creates a table, then you can make some other if statement.
     
    MyVodaFone, Nov 16, 2010 IP
  7. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Well, adopting from a working create table example, I simplified the code back to its original with a tiny change and it worked. Can someone please explain why it works when like this:
    mysql_query("CREATE TABLE $tablo_adi(
    id INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(id),
     $alanA $turuA($karakterA),
     $alanB $turuB($karakterB),
     age INT)")                           //This line is completely irrelevant. I put it there just to see if it would work. Interestingly, it did!
     or die(mysql_error());  
    
    PHP:
    and doesn't work when like this:
    mysql_query("CREATE TABLE $tablo_adi(
    id INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(id),
     $alanA $turuA($karakterA),
     $alanB $turuB($karakterB)")
     or die(mysql_error());  
    
    PHP:
     
    ingilizdili, Nov 16, 2010 IP
  8. ingilizdili

    ingilizdili Peon

    Messages:
    61
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Finally I've got it! One bracket was missing. So, here is the code in its ultimate form:
    mysql_query("CREATE TABLE $tablo_adi(
    id INT NOT NULL AUTO_INCREMENT, 
    PRIMARY KEY(id),
     $alanA $turuA($karakterA),
     $alanB $turuB($karakterB))") //one brackey ")" was missing here, right after karakterB
     or die(mysql_error());  
    
    PHP:
     
    ingilizdili, Nov 16, 2010 IP
  9. Ankurkr

    Ankurkr Peon

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    nice comments. i am also looking for this..........
     
    Ankurkr, Nov 17, 2010 IP