1

我正在使用以下代码来使用 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 

我认为这是因为当页面加载时没有回发,即使代码正在执行。如何通过单击提交按钮检查该页面是否加载?有人能帮我吗?

4

5 回答 5

2
 if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo "Page is called via a POST method";

}

于 2013-10-03T09:13:26.923 回答
1

if在您的声明之前使用以下代码

if($_SERVER['REQUEST_METHOD'] == 'POST')

上面的代码表明页面请求来自一个提交按钮。(POST 类型)

例如:

    <?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>
于 2013-10-03T08:45:29.837 回答
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>
于 2013-10-03T09:27:09.730 回答
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 ?>
于 2013-10-03T08:46:10.600 回答
0

正如桑尼·古普塔所说,

  1. 检查方法:如果表单已提交(单击提交按钮),则设置$_POST。您只想处理$_FILES是这种情况

  2. 旁注 1:对数组使用不带引号的索引会触发警告和日志。在最好的情况下,那是无用的。考虑使用引号而不是不存在的 PHP 常量:

    if (!empty($_GET['success']))

  3. 旁注 2:您的do/while将始终在第一个循环中记录错误。更喜欢干净的代码并在使用之前初始化$data ,而不是之后。此外,如果您想阅读 3 个条目,请不要只检查$data[0] 。

    while ($data = fgetcsv($handle,1000,","'")) { if (count($data) > 2) { [...] } } ;

  4. 旁注 4:最后但同样重要的是,不推荐使用mysql_xxx函数。考虑改用mysqli_xxx函数。并且更喜欢mysqli_real_rescape_string到addlashes (),它更安全。

于 2013-10-03T09:01:43.783 回答