SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Member
    Join Date
    May 2007
    Posts
    16
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Create a Table with name from a form?

    I have a form that a user enters a restaurant name into. The PHP code should create a MySQL table, using the posted value as the table name. I've spent hours looking over this, but I still keep getting an error, and the table does not get created. Thanks in advance.

    -- ERROR -----------------------------

    Warning: mysql_errno(): supplied argument is not a valid MySQL-Link resource on line 37

    Warning: mysql_error(): supplied argument is not a valid MySQL-Link resource on line 37
    : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''cars' (lineid INT NOT NULL AUTO_INCREMENT PRIMARY KEY, item TEXT, descrip TEXT,' at line 1


    -- FORM ------------------------------

    <form name="form1" method="post" action="<?php echo $editFormAction; ?>">
    <input type="text" name="restname" size="70"/>
    <input type="submit" name="Submit" value="Add Restaurant" />
    <input type="hidden" name="MM_insert" value="form1">
    </form>


    -- PHP --------------------------------

    <?php
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
    {
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $theValue;

    switch ($theType) {
    case "text":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "long":
    case "int":
    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
    break;
    case "double":
    $theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
    break;
    case "date":
    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
    break;
    case "defined":
    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
    break;
    }
    return $theValue;
    }

    $editFormAction = $_SERVER['PHP_SELF'];
    if (isset($_SERVER['QUERY_STRING'])) {
    $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
    }

    if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
    $insertSQL = sprintf("CREATE TABLE %s (lineid INT NOT NULL AUTO_INCREMENT PRIMARY KEY, item TEXT, descrip TEXT, price TEXT, empid INT(3))",
    GetSQLValueString($_POST['restname'], "text"));

    echo mysql_errno($insertSQL).":".mysql_error($insertSQL)."\n";

    mysql_select_db($database_menusdb, $menusdb);
    $Result = mysql_query($insertSQL, $menusdb) or die(mysql_error());

    }
    mysql_close($menusdb);
    ?>

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,017
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    i don't do php but the error message

    ... near ''cars' (lineid INT ...

    suggests that you are putting the table name inside single quotes, which is wrong
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    Worship the Krome kromey's Avatar
    Join Date
    Sep 2006
    Location
    Fairbanks, AK
    Posts
    1,621
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As far as your PHP errors go, you are trying to pass your SQL query to mysql_errno and mysql_error, which is wrong. You call them with no parameters to get information on the last error, just as you are doing on the line not generating a PHP error:
    PHP Code:
    $Result mysql_query($insertSQL$menusdb) or die(mysql_error()); 
    PHP questions? RTFM
    MySQL questions? RTFM

  4. #4
    Working on it... Contrid's Avatar
    Join Date
    Apr 2006
    Location
    Online
    Posts
    955
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    It looks like you are doing this :

    Code sql:
    CREATE TABLE 'cars'

    ...where you should be doing this :

    Code sql:
    CREATE TABLE `cars`

    Notice the single quotes in the first query.
    And so I got lost in code...completely asphyxiated by it...

    Premium WordPress plugins - Tribulant Software

  5. #5
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,017
    Mentioned
    53 Post(s)
    Tagged
    2 Thread(s)
    yeah, what i said

    the backticks i.e. `cars` are required only if the name is a reserved word or contains special characters, neither of which is the case here (nor should it ever be), so don't put them in
    r937.com | rudy.ca | Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  6. #6
    SitePoint Evangelist superuser2's Avatar
    Join Date
    Aug 2006
    Posts
    598
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Please get in the habit of using syntax hilighting on your code; it makes it much more readable especially to those of us who are used to it that way :-).

    Do you really want to create tables based on user input? You might try reading up on one-to-many and many-to-many relationships if that's what your after.

  7. #7
    SitePoint Wizard
    Join Date
    Dec 2004
    Location
    At My Desk!!
    Posts
    1,642
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It seems very odd to have a table for each user restaurant, why not create a table called Restaurants and just insert all the users restaurants in there, give them all a unique Id and you are sorted for anything you could possibly want to do.
    "Am I the only one doing ASP.NET in Delphi(Pascal)?"


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
  •