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
https://forums.phpfreaks.com/topic/137200-html-form-button-creat-table/
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();

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


<

 

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


<

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.