【发布时间】:2016-02-10 14:39:52
【问题描述】:
我的主机刚刚贬低了 mysql 连接以支持 mysqli
这个特定的脚本是一个示例,但在我尝试过渡到 Mysqli 之前运行良好。不幸的是,我在另一个工作岗位上从事 php 编程已有 4 年了,并且已经有一段时间没有看过这个了。所以请温柔:-)
我能够连接到数据库,但输出如下所示: 错误:INSERT INTO 文章(名称、参考)VALUES ('','')
我一定是对 $data 变量做错了,但我不知道,因为它适用于以前的版本。
感谢您的帮助
<?php
ini_set('auto_detect_line_endings',TRUE);
function clean($str, $default = '') {
if (!isset($str)) $str = $default;
$str = @trim($str);
if(get_magic_quotes_gpc()) {
$str = stripslashes($str);
}
return mysql_real_escape_string($str);
}
$host_name = "some host";
$database = "some database";
$user_name = "someusername";
$password = "some password";
$connect = mysqli_connect($host_name, $user_name, $password, $database);
if (mysqli_connect_errno()){ echo "Failed to connect to MySQL: " . mysqli_connect_error(); } else {echo 'connected to DB<br />';}
$lines = $value1 = $value2 = $data = 0;
if ($_FILES["csv"]["size"] > 0) {
//get the csv file
$file = $_FILES["csv"]["tmp_name"];
$handle = fopen($file,"r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($data[0]) {
$sql = "INSERT INTO articles (name, reference) VALUES ('".clean($data[0])."','".clean($data[1])."')";
if (mysqli_query($conn, $sql)) { } else { echo "Error: " . $sql . "<br>" . mysqli_error($conn); }
$lines++;
}
}
header('Location: <?php echo $_SERVER["PHP_SELF"]; ?>?success=1&lines='.$lines.''); die;
}
if ( isset($_GET['success']) )
{
$success = $_GET['success'];
$lines = $_GET['lines'];
} else {
$success = 0;
}
mysqli_close($conn);
if ($success != 0 ) {
echo "<b>Your file has been imported.</b><br>";
echo "Found a total of ".$lines." records in this csv file.<br />";
}
?>
<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data" name="form1" id="form1">
Choose your file: <br />
<input name="csv" type="file" id="csv" />
<input type="submit" name="Submit" value="Submit" />
</form>
【问题讨论】:
-
你遇到了什么错误?
标签: php csv mysqli mysqlimport