0

I want to pull data from a txt file called domains.txt and insert the contents of the file into a database. Below is the code i wrote but is not working.Please help me

<?php

$conn = mysql_connect("localhost","root","");
if (!$conn) {
    die("Could not connect: " . mysql_error());
}
mysql_select_db("modify_domains");
$file = fopen("domains.txt", "r");

    // Read line by line until end of file
    while (!feof($file)) { 

    // Make an array using comma as delimiter
       $array = explode(",",fgets($file)); 
        $domain_name=$array[0];
        $reg_email=$array[1];
        $tech_email=$array[2];
        $billing_email=$array[3];
        $admin_email=$array[4];
        $password=$array[5];
    $sql = "INSERT INTO tbl_domains (domain_name, reg_email, tech_email,billing_email,admin_email,password) VALUES('".$adomain_name"','".$reg_email."',".$tech_email.",".$billing_email.",".$admin_email.",".$password.")";   
    mysql_query($sql,$conn)  or die("Could not connect: " . mysql_error());
    // use mysql insert query here
}

?>
db100
  • 55
  • 10
  • so what _exactly_ is not working? – Paul Coldrey Sep 17 '15 at 07:51
  • Not working means??? Have you get any error??? – Saty Sep 17 '15 at 07:52
  • in the SQL string I can see you are missing a "." after $adomain_name, plus I think you mean $domain_name not $adomain_name. I think your issue is really just typos. The error messages it shows should be all you need – Paul Coldrey Sep 17 '15 at 07:52
  • its giving me a mysql error: Could not connect: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '@gameboy.com.mw)' at line 1 ..but i cant figure out the error am making – db100 Sep 17 '15 at 07:58

1 Answers1

0

All text fields must be separated by single quotes (like '".$reg_email."') and don't forget the point to separate text field and vars.

The query should be like that:

$sql = "INSERT INTO tbl_domains 
(domain_name, reg_email, tech_email,billing_email,admin_email,password) VALUES
('".$adomain_name."','".$reg_email."','".$tech_email."','".$billing_email."','".$admin_email."','".$password."')"; 

All text fields must be separated with single quotes. Also you could write it like this:

 $sql = "INSERT INTO tbl_domains 
    (domain_name, reg_email, tech_email,billing_email,admin_email,password) VALUES
    ('$adomain_name','$reg_email','$tech_email','$billing_email','$admin_email','$password')"; 
Javier Núñez
  • 612
  • 1
  • 5
  • 17
  • thanks man...its now inserting data into the database though its giving me this notice : Undefined offset: 1 in C:\wamp\www\fred-test\fred-sit\insert-test.php on line 16 – db100 Sep 17 '15 at 08:06
  • This must be related with empty lines. You are fetching a line without fields separated by commas. Maybe the last one? – Javier Núñez Sep 17 '15 at 08:07
  • Hello Javier, what if i want the passwords for all the domains to be generated randomly and inserted in the password field in the database instead of getting an already set password value in the text file. How can i do it? – db100 Sep 17 '15 at 09:39
  • http://stackoverflow.com/questions/6101956/generating-a-random-password-in-php Here you have a function to get a new generated password, so you just get it from the function and throw to the database. Then you just have to do: $password=randomPassword(); – Javier Núñez Sep 17 '15 at 09:41