Jump to content

Creating Table script


Moksha

Recommended Posts

Hi guys, im fairly new to php so go easy on me. I just bought teh book PHP 5 in easy steps, and im trying to run a script in it that creates a new database, and allows you then you create a table in that databse.

 

The script from the book looks like this:

 

<!-- example for PHP 5.0.0 final release -->

<html><head><title>Creating a table</title></head>
<body>
<?php

$self = $_SERVER['PHP_SELF'];
$fields = $_POST['fields'];
$db =     $_POST['db'];
$name =   $_POST['name'];
$table =  $_POST['table'];
$type =   $_POST['type'];
$size =   $_POST['size'];


if( !$fields and !$db )
{
  $form ="<form action=\"$self\" method=\"post\">";
  $form.="How many fields are needed in the new table?<br>";
  $form.="<input type=\"text\" name=\"fields\" size=\"5\">";
  $form.="<input type=\"submit\" value=\"Submit\">";
  echo($form);
}
else if( !$db )
{ 
  $form ="<form action=\"$self\" method=\"post\">";
  $form.="Database:     <input type=\"text\" name=\"db\"><br>";
  $form.="Table Name:  <input type=\"text\" name=\"table\" size=\"10\"><br> ";
  for ($i = 0 ; $i <$fields; $i++) 
  {
    $form.="Column Name:<input type=\"text\" name=\"name[$i]\" size=\"10\"> ";
    $form.="Type: <select name=\"type[$i]\">";
    $form.="<option value=\"char\">char</option>";	
    $form.="<option value=\"varchar\">varchar</option>";
    $form.="<option value=\"int\">int</option>";
    $form.="<option value=\"float\">float</option>";
    $form.="<option value=\"timestamp\">timestamp</option>";
    $form.="</select> ";
    $form.="Size:<input type=\"text\" name=\"size[$i]\" size=\"5\"><br>";
  }
  $form.=" <input type=\"submit\" value=\"Submit\"></form>";
  echo($form);
}
else
{
  $conn = @mysql_connect("localhost", "root", "alpha")
or die("Could not connect.");

  $rs = @mysql_select_db($db, $conn)
or die("Could not select database.");

  $num_columns = count($name);

  $sql = "create table $table (";
  for ($i = 0; $i < $num_columns; $i++) 
  {
    $sql .= "$name[$i] $type[$i]";
    if(($type[$i] =="char") or ($type[$i] =="varchar"))
    {
      if($size[$i] !="" ){ $sql.= "($size[$i])"; }
    }
    if(($i+1) != $num_columns){ $sql.=","; }
  }
  $sql .= ")";

  echo("SQL COMMAND: $sql <hr>");

  $result = mysql_query($sql,$conn)
or die("Could not execute SQL query");

  if ($result) {     
echo("RESULT: table \"$table\" has been created");
  }
}

?>
</body></html>

 

 

 

 

 

------------------

i put this file called create_table.php in the directory of my server and try access it by typing localhost/create_table.php

 

Immediately i get the following errors:

 

Notice: Undefined index: fields in C:\HTTPServerDocs\create_table.php on line 8

 

Notice: Undefined index: db in C:\HTTPServerDocs\create_table.php on line 9

 

Notice: Undefined index: name in C:\HTTPServerDocs\create_table.php on line 10

 

Notice: Undefined index: table in C:\HTTPServerDocs\create_table.php on line 11

 

Notice: Undefined index: type in C:\HTTPServerDocs\create_table.php on line 12

 

Notice: Undefined index: size in C:\HTTPServerDocs\create_table.php on line 13

 

 

but it still allows me to input how many fields in the table.

After i do this i follow the remaining instructiosn and create the database...then i receive the following error:

 

Notice: Undefined index: fields in C:\HTTPServerDocs\create_table.php on line 8

Could not select database

 

Is there soemthing wrong with teh php code, or something worng with my MySQL. Im pretty sure my MySQL is function fine as i can perform operations with other databases.

Link to comment
Share on other sites

yeah i get what you mean by warnings....

but still at the end it says "could not select database". So maybe the database isnt getting created or soemthing.

Yeah, that bit is an error. The code you have posted up (could you please use the [code[/code] tags! I have added them for you.) doesn't create a database, does the database that you are entering actually exist?

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.