1

我不断收到No database selected错误。我不确定是否有办法检查是否有东西被阻止。

这是我的PHP代码:

<?php
  if(isset($_POST['submit']))
  {
    $fname = $_FILES['sel_file']['name'];

    $chk_ext = explode(".",$fname);

    if(strtolower($chk_ext[1]) == "csv")
    {
      $filename = $_FILES['sel_file']['tmp_name'];
      $handle = fopen($filename, "r");

      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
      {
        $sql = "INSERT into thingy(thingr,thing1,thing2) " .
            "values('$data[0]','$data[1]','$data[2]')";
        mysql_query($sql) or die(mysql_error());
      }
      fclose($handle);
      echo "Successfully Imported";
    }
    else
    {
      echo "Invalid File";
    }   
  } 
?>
<form action='' method='post' enctype='multipart/form-data' >

  Import File : <input type='file' name='sel_file' size='20'>
  <input type='submit' name='submit' value='submit'>

</form>
4

4 回答 4

1

要检查的显而易见的事情是您的数据库是否实际使用mysql_connect.

于 2012-11-13T03:33:14.320 回答
1

您需要先连接到 mysql,然后在开始执行 sql 查询之前选择一个数据库

尝试这个。

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
    die('Not connected : ' . mysql_error());
}

// make foo the current db
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
    die ('Can\'t use foo : ' . mysql_error());
}

if(isset($_POST['submit']))
   {
         $fname = $_FILES['sel_file']['name'];

         $chk_ext = explode(".",$fname);

         if(strtolower($chk_ext[1]) == "csv")
         {

             $filename = $_FILES['sel_file']['tmp_name'];
             $handle = fopen($filename, "r");

            while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
             {
                $sql = "INSERT into thingy(thingr,thing1,thing2) values('$data[0]','$data[1]','$data[2]')";
                mysql_query($sql) or die(mysql_error());
             }

             fclose($handle);
             echo "Successfully Imported";
         }
         else
        {
             echo "Invalid File";
         }   
    }

    ?>

   <form action='' method='post' enctype='multipart/form-data' >

       Import File : <input type='file' name='sel_file' size='20'>
        <input type='submit' name='submit' value='submit'>

    </form>
于 2012-11-13T03:37:17.413 回答
1

尝试这个,

<?php
  $conn_str = mysql_connect('localhost', 'yout_user_id', 'your_user_password');
  if (!$conn_str) {
    die('Not connected  to the database: ' . mysql_error());
  }

  $db_selected = mysql_select_db('your_database_name', $conn_str);
  if (!$db_selected) {
    die ("Can\'t use your_database_name : " . mysql_error());
  }
  if(isset($_POST['submit']))
  {
    $fname = $_FILES['sel_file']['name'];

    $chk_ext = explode(".",$fname);

    if(strtolower($chk_ext[1]) == "csv")
    {

      $filename = $_FILES['sel_file']['tmp_name'];
      $handle = fopen($filename, "r");

      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
      {
        $sql = "INSERT into thingy(thingr,thing1,thing2) " . 
            "values(\"$data[0]\",\"$data[1]\",\"$data[2]\")";
        mysql_query($sql) or die(mysql_error());
       }
       fclose($handle);
       echo "Successfully Imported";
     }
     else
     {
       echo "Invalid File";
     }   
   }
?>

<form action='' method='post' enctype='multipart/form-data' >

  Import File : <input type='file' name='sel_file' size='20'>
  <input type='submit' name='submit' value='submit'>
</form>
于 2012-11-13T03:44:01.563 回答
0

您需要mysql_select_db("your_dbName", $link_identifier)选择数据库..

mysql_select_db在 php.net 上查找更多详细信息

另外,开始使用mysqli函数或PDO语句..

于 2012-11-13T03:32:49.243 回答