简体   繁体   English

我如何解决这个 MySQL 错误?

[英]How do i troubleshoot this MySQL error?

I'm making a simple website for a class, and I am trying to save information to my database.我正在为一个班级制作一个简单的网站,我正在尝试将信息保存到我的数据库中。 The error is not very specific and I do not know which part of my code I need to fix.该错误不是很具体,我不知道我需要修复代码的哪一部分。

Error message:错误信息:

check the manual that corresponds to your MariaDB server version for the right syntax to use near ')' at line 2检查与您的 MariaDB 服务器版本相对应的手册,以获取在第 2 行的“)”附近使用的正确语法

My PHP code:我的PHP代码:

<?php
include 'mysqli.php' ;

$result = $con->query("select * from setList s 
left join songTable t on s.SetList_ID = t.Song_ID
left join bands b on s.SetList_ID = b.Band_ID");

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$setList = $_POST['setlist'];
$venue = $_POST['venue'];
$date = $_POST['dateOfShow'];
$band= $_POST['band'];

$set = $result->fetch_object();

//error handling and form
try {
    if (empty($setList) || empty($venue) || empty($date) ||         empty($band)) {
        throw new Exception(
            "All Fields Required");
    }

    if (isset($set)) {
        $id = $set->SetList_ID;

   $q = "update setList set SetList_Name = '$setList',
            Venue = '$venue', Show_Date = $date, Band_Name = '$band')";   
        }
    else{

    $q = "insert setList (SetList_Name, Venue, Show_Date, Band_Name)
        values ('$setList', '$venue', $date, '$band')";
    }

    $result = $con->query($q);
    if (!$result) {
        throw new Exception($con->error);
    }

     header('Location:my_set-lists.php');
} catch(Exception $e) {
    echo '<p class ="error">Error: ' .
    $e->getMessage() . '</p>';
  }
 }
?>

The error message tells you exactly where the problem is;错误消息会准确地告诉您问题出在哪里; you have an extra ) .你有一个额外的) Replace代替

$q = "update setList set SetList_Name = '$setList',
        Venue = '$venue', Show_Date = $date, Band_Name = '$band')";
// extra ) is here ---------------------------------------------^

With

$q = "update setList set SetList_Name = '$setList',
        Venue = '$venue', Show_Date = $date, Band_Name = '$band'";

Note: your next query (starting insert setList ) is also going to fail;注意:您的下一个查询(开始insert setList )也会失败; it should be INSERT INTO setList... .它应该是INSERT INTO setList... A decent IDE (like PHPStorm) would catch these errors for you.一个不错的 IDE(如 PHPStorm)会为您捕获这些错误。

Also, you are wide open to SQL injection .此外,您对SQL 注入持开放态度 You really need to be using prepared statements.您确实需要使用准备好的语句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM