Jump to content

HTML Form button creat table


razta

Recommended Posts

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!

Link to comment
Share on other sites

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();

Link to comment
Share on other sites

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


<

 

Link to comment
Share on other sites

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


<

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.