[英]Importing csv files using PHP , It says itis imported yet its not
I was testing it and yes it let me select a file and sai that i "have successfully imported" the file and yet when i looked at my database it doesnt imported ... .........................................................................................................................................................................................................................................................................................................................................................我正在测试它,是的,它让我选择一个文件,然后说我“已成功导入”该文件,但是当我查看我的数据库时,它并没有导入……………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………… ………………………………………………………………………………………………………………………………………………………………………………………………………………
this is my function.php这是我的function.php
if(isset($_POST["Import"])){
$servername = "localhost";
$username = "root";
$password = "";
$db = "lcho_login";
try {
$conn = mysqli_connect($servername, $username, $password, $db);
if($_FILES["file"]["size"] > 0)
{
$file = fopen($filename, "r");
while (($getData = fgetcsv($file, 10000, ",")) !== FALSE)
{
$sql = "INSERT into lcho_dengue_activities (district_id,barangay_id,month,year,dengue_ind1,dengue_ind2,dengue_ind3,dengue_ind4,dengue_ind5,dengue_ind6,dengue_ind7,dengue_ind8,dengue_ind9,dengue_ind10,dengue_ind11)
values ('".$getData[0]."','".$getData[1]."','".$getData[2]."','".$getData[3]."','".$getData[4]."','".$getData[5]."','".$getData[6]."','".$getData[7]."','".$getData[8]."','".$getData[9]."','".$getData[10]."')";
echo "yes";
$result = mysqli_query($conn, $sql);
if(!isset($result))
{
echo "<script type=\"text/javascript\">
alert(\"Invalid File:Please Upload CSV File.\");
window.location = \"imports.php\"
</script>";
}
else {
echo "<script type=\"text/javascript\">
alert(\"CSV File has been successfully Imported.\");
window.location = \"imports.php\"
</script>";
}
}
fclose($file);
}
}
catch(exception $e)
{
echo "Connection failed: " . $e->getMessage();
}
return $conn;
$filename=$_FILES["file"]["tmp_name"];
}
?>
and this is my imports.php这是我的imports.php
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" crossorigin="anonymous">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" crossorigin="anonymous">
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" crossorigin="anonymous"></script>
</head>
<body>
<div id="wrap">
<div class="container">
<div class="row">
<form class="form-horizontal" action="functions.php" method="post" name="upload_excel" enctype="multipart/form-data">
<fieldset>
<!-- Form Name -->
<legend>Form Name</legend>
<!-- File Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="filebutton">Select File</label>
<div class="col-md-4">
<input type="file" name="file" id="file" class="input-large">
</div>
</div>
<!-- Button -->
<div class="form-group">
<label class="col-md-4 control-label" for="singlebutton">Import data</label>
<div class="col-md-4">
<button type="submit" id="submit" name="Import" class="btn btn-primary button-loading" data-loading-text="Loading...">Import</button>
</div>
</div>
</fieldset>
</form>
</div>
</div>
</div>
</body>
</html>
It is obvious from your shown code that you're not passing all the columns value on your $sql
query while trying to insert on table.从您显示的代码中可以明显看出,您在尝试插入表时没有传递$sql
查询中的所有列值。 Because I can see you've 15 columns but only passing 11 values.因为我可以看到您有15列但只传递了11 个值。 Why?为什么?
$sql = "INSERT into lcho_dengue_activities (district_id,barangay_id,month,year,dengue_ind1,dengue_ind2,dengue_ind3,dengue_ind4,dengue_ind5,dengue_ind6,dengue_ind7,dengue_ind8,dengue_ind9,dengue_ind10,dengue_ind11) values('".$getData[0]."','".$getData[1]."','".$getData[2]."','".$getData[3]."','".$getData[4]."','".$getData[5]."','".$getData[6]."','".$getData[7]."','".$getData[8]."','".$getData[9]."','".$getData[10]."')";
// Perform a query, check for error
if (!mysqli_query($con,$sql))
{
echo("Error description: " . mysqli_error($con));
}
Because of the column and passed value number mismatch the mysqli_query($con,$sql)
function returning false
for $result
so if(!isset($result))
line evaluates false
and goes to else block which contains the message CSV File has been successfully Imported.
由于列和传递的值编号不匹配, mysqli_query($con,$sql)
函数为$result
返回false
,因此if(!isset($result))
行评估为false
并转到包含消息CSV File has been successfully Imported.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.