Steps to Insert txt data into database:
If you look at the countryCode.txt file, you can realize that to put this type of huge data into a database table is quite a tough time taking job if you do it manually. However, in this discussion I am going through steps where the PHP code will do this job in one click.
Step 1:
Place the text file into the www folder. Make sure that each line data holds the code then the sign and in last the name of the countries (example: countryCode:countryName). In PHP, this entire data will place in an array and each line will count one array number. The “:” sign is the separator of code and the name of the entire data.
Step 2:
To do the backend operation happen, we have to make a front-end html page from where we can put a database table name and call the php to run. I am going to give you a simple example to do this with a minimum need of an input text field and a create button in the html page.
HTML Code:
<html>
<head>
<title>:: EMI Application :: Create Table</title>
</head>
<body style="background:#E4E4E4; margin:auto;">
<script Language="JavaScript">
<!--
function Validator(){
if (createTable.name.value == ""){
alert("Enter Table Name.");
createTable.name.focus();
return (false);
}
return (true);
}
-->
</script>
<div style="background:#990000; padding:5px 0px 10px 10px;">
<form style="font-family:Arial, Helvetica, sans-serif; color:#999999;" name="createTable" method="POST" action="countryCode.php" onsubmit="return Validator()">Database Table Name:<br><input type="text"size="29"style="font-family:Arial, Helvetica, sans-serif;background:#FFFFCC;color:#996666;" maxLength="100" name="name">
<input type="submit"style="font-family:Arial, Helvetica, sans-serif;font-size:12;" value="Create">
</form>
</div>
<div style="padding:2px 10px 10px 10px;font-family:Arial, Helvetica, sans-serif;color:#999999;font-size:11;"><strong>"Collect All" to Make a Professional Mobile Application in Flash Lite 1.1 Enabled Devices. EMI Application.</strong> © - 2010-2011 <strong> MarlöPax.</strong> All rights reserved.</div>
</body>
</html>
The above HTML code will display an input text field and a create button with a JavaScript text field validation and some CSS style decoration.
Step 3:
Make a new PHP file “creatTable.php” for this operation.
continue... Step 3..........
marlopax
Subha Nabo Barsha (Bengali New Year 1417)
Thursday, April 15, 2010
1933 1st May, Happy Birthday Baba (DAD), Dedicate this post to his 76th birthday
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