Page 1 of 2 12 LastLast
Results 1 to 10 of 12

Thread: Really simple php form insert into mysql db

  1. #1
    Join Date
    Jan 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Really simple php form insert into mysql db

    Hello,
    I am trying to use this PHP form to mysql script that I have found on the internet.
    Here is my form:
    Code:
    <html>
    <head>
    <title>English Language</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>
     
    <body>
    <form name="form1" method="post" action="insert.php">
    <p>Forum URL: http://<input name="name" type="text" id="tableprefix">.example.com
    <input type="submit" name="Submit">
    </p>
    </form>
    </body>
    </html>
    Basically, I want to have the form users input insert data into a table with a variable table prefix. Here is what I basically want to have in my insert.php:
    Code:
    <?php
    
    // Include the Mysql Config file (config.php)
    include 'config.php';
    
    // Connect to Mysql
    $conn = mysql_connect($dbhost, $dbuser, $dbpass) or die ('Error connecting to mysql');
    mysql_select_db($dbname);
    
    $tableprefix = $_POST['tableprefix'];
    //$table = $tableprefix."_languages";
    
    // Insert into Mysql Database:
    
    $query = "INSERT INTO `".$tableprefix."_languages` (lid, ldir, lname, lauthor, lemail) VALUES ('3', 'en', 'English', 'Invision Power Board', 'languages@invisionboard.com')";
    mysql_query($query) or die("Query failed: " . mysql_error());
    
    //mysql_query("INSERT INTO $table (lid, ldir, lname, lauthor, lemail) VALUES ('3', 'en', 'English', 'Invision Power Board', 'languages@invisionboard.com')") or die ("Query failed: " . mysql_error());
    
    // Show alert when data inserted to Mysql
    echo "<script language=javascript>alert('Data inserted to Mysql Database!'); window.location = 'index.php'; </script>";
    
    // Close connection to Mysql
    mysql_close($conn);
    ?>
    It just does not work. The error I get is:
    Query failed: Table 'exampleuser_exampledb._languages' doesn't exist
    I do not know how to get the variable to work in there. Any help to get it to work would be appreciated. Thanks!

  2. #2
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    You are getting the error because the sql table "exampleuser_exampledb._languages" is not on in the database you are connected to. From what I see, you are connecting to the sql database correctly, but the table does not exist in that database. You need to insert the table into the database via phpmyadmin or whatever program you use to do it. The table code should look something like this:
    PHP Code:
    CREATE TABLE `name_of_table` (
        `
    your values`...
    ); 
    MySQL works by connecting to a database and then searching within the database tables. You need to have a table in the database with all the values you want to insert so that it knows where to put the data inserted. Hope this helps out

  3. #3
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    You have to create the table first before you can insert data into it.

    For example:

    Code:
    CREATE TABLE `$tablename` (
    `id` TEXT NOT NULL ,
    `name` TEXT NOT NULL
    ) ENGINE = MyISAM;
    Hope this helps.

    EDIT: Sorry Titan85, posted same time.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  4. #4
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by thetestingsite View Post
    EDIT: Sorry Titan85, posted same time.
    lol, it happens

  5. #5
    Join Date
    Jan 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    The table is already created, and I am entering the table prefix in the form for a table already created. It isn't that which is the problem, it is simply not entering the data from the form into that part of the query I think.

    Edit: If you notice, the part where the table prefix should be is blank: exampleuser_exampledb.shouldbeherebutisnot_languages
    Last edited by Alk; 01-15-2007 at 05:12 PM. Reason: Added some more

  6. #6
    Join Date
    Aug 2006
    Location
    Ohio
    Posts
    266
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Saw your edit, I believe that the error is in this:
    PHP Code:
    $query "INSERT INTO `".$tableprefix."_languages` (lid, ldir, lname, lauthor, lemail) VALUES ('3', 'en', 'English', 'Invision Power Board', 'languages@invisionboard.com')";
    mysql_query($query) or die("Query failed: " mysql_error()); 
    Make it this:
    PHP Code:
    $query "INSERT INTO `$tableprefix_languages` (lid, ldir, lname, lauthor, lemail) VALUES ('3', 'en', 'English', 'Invision Power Board', 'languages@invisionboard.com')";
    mysql_query($query) or die("Query failed: " mysql_error()); 
    EDIT: Go with what thetestingsite said
    Last edited by Titan85; 01-15-2007 at 05:17 PM. Reason: thetestingsite is smarter

  7. #7
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Instead of making the variable with the "_languages" part in the query, make a variable $table with that info in it, then call $table in the query. I see you already had that in your php code, but it is commented out.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  8. #8
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    Another thing I noticed was that you are calling for the form field "tableprefix" in this line of code:

    Code:
    $tableprefix = $_POST['tableprefix'];
    but there is none by that name in your form. Try either changing the html form fireld to have this name, or change the above to the following:

    Code:
    $tableprefix = $_POST['name'];
    because "name" is the name of the field in the form.

    Hope this helps.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

  9. #9
    Join Date
    Jan 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by thetestingsite View Post
    Instead of making the variable with the "_languages" part in the query, make a variable $table with that info in it, then call $table in the query. I see you already had that in your php code, but it is commented out.
    Yes, that was because I was testing it like you said as well, but it didn't work either.
    Quote Originally Posted by Titan85 View Post
    Saw your edit, I believe that the error is in this:
    PHP Code:
    $query "INSERT INTO `".$tableprefix."_languages` (lid, ldir, lname, lauthor, lemail) VALUES ('3', 'en', 'English', 'Invision Power Board', 'languages@invisionboard.com')";
    mysql_query($query) or die("Query failed: " mysql_error()); 
    Make it this:
    PHP Code:
    $query "INSERT INTO `$tableprefix_languages` (lid, ldir, lname, lauthor, lemail) VALUES ('3', 'en', 'English', 'Invision Power Board', 'languages@invisionboard.com')";
    mysql_query($query) or die("Query failed: " mysql_error()); 
    EDIT: Go with what thetestingsite said
    Yes, I tried that too...
    Quote Originally Posted by thetestingsite View Post
    Another thing I noticed was that you are calling for the form field "tableprefix" in this line of code:

    Code:
    $tableprefix = $_POST['tableprefix'];
    but there is none by that name in your form. Try either changing the html form fireld to have this name, or change the above to the following:

    Code:
    $tableprefix = $_POST['name'];
    because "name" is the name of the field in the form.

    Hope this helps.
    Aha, that's what it was! Thank you so much for your help, that has helped make it work. I just changed the <input name="name" to tableprefix and that made it work. Silly me, such a silly mistake. Once I tried all the other solutions suggested I thought it should have worked by now!

    Thank you all! I will also use the thank you button for both of you.

    BTW, what is the id part of the tag used for in <input name="tableprefix" type="text" id="tableprefix"> then?
    Last edited by Alk; 01-15-2007 at 05:30 PM. Reason: left out 'the'

  10. #10
    Join Date
    Sep 2006
    Location
    St. George, UT
    Posts
    2,769
    Thanks
    3
    Thanked 157 Times in 155 Posts

    Default

    That would be for javascripts or css. Not really needed unless for one of those reasons.
    "Computer games don't affect kids; I mean if Pac-Man affected us as kids, we'd all be running around in darkened rooms, munching magic pills and listening to repetitive electronic music." - Kristian Wilson, Nintendo, Inc, 1989
    TheUnlimitedHost | The Testing Site | Southern Utah Web Hosting and Design

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
  •