Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Creating a table in mysql using a php form?

RottNKorpseRottNKorpse Posts: 2Member
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]
Name:

Filename:

Category:

Header Image:


[/code]

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]$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.";
?>[/code]

so any help on where I went wrong?

Comments

  • nugentnugent Posts: 87Member
    you have to connect to the database server before running any SQL commands, using the mysql_connect() function. then you execute the SQL command using either the mysql_db_query() or the mysql_query() functions, close link to mysql database by using the mysql_close() function.

    see the MySQL section of the PHP manual:
    http://www.php.net/manual/en/ref.mysql.php

    ------
    nugent



  • RottNKorpseRottNKorpse Posts: 2Member
    yes I know how to connect to a database and it is already in my code...I didnt include it because its not necessary when asking for help and most forums dont want you to include it because its just pointless code. Most want to see the actual query code.

    but anyway thanks for commenting.
  • ITAITA Posts: 463Member
    : so any help on where I went wrong?

    The first thing to do is find out what is wrong with the query. mysql_query() returns true or false on error, so you can do the following:

    [code]
    $result = mysql_query($query)

    if(!$result)
    {
    print 'DB error ' . mysql_error();
    }
    [/code]

    mysql_error() returns the error outputted by mysql. Tell us what this error is, and we can help further. I think I had a similar problem a while back, when I tried to do a similar thing. I think you might have to put each 'create table' in separate queries - I can't remember though.

    Hope this helps.

    Isaac


    "Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"

Sign In or Register to comment.