Create PHP code in “creatTable.php”

Write the code below:


PHP Code:
<?PHP
 $host
="localhost";
 
$user="root";
 
$pass="";
 
$database="db01sep2009mob_app";
 
$conn=mysql_connect ($host$user$pass);
 
mysql_select_db ($database);
 
$tableName=$_POST["name"];
$sql="CREATE TABLE $tableName
 (`countryCode`CHAR(2)NOT NULL,
 `countryName`VARCHAR(100)NOT NULL,
 PRIMARY KEY(`countryCode`)
 )"
;
$query=mysql_query$sql$conn )or die("Database Exist");
if(
$query==1){
echo 
"Creating table:<strong> $tableName,</strong> Successfully.<br>";
 
$open="countryCode.txt";
 
$countries =file("$open");
 for(
$i=0;$i<count($countries);$i){
 
$tag=explode(':',$ countries[$i]);
  
$i++;
  
$queryTable=mysql_query("INSERT $tableName SET  countryCode= '$tag[0]',countryName='$tag[1]'");
}
}else{
echo 
$query;
}
?>

The above PHP codes connect the database, and then create a table getting the name from the html page. After that it opens the countryCode.txt file, and converts all the data into an array, and separates each array into two parts where the “:” sign is and insert the entire data into the table by looping it till it counts the total number of arrays.


continue...


marlopax


Saturday, May 01, 2010