Here is my html form
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Welcome, Database Entry Form Step 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1 style="color:#FF0000;font-size:16px">Step 1 Name and Number of Fields</h1>
<form method="post" action="do_showfielddef.php">
<label>Table Name</label><br />
<input type="text" name="table_name" size="30" />
<label>Number Of Fields</label><br />
<input type="text" name="num_fields" size="5" />
<br />
<input type="submit" name="submit" value="Go To Step 2" />
</form>
</body>
</html>
and my Field Definition File (php) defines the fields I set in the form:
<?php
//validate input
if ((!$_POST[table_name]) || (!$_POST[num_fields])) {
header( "Location: http://www.eriescene.com/createdbtable.htm");
exit;
}
// create form for display
$form_block = "
<form method=\"post\" action=\"do_createtable.php\">
<input type=\"hidden\" name=\"table_name\" value=\"$_POST[table_name]\" />
<table cellpadding=\"5\" cellspacing=\"5\">
<tr>
<th>Field Name</th><th>Field Type</th><th>Field Length</th></tr>";
// count from 0 until you reach number of field variable
for ($i = 0; $i < $_POST[num_fields]; $i++) {
// add to the form block one row for each field
$form_block .= "
<tr>
<td align=\"center\">
<input type=\"text\" name=\"field_name\" size=\"30\" />
</td>
<td align=\"center\">
<select name=\"field_type[]\">
<option value=\"char\">char<option>
<option value=\"date\">date<option>
<option value=\"float\">float<option>
<option value=\"int\">int<option>
<option value=\"text\">text<option>
<option value=\"varchar\">varchar<option>
</select>
</td>
<td align=\"center\">
<input type=\"text\" name=\"field_length\" size=\"5\" />
</td> </tr>";
} // finish up the form block
$form_block .= "
<tr>
<td align=\"center\">
<input type=\"submit\" value=\"Create Table\" />
</td>
</tr>
</table>
</form>";
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Create the DataBase: Step 2</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1 style="color:#FF0000;font-size:16px">Define Fields For Table: <? echo "$_POST[table_name]"; ?></h1>
<? echo "$form_block"; ?>
</body>
</html>
and finally the I construct the query posting my field result to this file: (php)
<?php
$db_name = "markbad_markbadsql";
$connection=mysql_connect ("localhost", "markbad_drpl1", "passwordremoved")
or die ('I cannot connect to the database because: ' . mysql_error());
$db = mysql_select_db ("$db_name, $connection")
or die ('I cannot connect to the database because: ' . mysql_error());
//start creating SQL statment
$sql = "CREATE TABLE $_POST[table_name] (";
//continue SQL statement for each field
for ($i = 0; $i < count ($_POST[field_name]); $i++) {
$sql .= $_POST[field_name][$i]." " .$_POST[field_type][$i];
if ($_POST[field_length][$i] != "") {
$sql .= " (".$_POST[field_length][$i]."),";
} else {
$sql .= ",";
}
}
// Clean up the end of the string.
$sql = substr ($sql, 0, 1);
$sql .= ")";
// Execute Query
$result = mysql_query($sql,$connection)
or die (mysql_error());
// Get a good result if sucessful
if ($result) {
$msg = "<p>".$_POST[table_name]." has been created!</p>";
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
</head>
<body>
<h1>adding to table <? echo "$db_name"; ?>...</h1>
<? echo "$msg"; ?>
</body>
</html>
I get a funny error:
"I cannot connect to the database because: Access denied for user 'markbad_drpl1'@'localhost' to database 'markbad_markbadsql, Resource id #2' "
Sounds like a password problem but I am using the right password and the username is the one able to use the DB
I don't know to much about mysql I only know enough to do this in PHP reading the book and I am on chapter 12. I know I guess enough but I really need help with this I have been debugging this for too long.. Anyone want to take a crack at it??
by the way
MySql Version - 4.1.13-standard
PHP Version - 4.3.11
Apache Version - 1.3.34 (Unix)