我目前正試圖從一個CSV文件插入數據到我的mysql數據庫中。該表單和所有功能正常工作,因爲我已經測試過,以確保它正在讀取文件等。當我準備將PDO插入到mysql中時,我在Chrome中沒有出現任何錯誤。現在,當我讓它當我在Chrome(任何瀏覽器)服務器錯誤,我看着我的錯誤日誌文件中顯示了其中的功能是說是:SQLSTATE [HY000] [2002]錯誤
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;
已更新-------------------------------------繼承人一些信息
PHP信息 - PHP Version 5.3.10, System Windows NT W03 6.0 build 6002 (Windows Server 2008 Standard Edition Service Pack 2) i586
修訂----------------------------------- -
我能夠連接到MySQL服務器並瀏覽它 - 插入來自我所做的其他功能的數據,並且它們都工作得很完美。只是這個會給我一個錯誤。
UPDATED --------------------------------------------- ----------------------------
想通了,謝謝你的幫助。我實際上需要填寫所有的數據列才能正常工作。例如我有(姓,名,電子郵件,電話)我需要填寫用戶名,密碼,狀態部分。感謝您的幫助!希望這會幫助任何落在同一個洞裏的人!
也許您的用戶名和密碼不正確 – Ibu 2012-08-17 17:39:54
否我檢查; /。 – 2012-08-17 17:41:59
@Ibu不,「機器主動拒絕它」意味着防火牆問題或服務器沒有監聽TCP。 – 2012-08-17 17:42:21