Hello,

I'm somewhat new to PHP. I'm having problems with a script and I don't know
why. It is from a book, yet it does not work for some reason. Both Apache
and MySQL are on and are working fine on my system. The code deals with
creating tables within a database (the database already exists. The error is
that it could connect to the database, but couldn't create the table within
the specified DB. Below are two PHP files that work together for this
specific project. Any help with this is much appreciated. Here are the
scripts:

Script #1


<?php

// Check that the user entered the info. If not then direct them back to the
form

if ((!$table_name) || (!$num_fields)) {
        header ("Location:
http://localhost/examples/dynamic/authentication/auth_app/show_createtable.h
tml");
        exit;
}

$form_block =  "<form method=\"post\" action=\"do_createtable.php\">
                <input type=\"hidden\" name=\"table_name\"
value=\"$table_name\">
                <table cellspacing=\"5\" cellpadding=\"5\">
                <tr>
                <th>FIELD NAME</th><th>FIELD TYPE</th><th>FIELD
LENGTH</th></tr>
                ";

for ($i = 0; $i < $num_fields; $i++) {

$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>
                ";
}

$form_block .= "<tr>
                <td align=\"center\" colspan=\"3\"><input type=\"submit\"
value=\"Create Table\"></td>
                </tr>
                </table>
                </form>
                ";
?>

<html>
<head>
<title>Create a Database Table: Step 2</title>
</head>
<body>

<h1>Define fields for <?php echo "$table_name"; ?></h1>
<?php echo "$form_block"; ?>

</body>
</html>





Script #2


<?php

$db_name="testDB";

$connection = @mysql_connect("localhost", "afghan", "office939") or
die ("Couldn't connect.");

$db = @mysql_select_db($db_name, $connection)
or die("Couldn't select database.");

$sql = "CREATE TABLE $table_name (";

for ($i = 0; $i < count($field_name); $i++) {
$sql .= "$field_name[$i] $field_type[$i]";
        if ($field_length[$i] != "") {
                $sql .= "(field_length[$i]),";
        } else {
                $sql .= ",";
        }
}

$sql = substr($sql, 0, -1);

$sql .= ")";

$result = @mysql_query($sql,$connection) or die("Couldn't execute query.");

if ($result) {
        $msg = "<p>$table_name has been created!</p>";
}

?>

<html>
<head>
<title>Create a Database Table: Step 3</title>
</head>
<body>

<h1>Adding table to <?php echo "$db_name"; ?>...</h1>

<?php echo "$msg"; ?>

</body>
</html>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to