我目前正在研究一个示例系统,该系统可以将csv文件上传到我的phpmyadmin MySQL数据库中。
这很奇怪,因为不再在phpmyadmin中插入任何数据。
我的代码以前曾经工作过,后来上传了几次数据。 每次我上传csv时,都会出现以下错误:
注意:未定义变量:第43行的C:\ xampp \ htdocs \ intern \ orders.php中的sql
上传源代码:
<?php
$con = mysqli_connect('localhost','root','') or die (mysql_error());
mysqli_select_db($con, 'test');
$mimes = array('application/vnd.ms-excel','text/plain','text/csv','text/tsv');
if(isset($_POST['submit'])){
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file,"r");
$num = 0;
if(in_array($_FILES['file']['type'],$mimes)){
while(($fileop = fgetcsv($handle,2000,",")) !==false){
if($num == 0){
$num++;
}
else{
$KeyAccount = $fileop[0];
$BatchNumber= $fileop[1];
$Product = $fileop[2];
$Quantity = $fileop[3];
$PO = $fileop[4];
$DateRequested = $fileop[5];
$DateDelivered = $fileop[6];
$Status = $fileop[7];
$Serial = $fileop[8];
$Voucher = $fileop[9];
$DateExpiry = $fileop[10];
$sql = mysqli_query($con, "INSERT INTO orders (KeyAccount,BatchNumber,Product,Quantity,PO,DateRequested,DateDelivered,Status,Serial,Voucher,DateExpiry) VALUES ('$KeyAccount','$BatchNumber','$Product','$Quantity','$PO','$DateRequested','$DateDelivered','$Status','$Serial','$Voucher','$DateExpiry')");
}
}
}
else {
echo '<script language="javascript">';
echo 'alert("INVALID FILE FORMAT.")';
echo '</script>';
die(header( "refresh:0;" ));
}
if($sql){
echo '<script language="javascript">';
echo 'alert("Successfully Inserted.")';
echo '</script>';
}
else{
echo "error";
}
}
?>
感谢进阶!
答案 0 :(得分:1)
只需在此条件if(isset($ _ POST ['submit'])))之前定义此$ sql变量
$sql='';
if(isset($_POST['submit']))
答案 1 :(得分:0)
如果未上传任何文件,它将不会进入IF
条件,并且不会声明$sql
变量。
您可以进行以下更改并解决问题:
如果要检查文件的条件,请先将以下完整代码移入内部:
if(in_array($_FILES['file']['type'],$mimes)){
while(($fileop = fgetcsv($handle,2000,",")) !==false){
if($num == 0){
$num++;
}
else{
$KeyAccount = $fileop[0];
$BatchNumber= $fileop[1];
$Product = $fileop[2];
$Quantity = $fileop[3];
$PO = $fileop[4];
$DateRequested = $fileop[5];
$DateDelivered = $fileop[6];
$Status = $fileop[7];
$Serial = $fileop[8];
$Voucher = $fileop[9];
$DateExpiry = $fileop[10];
$sql = mysqli_query($con, "INSERT INTO orders (KeyAccount,BatchNumber,Product,Quantity,PO,DateRequested,DateDelivered,Status,Serial,Voucher,DateExpiry) VALUES ('$KeyAccount','$BatchNumber','$Product','$Quantity','$PO','$DateRequested','$DateDelivered','$Status','$Serial','$Voucher','$DateExpiry')");
if($sql){
echo '<script language="javascript">';
echo 'alert("Successfully Inserted.")';
echo '</script>';
}
else{
echo "error";
}
}
}
}
答案 2 :(得分:0)
您应该使用mysqli_effected_rows来验证插入了多少条记录。 关于不插入,您需要检查文件是否正确读取,Wat插入语句以及是否抛出任何错误消息。
if(in_array($_FILES['file']['type'],$mimes)){
$inteffectedRows = 0;
while(($fileop = fgetcsv($handle,2000,",")) !==false) {
$KeyAccount = $fileop[0];
$BatchNumber= $fileop[1];
$Product = $fileop[2];
$Quantity = $fileop[3];
$PO = $fileop[4];
$DateRequested = $fileop[5];
$DateDelivered = $fileop[6];
$Status = $fileop[7];
$Serial = $fileop[8];
$Voucher = $fileop[9];
$DateExpiry = $fileop[10];
$sql = mysqli_query($con, "INSERT INTO orders (KeyAccount,BatchNumber,Product,Quantity,PO,DateRequested,DateDelivered,Status,Serial,Voucher,DateExpiry) VALUES ('$KeyAccount','$BatchNumber','$Product','$Quantity','$PO','$DateRequested','$DateDelivered','$Status','$Serial','$Voucher','$DateExpiry')");
$inteffectedRows += mysqli_effected_rows($con);
}
if( 0 < $inteffectedRows ) {
echo '<script language="javascript">';
echo 'alert("Successfully Inserted.")';
echo '</script>';
}
else{
echo "error";
}
}
else {
echo '<script language="javascript">';
echo 'alert("INVALID FILE FORMAT.")';
echo '</script>';
die(header( "refresh:0;" ));
}