使用PHP更新MYSQL数据库

时间:2016-09-20 03:12:25

标签: php mysql

我正在做一个评估,我需要创建一个连接到PHP MyAdmin的表单,但我一直得到Parse错误:语法错误,第40行的C:\ xampp \ htdocs \ import.php中的意外文件结束。我不知道如何解决它。

<!DOCTYPE html>

<title>HTML Tutorial</title>

<body>

<?php

$servername = "localhost";
$username = "root";
$password = "";

// Create connection
 $conn = mysqli_connect($servername, $username,$password,"movieclub");

// Check connection
 if (!$conn) {
     die("Connection failed: " . mysqli_connect_error());
}
//successful connection, 


//Opens and imports movieclub.csv file
 $file = fopen("TRY.csv",'r');

 while(! feof($file))
   {
$CurrentLine = (fgetcsv($file));
if ($CurrentLine != "")
{
$result = mysqli_query($conn, "SELECT  Title FROM movie WHERE Title='".$CurrentLine[0]."'");

$movieQuery = mysqli_query($conn,"INSERT INTO movie(Title, Director, Producer, Release_Date, Running Time (Mins), Genre, Starring, Distributor) VALUES ('".$CurrentLine[0]."', '".$CurrentLine[1]."', '".$CurrentLine[2]."', '".$CurrentLine[3]."', '".$CurrentLine[4]."', '".$CurrentLine[5]."', '".$CurrentLine[6]."','".$CurrentLine[7]."','".$CurrentLine[8]."' )");

print_r(mysqli_error($conn));
mysqli_close($conn);
?>

</body> 
</html>

4 个答案:

答案 0 :(得分:1)

你忘记了代码中的花括号。确保所有括号在

之前关闭
 ?>

关闭Canonical Tag。 此错误告诉您代码没有结束点。 所以请放

  }

结束前的这些括号。

答案 1 :(得分:0)

你没有关闭while和if条件。

更新的代码是,

<!DOCTYPE html>

<title>HTML Tutorial</title>

<body>

<?php

$servername = "localhost";
$username = "root";
$password = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password, "movieclub");

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
//successful connection,

//Opens and imports movieclub.csv file
$file = fopen("TRY.csv", 'r');

while (!feof($file)) {
    $CurrentLine = (fgetcsv($file));
    if ($CurrentLine != "") {
        $result = mysqli_query($conn, "SELECT  Title FROM movie WHERE Title='" . $CurrentLine[0] .
            "'");

        $movieQuery = mysqli_query($conn,
            "INSERT INTO movie(Title, Director, Producer, Release_Date, Running Time (Mins), Genre, Starring, Distributor) VALUES ('" .
            $CurrentLine[0] . "', '" . $CurrentLine[1] . "', '" . $CurrentLine[2] . "', '" .
            $CurrentLine[3] . "', '" . $CurrentLine[4] . "', '" . $CurrentLine[5] . "', '" .
            $CurrentLine[6] . "','" . $CurrentLine[7] . "','" . $CurrentLine[8] . "' )");

        if(!$movieQuery)  print_r(mysqli_error($conn));
    }
}

mysqli_close($conn);

?>

</body> 
</html>

答案 2 :(得分:0)

缺少卷曲括号。

<!DOCTYPE html>

<title>HTML Tutorial</title>

<body>

<?php

  $servername = "localhost";
  $username = "root";
  $password = "";

// Create connection
 $conn = mysqli_connect($servername, $username,$password,"movieclub");

 // Check connection
 if (!$conn) {
  die("Connection failed: " . mysqli_connect_error());
  }
 //successful connection, 


    //Opens and imports movieclub.csv file
    $file = fopen("TRY.csv",'r');

    while(! feof($file))
    {
     $CurrentLine = (fgetcsv($file));
     if ($CurrentLine != "")
     {
      $result = mysqli_query($conn, "SELECT  Title FROM movie WHERE Title='".$CurrentLine[0]."'");

    $movieQuery = mysqli_query($conn,"INSERT INTO movie(Title, Director, Producer, Release_Date, Running Time (Mins), Genre, Starring, Distributor) VALUES ('".$CurrentLine[0]."', '".$CurrentLine[1]."', '".$CurrentLine[2]."', '".$CurrentLine[3]."', '".$CurrentLine[4]."', '".$CurrentLine[5]."', '".$CurrentLine[6]."','".$CurrentLine[7]."','".$CurrentLine[8]."' )");

}
}
print_r(mysqli_error($conn));
mysqli_close($conn);
?>

</body> 
</html>

答案 3 :(得分:0)

注意:建议使用Prepared语句,您可以使用bind_parm执行语句。因为如果您使用prepared statement,则可以避免SQL注入。或者esle你对SQL注入很开放。

  

解析错误:语法错误,第40行的C:\ xampp \ htdocs \ import.php中的文件意外结束

您遇到的错误是由于您正在制作的语法错误。确保您必须关闭已打开的所有大括号,并在执行文件之前检查是否已关闭所有php标记。

错误:您在print_r(mysqli_error($conn));行之前的最后一个错过了大括号。

您已打开while循环并执行该语句,但您尚未关闭while循环所需的大括号。对于您在IF内打开的while也会跟进。

这是您在Parse Error中遇到的错误。

因此,您的纠正后的代码如下所示。

<强> import.php

<!DOCTYPE html>
<title>HTML Tutorial</title>
<body>
<?php
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
 $conn = mysqli_connect($servername, $username,$password,"movieclub");
// Check connection
 if (!$conn) {
     die("Connection failed: " . mysqli_connect_error());
}
//successful connection, 
//Opens and imports movieclub.csv file
$file = fopen("TRY.csv",'r');
while(! feof($file))
{
$CurrentLine = (fgetcsv($file));
if ($CurrentLine != "")
{
$result = mysqli_query($conn, "SELECT  Title FROM movie WHERE Title='".$CurrentLine[0]."'");
$movieQuery = mysqli_query($conn,"INSERT INTO movie(Title, Director, Producer, Release_Date, Running Time (Mins), Genre, Starring, Distributor) VALUES ('".$CurrentLine[0]."', '".$CurrentLine[1]."', '".$CurrentLine[2]."', '".$CurrentLine[3]."', '".$CurrentLine[4]."', '".$CurrentLine[5]."', '".$CurrentLine[6]."','".$CurrentLine[7]."','".$CurrentLine[8]."' )");
}
}
print_r(mysqli_error($conn));
mysqli_close($conn);
?>
</body> 
</html>