1

I have a batchcode.txt which contains list of batch code.

645863
302422
430307
821773
599738
671768
732159

and so on

I have a table in my database which is called batchcode with fields 'id','batchcode'.

My problem is it wont insert my text file into my table called batchcode. I already did the import and select table but nothing works...

I need some help can anyone help me please.

2 Answers 2

1

You have to split txt file by new line

$file = fopen("batchcode.txt","r");

$file = explode("\n", $file);

foreach ($file as $value){
   $sql = "INSERT INTO batchcode( batchcode ) VALUES ('$value')";
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try this :

<?
$host= "localhost"; //These 7 lines to connect to database
$user= "root";
$pass= "";
$db="database_name";

$connect= mysql_connect($host,$user,$pass);
if (!$connect)die ("Cannot connect!");
mysql_select_db($db, $connect);

$file = fopen("batchcode.txt","r");  // Open the txt file for reading

while(! feof($file))
{
$line = fgets($file);
$sql = "INSERT INTO batchcode( batchcode ) VALUES ('$line')"; //Insert every read line from txt to mysql database
mysql_query($sql);
}
fclose($file);
?>

Note: Specify your database name in $db="database_name"; create table named batchcode, create columns named id and batchcode (idcolumn should have AUTO_INCREMENT property).

1 Comment

Check you database name, table name and column names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.