Edit: I changed the code, as I didn't realise that you also have to post variables from a form in a selfsubmitting form. I get the error: creating table "goods" failed You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'NOT NULL default '', price decimal(3,2) NOT NULL default.
Hi guys.
I'm brand new to PHP and this is my first real script. It's a selfsubmitting form, that is supposed to generate tables in a MySQL database, but it doesn't.
at first pageview, "submit" button is not set, display form. If it is set, try connecting with the db parameters collected from the form. If connection variables are set (= successfully establishes connection) then create the tables and check their existence. If the variables doesn't get set (= fails to establish connection) Die with error message.
<?php
function form(){
?>
<form id="dblink_params" name="dblink_params" method="post" action="<?php echo $PHP_SELF;?>">
<fieldset class="formfield">
<legend class="hlook1">1). YOUR DATABASE CONNECTION PARAMETERS:</legend>
<label>Database name:</label><input type="text" size="20" name="dbname" /><br />
<label>Database hostname:</label><input type="text" size="20" name="dbhost" /><br />
<label>Database username:</label><input type="text" size="20" name="dbusername" /><br />
<label>Database password:</label><input type="text" size="20" name="dbpassword" />
</fieldset>
<input type="submit" value="submit" name="submit">
</form>
<?php
}
// Variables picked up, and beginning whitespace trimmed away here,
$dbname = trim($_POST['dbname']);
$dbhost = trim($_POST['dbhost']);
$dbusername = trim($_POST['dbusername']);
$dbpassword = trim($_POST['dbpassword']);
// The standard messages are defined here
$failure = '<h3 class="warning">Setting of database parameters failed, and required tables were not created. Re-check your entered values.<h3>';
// at first pageview, "submit" button is not set, so display form and halt script with die. If it is set, contunue by trying to connect with the db parameters collected from the form. If connection variables are successfully set (= establishes connection) then create tablevariables and check created tables. If the connection variables do not get set (= fails to establish connection) Die with error message.
if (!isset($submit)) die (print form());
if ($dbconnector = mysql_connect($dbhost, $dbusername, $dbpassword) && $dbselector = mysql_select_db($dbname)){
$goods = "CREATE TABLE goods (
id INT(6) UNSIGNED NOT NULL auto_increment PRIMARY KEY,
cat VARCHAR(20) NOT NULL default '',
title VARCHAR(100) NOT NULL default '',
description VARCHAR(255) NOT NULL default '',
price decimal(3,2) NOT NULL default '0.00',
counter INT default '0'
) TYPE=innodb";
$result = mysql_query($goods) OR die ('creating table "goods" failed. '. mysql_error());
$temp_user = "CREATE TABLE temp_user (
id VARCHAR(10) NOT NULL PRIMARY KEY,
passwd VARCHAR(255) NOT NULL default '',
date TIMESTAMP NOT NULL
) TYPE=innodb";
$result = mysql_query($temp_user) OR die ('creating table "temp user" failed. '. mysql_error());
$downloads = "CREATE TABLE downloads (
id INT(6) UNSIGNED NOT NULL auto_increment PRIMARY KEY,
user VARCHAR(10) NOT NULL default '',
links VARCHAR(255) NOT NULL default '',
INDEX usr (user),
FOREIGN KEY (user) REFERENCES temp_user(id) ON DELETE CASCADE
) TYPE=innodb";
$result = mysql_query($downloads) OR die ('creating table "downloads" failed. '. mysql_error());
$parameter = "CREATE TABLE parameter (
id INT(6) UNSIGNED NOT NULL auto_increment PRIMARY KEY,
param VARCHAR(255) NOT NULL default ''
) TYPE=innodb";
$result = mysql_query($parameter) OR die ('creating table "parameter" failed. '. mysql_error());
die ('<h3 class="success">Connecting with your supplied parameters succeded. You can now start the process of configuring your shop and populating your catalogue. please remove this script (setup.php) from your webdirectory');
}
else die($failure);
//$insert = "INSERT INTO goods VALUES (1, 'apparel', 'megalomanic tour shirt', 'our world tour 07 shirt', '24.99')";
//$insert = "INSERT INTO goods VALUES (2, 'download', 'subside in flesh', 'our latest track from hell', '1.00')";
//$insert = "INSERT INTO goods VALUES (3, 'media','Who Is This God Person Anyway?', 'our tour DVD', '14.99')";
?>