我目前正在尝试将CSV文件中的数据插入到我的mysql数据库中。表格和所有功能都正常工作,因为我已经过测试以确保它正在读取文件等。当我准备将PDO插入到mysql中时,我在Chrome中没有错误。现在,当我在Chrome(任何浏览器)中出现服务器错误时,我发现它execute()
,我查看了我的错误日志文件,其中显示了函数所在的位置:
SQLSTATE[HY000] [2002]
No connection could be made because the target machine actively refused it.
我会将代码粘贴到我所做的功能上 代码:
function returnBack(){
header("Location:csvuploader/csvuploaderform.php?seterror=1");
exit;
}
if (isset($_POST['submit'])){
/*******************************CHECK TO MAKE SURE FORM IS PROPERLY FILLED AND GET NAME OF COLUMNS IN CSV FILE *********************************************************/
if (empty($_FILES['file'] ) )
{
returnBack();
}
if (empty($_POST['firstname'])){
echo "firstname is empty";
returnBack();
}
if (empty($_POST['lastname'])){
echo "lastname is empty";
returnBack();
}if (empty($_POST['email'])){
echo "email is empty";
returnBack();
}
if (empty($_POST['phone'])){
returnBack();
}
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file , "r");
$fileop = fgetcsv($handle,1000,",");
$fileop=array_map("strtoupper",array_map("trim",$fileop));
$firstname_index = array_search(strtoupper($_POST["firstname"]),$fileop);
if ($firstname_index===false){
returnBack();
}
$lastname_index = array_search(strtoupper($_POST['lastname']),$fileop);
if ($lastname_index===false){
returnBack();
}
$email_index = array_search(strtoupper($_POST['email']),$fileop);
if ($email_index===false){
returnBack();
}
$phone_index = array_search(strtoupper($_POST['phone']),$fileop);
if ($phone_index===false){
returnBack();
}
/***********************ASSIGN COLUMN VALUES TO ACCORDING VARIABLES AND INSERT THEM INTO CSV TABLE IN DB *************************************/
try {
# MySQL with PDO_MYSQL
$DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$DBH->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );
}
catch(PDOException $e) {
echo "I'm sorry, I'm afraid I can't do that.";
file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
}
/*$fileop = fgetcsv($handle,1000,",")*/
while (($fileop=fgetcsv($handle)) !== false)
{
$fileop=array_map("trim",$fileop);
$firstname = $fileop[$firstname_index];
$lastname = $fileop[$lastname_index];
$email = $fileop[$email_index];
$phone = $fileop[$phone_index];
$insertdata = $DBH->prepare("INSERT INTO csvdata (firstname, lastname, email, phone) VALUES ('$firstname','$lastname','$email','$phone')");
//var_dump($insertdata);
$insertdata->execute();
}
}
$DBH = null;
UPDATED ------------------------------------- Heres some info
PHP信息 - PHP Version 5.3.10, System Windows NT W03 6.0 build 6002 (Windows Server 2008 Standard Edition Service Pack 2) i586
更新------------------------------------
我能够连接到MySQL服务器并浏览它 - 插入我所做的其他功能的数据,它们都完美无缺。只是这个人会给我一个错误。
更新---------------------------------------------- ---------------------------
想出你们所有人的帮助。我实际上需要填写所有数据列才能使用。例如我有(名字,姓氏,电子邮件,电话)我需要填写用户名,密码,状态部分。谢谢你们的帮助!希望这能帮助任何陷入同一个洞的人!
答案 0 :(得分:1)
想出你们所有人的帮助。我实际上需要填写所有数据列才能使用。例如我有(名字,姓氏,电子邮件,电话)我需要填写用户名,密码,状态部分。谢谢你们的帮助!希望这能帮助任何陷入同一个洞的人!
while (($fileop=fgetcsv($handle)) !== false)
{
$fileop=array_map("trim",$fileop);
$firstname = $fileop[$firstname_index];
$lastname = $fileop[$lastname_index];
$email = $fileop[$email_index];
$phone = $fileop[$phone_index];
$csvusername = $firstname . $lastname;
$csvpasswordname = $lastname . $firstname;
$status = "Open";
$sth = $DBH->prepare("INSERT INTO csvdata (firstname, lastname, email, phone, username, password, status, memberview) VALUES (?,?,?,?,?,?,?,?)");
$sth->execute(array($firstname, $lastname, $email, $phone, $csvusername, $csvpasswordname, $status, $memberview));
}
答案 1 :(得分:0)
不一定是您的问题的一部分,但您使用的是准备好的状态错误。使用准备好的语句的PArt是这样的,DB负责db特定的转义。为此,您使用占位符编写查询,然后将其替换为传递给语句的值。所以在这种情况下:
$insertdata = $DBH->prepare("INSERT INTO csvdata (firstname, lastname, email, phone) VALUES (?,?,?,?)");
$insertdata->execute(array($firstname,$lastname,$email,$phone));
或者使用更稳健的命名占位符:
$insertdata = $DBH->prepare("INSERT INTO csvdata (firstname, lastname, email, phone) VALUES (:firstname, :lastname, :email, :phone)");
$insertdata->execute(array(
':firstname' => $firstname,
':lastname' => $lastname,
':email' => $email,
':phone' => $phone
));
现在,只要您的错误ID愿意打赌您使用localhost
作为主机名,这可能会导致Windows上出现问题 - 请尝试使用127.0.0.1
。例如:
$DBH = new PDO("mysql:host=127.0.0.1;dbname=the_db_name", $username, $password);