Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Creating a table in mysql using a php form?

Status
Not open for further replies.

RottNKorpse

Technical User
Aug 8, 2006
13
Well I am trying to make an admin section to allow my other admins of my site able to add content to my mysql database without giving them phpmyadmin access and without teaching them out to use it as well. So I am trying to make a form that they can use to fill in a few simple fields and then it create the tables in the database and then insert a row in a seperate table that is already made.

Here is how I want it to work...
Name:
Filename:
Category:
Header Image:

"Name" would be used for the print output saying that "NameHere has been added to the database" and also be used in inserting a value in a table that has already been created called "pageheaders".

The Filename will be used many times...this will be the from portion of the names of the tables as well as part of the insert into pageheaders.

Category will just be part of the insert into pageheaders.
Header Image is also just part of the insert.

Here is the form portion of what I am trying to do...the action is calling to another page where the sql stuff is because that is the only way I knew to do it but if you know a better way to have them in the same file then I am more than happy to use that.

Form Code:
Code:
<form action="tablesandinsert.php" method="post">
Name: <input type="text" name="name"><br>
Filename: <input type="text" name="filename"><br>
Category: <input type="text" name="category"><br>
Header Image: <input type="text" name="headerimage"><br>
<input type="submit" value="Submit">
</form>

And here is the sql code that is used for inserting and creating sql stuff...it doesnt actually insert anything or create the tables so I obviously did something wrong.

SQL Stuff:
Code:
<?

$db_addr = 'localhost';
$db_user = 'myuser';
$db_pass = 'mypass';
$db_name = 'mydb';
$connect = @mysql_connect("$db_addr", "$db_user", "$db_pass");

if (!($connect)) // If no connect, error and exit().
{
echo("<p>Unable to connect to the database server.</p>");
exit();
}

if (!(@mysql_select_db($db_name))) // If can't connect to database, error and exit().
{
echo("<p>Unable to locate the $db_name database.</p>");
exit();
}

$name=$_POST['name'];
$filename=$_POST['filename'];
$category=$_POST['category'];
$headerimage=$_POST['headerimage'];

$query="CREATE TABLE `$filename_fins` (
  `gif_id` mediumint(9) NOT NULL auto_increment,
  `fingifurl` text NOT NULL,
  PRIMARY KEY  (`gif_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE `$filename_trons` (
  `gif_id` mediumint(9) NOT NULL auto_increment,
  `trongifurl` text NOT NULL,
  PRIMARY KEY  (`gif_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE `$filename_misc` (
  `gif_id` mediumint(9) NOT NULL auto_increment,
  `miscgifurl` text NOT NULL,
  PRIMARY KEY  (`gif_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `pageheaders` VALUES ('$name', '$filename', '$headerimage', '$category/$filename/index');";
$result=mysql_query($query);
Print "$name has been successfully added to the database.";
?>

so any help on where I went wrong?
 
You are missing the part where you actually execute the query
[blue]mysql_query()[/blue]?

Have you tried to echo the query? and running it from a GUI to see if it generates any errors?

Also i strongly suggest you don't supress the errors when you are developing. take off the "@" from the mysql function calls so if anything goes wrong you'll know about it.
Once you are done developing and you know its working you can put them back in. Or better yet catch them and do something with them.

Also when you execute your query add the following to it:

$res=mysql_query($qry) or [red]die (mysql_error(););[/red]

This way it displays an error if something went wrong and tells you what it thinks is wrong with it.

As for doing it in the same page, its all a question of testing wether the form has been submitted or not. If it has then you do all the query stuff. if it hasn't you just show the form.




----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Code:
$query="CREATE TABLE `$filename_fins` (
  `gif_id` mediumint(9) NOT NULL auto_increment,
  `fingifurl` text NOT NULL,
  PRIMARY KEY  (`gif_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE `$filename_trons` (
  `gif_id` mediumint(9) NOT NULL auto_increment,
  `trongifurl` text NOT NULL,
  PRIMARY KEY  (`gif_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

CREATE TABLE `$filename_misc` (
  `gif_id` mediumint(9) NOT NULL auto_increment,
  `miscgifurl` text NOT NULL,
  PRIMARY KEY  (`gif_id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;

INSERT INTO `pageheaders` VALUES ('$name', '$filename', '$headerimage', '$category/$filename/index');";

the above is actually several queries, which php cannot handle. you must iterate through these one query at a time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top