我使用以下代码来使用PHP修改数据库:
<?php
//connect to the database
$connect = mysql_connect("localhost","root","");
mysql_select_db("loginform",$connect); //select the table
//
if ($_FILES['csv']['size'] > 0) {
//get the csv file
$file = $_FILES['csv']['tmp_name'];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO mytable( name,country,age) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
//
//redirect
header('Location: sample.php?success=1'); die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import a CSV File with PHP & MySQL</title>
</head>
<body>
<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>
<form action="" 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>
</body>
</html>
我能够将数据插入到MySQL表中,但问题是我收到错误:
Notice: Undefined index: csv in C:\xampp\htdocs\deepthi\excel\sample.php on line 8
Notice: Use of undefined constant success - assumed 'success' in C:\xampp\htdocs\deepthi\excel\sample.php on line 44
我认为这是因为在页面加载时没有回发,即使代码正在执行。如何通过单击“提交”按钮来检查该页面是否已加载?有人能帮助我吗?
答案 0 :(得分:2)
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo "Page is called via a POST method";
}
答案 1 :(得分:1)
在if
声明
if($_SERVER['REQUEST_METHOD'] == 'POST')
上面的代码表明页面请求来自提交按钮。 (POST类型)
e.g:
<?php
//connect to the database
$connect = mysql_connect("localhost","root","");
mysql_select_db("loginform",$connect); //select the table
//
if($_SERVER['REQUEST_METHOD'] == 'POST')
if ($_FILES['csv']['size'] > 0) {
//get the csv file
$file = $_FILES['csv']['tmp_name'];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO mytable( name,country,age) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
//
//redirect
header('Location: sample.php?success=1'); die;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import a CSV File with PHP & MySQL</title>
</head>
<body>
<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>
<form action="" 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>
</body>
</html>
答案 2 :(得分:0)
您的变量没有价值,而您正在访问它。 请更新
if ($_FILES['csv']['size'] > 0) {
要,
if (isset($_FILES['csv']['size'])) {
if ($_FILES['csv']['size'] > 0) {
另外,添加右括号}
您的成功是数组的关键,应该用逗号括起来('
)。
并且还改变:
<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>
到
<?php if (isset($_GET['success']) && !empty($_GET['success'])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>
答案 3 :(得分:0)
正如Sunny Gupta所说,
检查方法:如果表单已提交(单击提交按钮),则设置 $ _ POST 。您希望仅处理 $ _ FILES
sidenote 1 :对数组使用不带引号的索引会触发警告和日志。在最好的情况下,这是无用的。考虑使用引号而不是非现有的PHP常量:
if(!empty($ _ GET [&#39; success&#39;]))
sidenote 2 : do / while 将始终在第一个循环中记录错误。首选 $ data ,然后使用它,而不是之后。另外,如果您想阅读3个条目,请不要只检查 $ data [0] 。
while($ data = fgetcsv($ handle,1000,&#34;,&#34;,&#34;&#39;&#34;)){ if(count($ data)&gt; 2){[...] } };
旁注4:最后但并非最不重要的是,不推荐使用 mysql_xxx 函数。请考虑使用 mysqli_xxx 函数。并且更喜欢mysqli_real_rescape_string到 addslashes(),它更安全。
答案 4 :(得分:0)
POST语句检查
if((isset($_POST['Submit']) && trim($_POST['Submit']) == "Submit") && ($_SERVER['REQUEST_METHOD'] == 'POST'))
<?php
//connect to the database
$connect = mysql_connect("localhost","root","");
mysql_select_db("loginform",$connect); //select the table
//
if((isset($_POST['Submit']) && trim($_POST['Submit']) == "Submit") && ($_SERVER['REQUEST_METHOD'] == 'POST'))
{
if ($_FILES['csv']['size'] > 0)
{
//get the csv file
$file = $_FILES['csv']['tmp_name'];
$handle = fopen($file,"r");
//loop through the csv file and insert into database
do {
if ($data[0]) {
mysql_query("INSERT INTO mytable( name,country,age) VALUES
(
'".addslashes($data[0])."',
'".addslashes($data[1])."',
'".addslashes($data[2])."'
)
");
}
} while ($data = fgetcsv($handle,1000,",","'"));
//
//redirect
header('Location: sample.php?success=1'); die;
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Import a CSV File with PHP & MySQL</title>
</head>
<body>
<?php if (!empty($_GET[success])) { echo "<b>Your file has been imported.</b><br><br>"; } //generic success notice ?>
<form action="" 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>
</body>
</html>