razta Posted December 16, 2008 Share Posted December 16, 2008 Hello, Im wanting to creat a HTML form that has a button "creat table" once this button has been pressed it executes the MySQL query. Ive been trying for days and my skills in PHP+MySQL are minimal. Hopefully some one can take a look at my code and point out some thing blatently obvious. Here's the HTML: <form action="SQL.php" method="post"> <input type="button" value="Create table" name="create_db"><br> </form> Here's the PHP minus the database connect stuff... <?php // Create table users $_POST[create_db]=$create; $create="CREATE TABLE users (user_id int(6),first_name varchar(15),last_name varchar(15),PRIMARY KEY (user_id),UNIQUE id (user_id),KEY id_2 (user_id))"; // Insert some data into users $insert = "INSERT INTO users VALUES ('1','John','Smith');INSERT INTO users VALUES ('2','Gordon','Brown');INSERT INTO users VALUES ('3','Hack','Me');INSERT INTO users VALUES ('4','Pablo','Picasso')"; $total=$create.$insert; mysql_query($total); mysql_close(); ?> Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/137200-html-form-button-creat-table/ Share on other sites More sharing options...
Maq Posted December 16, 2008 Share Posted December 16, 2008 Do you get any errors? Does anything happen? Do you want to submit to the same page, or a different page (SQL.php)? Your PHP code should look like this: // Create table users $_POST[create_db]=$create; $create="CREATE TABLE users (user_id int(6),first_name varchar(15),last_name varchar(15),PRIMARY KEY (user_id),UNIQUE id (user_id),KEY id_2 (user_id))"; // Insert some data into users $insert = "INSERT INTO users VALUES ('1','John','Smith');INSERT INTO users VALUES ('2','Gordon','Brown');INSERT INTO users VALUES ('3','Hack','Me');INSERT INTO users VALUES ('4','Pablo','Picasso')"; mysql_query($total) or die(mysql_error()); mysql_query($insert) or die(mysql_error()); mysql_close(); Quote Link to comment https://forums.phpfreaks.com/topic/137200-html-form-button-creat-table/#findComment-716715 Share on other sites More sharing options...
razta Posted December 16, 2008 Author Share Posted December 16, 2008 Thanks for the reply! I tried your code and I get no errors however the data is not insterted into the database. The HTML and PHP are on the same page SQL.php. Quote Link to comment https://forums.phpfreaks.com/topic/137200-html-form-button-creat-table/#findComment-716721 Share on other sites More sharing options...
Maq Posted December 16, 2008 Share Posted December 16, 2008 Easy enough. You should submit the page to itself in the action. If the button isset (clicked) then it executes the PHP code. Try this code: if(isset($_POST['create_db']) { // Create table users $_POST[create_db]=$create; $create="CREATE TABLE users (user_id int(6),first_name varchar(15),last_name varchar(15),PRIMARY KEY (user_id),UNIQUE id (user_id),KEY id_2 (user_id))"; // Insert some data into users $insert = "INSERT INTO users VALUES ('1','John','Smith');INSERT INTO users VALUES ('2','Gordon','Brown');INSERT INTO users VALUES ('3','Hack','Me');INSERT INTO users VALUES ('4','Pablo','Picasso')"; mysql_query($total) or die(mysql_error()); mysql_query($insert) or die(mysql_error()); mysql_close(); } ?> </pre> <form action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="post"> < Quote Link to comment https://forums.phpfreaks.com/topic/137200-html-form-button-creat-table/#findComment-716729 Share on other sites More sharing options...
razta Posted December 16, 2008 Author Share Posted December 16, 2008 I now get this error: Parse error: syntax error, unexpected '{' in /home/user/SQL.php on line 106 Thanks for your help, believe it or not ive been working on this and other code for days. Quote Link to comment https://forums.phpfreaks.com/topic/137200-html-form-button-creat-table/#findComment-716734 Share on other sites More sharing options...
Maq Posted December 16, 2008 Share Posted December 16, 2008 I don't have an environment to test in right now... This should work, although the error is pretty obvious. if(isset($_POST['create_db'])) { $create="CREATE TABLE users (user_id int(6),first_name varchar(15),last_name varchar(15),PRIMARY KEY (user_id),UNIQUE id (user_id),KEY id_2 (user_id))"; // Insert some data into users $insert = "INSERT INTO users VALUES ('1','John','Smith');INSERT INTO users VALUES ('2','Gordon','Brown');INSERT INTO users VALUES ('3','Hack','Me');INSERT INTO users VALUES ('4','Pablo','Picasso')"; mysql_query($total) or die(mysql_error()); mysql_query($insert) or die(mysql_error()); mysql_close(); } ?> </pre> <form action="<?php%20echo%20%24_SERVER%5B'PHP_SELF'%5D;%20?>" method="post"> < Quote Link to comment https://forums.phpfreaks.com/topic/137200-html-form-button-creat-table/#findComment-716751 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.