2015-09-17 142 views
0

我想从名为domains.txt的txt文件中提取数据并将文件的内容插入到数据库中。下面是我写的代码,但不是working.Please帮我从txt文件中提取数据并插入数据库

<?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 
} 

?> 
+0

那么_exactly_不工作? –

+0

不工作意味着?你有任何错误? – Saty

+0

在SQL字符串中我可以看到你缺少一个“。”在$ adomain_name之后,再加上我认为你的意思是$ domain_name不是$ adomain_name。我认为你的问题实际上只是错别字。它显示的错误消息应该是你需要的全部 –

回答

0

所有文本字段必须用单引号隔开(如“‘$ reg_email。’”),不要忘了点分离文本领域和变量。

查询应该是这样的:

$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."')"; 

所有文本字段必须用单引号分隔。你也可以这样写:

$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')"; 
+0

谢谢你......现在插入数据通过它给我这个通知:未定义的偏移量:1在C:\ wamp \ www \ fred-test \ fred-sit \ insert-test.php在线16 – db100

+0

这必须与空行相关。您正在获取没有用逗号分隔的字段的行。也许最后一个? –

+0

Hello Javier,如果我想要随机生成所有域的密码,并将其插入数据库的密码字段中,而不是在文本文件中获取已设置的密码值,该怎么办?我该怎么做? – db100

相关问题