Creating a table in mysql using a php form?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RottNKorpse
    New Member
    • Aug 2006
    • 1

    Creating a table in mysql using a php form?

    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 "pageheader s".

    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:
    $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?
  • Niheel
    Recognized Expert Moderator Top Contributor
    • Jul 2005
    • 2432

    #2
    Your insert needs to be assiged to the variable $query

    ex:
    [PHP]
    $query = "INSERT INTO 'pageheaders' . . . ";
    $result = mysql_query($qu ery);
    if($query){
    print "your output hoes here";
    }
    [/PHP]
    niheel @ bytes

    Comment

    • hemashiki
      New Member
      • Aug 2006
      • 34

      #3
      First connect with the mysql database.
      Then select your Database.
      then select your table into which the datas should be stored.
      Everything in php code.

      get the variables using post method

      $f1=$_POST['fieldname1']
      $f2=$_POST['fieldname2']

      insert into `table_name`(`f ieldname1`,`fie ldname2`....)va lues('$f1','$f2 '.....);

      Comment

      Working...