Log in

View Full Version : Really simple php form insert into mysql db



Alk
01-15-2007, 04:49 PM
Hello,
I am trying to use this PHP form to mysql script that I have found on the internet.
Here is my form:

<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:

<?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!

Titan85
01-15-2007, 05:04 PM
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:

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 :)

thetestingsite
01-15-2007, 05:04 PM
You have to create the table first before you can insert data into it.

For example:



CREATE TABLE `$tablename` (
`id` TEXT NOT NULL ,
`name` TEXT NOT NULL
) ENGINE = MyISAM;


Hope this helps.

EDIT: Sorry Titan85, posted same time.

Titan85
01-15-2007, 05:07 PM
EDIT: Sorry Titan85, posted same time.lol, it happens ;)

Alk
01-15-2007, 05:10 PM
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.

If you notice, the part where the table prefix should be is blank: exampleuser_exampledb.shouldbeherebutisnot_languages

Titan85
01-15-2007, 05:12 PM
Saw your edit, I believe that the error is in this:
$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:
$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

thetestingsite
01-15-2007, 05:14 PM
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.

thetestingsite
01-15-2007, 05:18 PM
Another thing I noticed was that you are calling for the form field "tableprefix" in this line of 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:



$tableprefix = $_POST['name'];


because "name" is the name of the field in the form.

Hope this helps.

Alk
01-15-2007, 05:29 PM
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.

Saw your edit, I believe that the error is in this:
$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:
$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...

Another thing I noticed was that you are calling for the form field "tableprefix" in this line of 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:



$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!:p

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?

thetestingsite
01-15-2007, 05:30 PM
That would be for javascripts or css. Not really needed unless for one of those reasons.

Alk
01-15-2007, 05:49 PM
That would be for javascripts or css. Not really needed unless for one of those reasons.

Ah! I couldn't find out what it was used for, so thanks!

jitson
10-28-2007, 03:25 AM
Was having a similar problem. The thread here answered by questions and provided a quick and easy solution. I'm new to coding in php and this board has been a huge help.
Thanks again.:)