首页 > 解决方案 > 如何调试“您的 SQL 语法有错误;请查看与您的 MariaDB 服务器版本相对应的手册以获取正确的语法使用”

问题描述

我一直在尝试更新一个实体,但我必须在更新页面上全部更新,否则会引发错误:

这是我的代码:

// to verify if user is an admin
    <?php include "include/uni_header.php"?>
    <?php
    if (isset($_GET['update'])){
        if ($_GET['update'] == null){
            header("Location: error/error.php");
        }else{
            $hlp = $_GET['update'];
        }
    }else{
        header("Location: error/error.php");
    }
    ?>
        <!-- mail -->
    
        <div class="latest-albums">
            <div class="container">
                <h3 class="agileits_w3layouts_head"> <span>Update existing</span></h3>
                <div class="wthree_latest_albums_grids gallery">
                    <div class="col-md-9 agile_mail_grid_left">
                       // here i am trying to fetch all value & putting them on the field
                        <?php
                                $sql = "select * from music where id=$hlp";
                                $getter = mysqli_query($connection, $sql);
                                while ($row=mysqli_fetch_assoc($getter)) {
                                    $id = $row['id'];
                                    $title = $row['title'];
                                    $singer = $row['singer'];
                                    $description = $row['description'];
                                    $length = $row['length'];
                                    $relese_date = $row['relese_date'];
                                    $lyrics = $row['lyrics'];
                                    $genere = $row['genere'];
                                    $songs = $row['songs'];
                                    $thumbnail = $row['thumbnail'];
                                    $language = $row['language'];
                                    $album_tag = $row['album_tag'];
                                    $youtube_id = $row['youtube_id'];
                                    $feature_tag = $row['feature_tag'];
                                }
                        ?>
                        // trying to update
                        <?php
                        if (isset($_POST['submits'])){
                            $title = $_POST['name'];
                            $singer = $_POST['singer'];
                            $description = $_POST['description'];
                            $length = $_POST['length'];
                            $date = $_POST['release_date'];
                            $lyrics = $_POST['lyrics'];
                            $category = $_POST['cat'];
                            $language = $_POST['cat1'];
                            $youtube = substr($_POST['youtube'], 17);
                            $album = $_POST['album'];
    
                            //thumbnail
                            $p_image = $_FILES['images']['name'];
                            $post_image_temp = $_FILES['images']['tmp_name'];
                            move_uploaded_file($post_image_temp, "thumbnail/$p_image");
    
                            //songs
                            $upload_file = $_FILES['files']['name'];
                            $temps = $_FILES['files']['tmp_name'];
                            move_uploaded_file($temps, "songs/$upload_file");
                            //query
                                $query = "update music set title='{$title}', singer='{$singer}', description='{$description}', length='{$length}', relese_date='{$date}', lyrics='{$lyrics}', genere={$category}, songs='{$upload_file}', thumbnail='{$upload_file}', language={$language}, album_tag='{$album}', youtube_id='{$youtube}' where id={$hlp}";
                                $sender = mysqli_query($connection, $query);
                                echo "error".mysqli_error($connection);
    
    
    
                        }
                        ?>
                       // pushing previously pulled value as user can see old values, it helps to update.
                        <form action="" method="post" enctype="multipart/form-data">
                            <label for="name">Enter Song Title</label>
                            <input type="text" name="name" placeholder="Name" value="<?php echo $title; ?>"><br>
    
                            <label for="email">Singer</label>
                            <input type="text" name="singer" placeholder="Singer" value="<?php echo $singer; ?>"><br>
    
                            <label for="description">Description</label>
                            <input type="text" name="description" placeholder="Description" value="<?php echo $description; ?>"><br>
    
                            <label for="length">Run Time</label>
                            <input type="text" name="length" placeholder="Run Time" value="<?php echo $length; ?>"><br>
    
                            <label for="images">Upload the song</label>
                            <input type="file" name="files" class="btn btn-default">
    
                            <label for="album">Album name</label>
                            <input type="text" name="album" placeholder="Album name" value="<?php echo $album_tag; ?>"><br>
    
                            <label for="youtube">Paste the Short URL only</label>
                            <input type="text" name="youtube" placeholder="ex: https://youtu.be/xxxxx" value="<?php echo $youtube_id; ?>"><br>
                            *This field only accepts SHORT URL <b>(open the song vid->share->copy link)</b>
                            <br><br>
    
                            <label for="country1">Choose Language</label>
                            <div class="btn-group">
                                <select id="country1" class="dropdown" name="cat1">
                                    <?php
                                            $sql1 = "select * from language where id=$language";
                                            $getter1 = mysqli_query($connection,$sql1);
                                            while ($r=mysqli_fetch_assoc($getter1)){
                                                $language=$r['language'];
                                            }
                                    ?>
                                    <option class="dropdown-header" value=""><?php echo $language; ?></option>
                                    <?php
                                    $querys = "select * from language";
                                    $sends = mysqli_query($connection, $querys);
    
                                    while ($row = mysqli_fetch_assoc($sends)) {
                                        $id = $row['id'];
                                        $language = $row['language'];
    
                                        echo "<option class='dropdown-item' value='{$id}'>{$language}</option>";
                                    }
                                    ?>
                                </select>
    
                            </div>
                            <br><br>
    
                            <label for="release_date">Release Date</label>
                            <input type="date" autocomplete="off" name="release_date" id="date" placeholder="Relese Date" value="<?php echo $relese_date; ?>"><br>
    
                            <br>
                            <label>Upload A Thumbnail Picture</label>
                            <input type="file" name="images" class="btn btn-default">
                            <br>
    
                            <label>Post the lyrics</label>
                            <textarea name="lyrics" placeholder="Description of the book"  id = "body"></textarea>
                            <br>
    
                            <label for="Genre">Choose Genre</label>
                            <div class="btn-group">
                                <select id="country" class="dropdown" name="cat">
                                    <option class="dropdown-header" value=""><?php echo $genere; ?></option>
                                    <?php
                                    $querys = "select * from category";
                                    $sends = mysqli_query($connection, $querys);
    
                                    while ($row = mysqli_fetch_assoc($sends)) {
                                        $id = $row['id'];
                                        $category = $row['title'];
    
                                        echo "<option class='dropdown-item' value='{$id}'>{$category}</option>";
                                    }
                                    ?>
                                </select>
    
                            </div>
    
                            <br><br>
    
                            <input type="submit" value="Post Your Song Now" name="submits">
                        </form>
                    </div>
    
                    <div class="clearfix"> </div>
                </div>
            </div>
        </div>
        <!-- //mail -->
    <?php include "include/about_footer.php"?>

当我单击提交时,它向我抛出了如下错误:

您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 1 行的 ' album_tag='fefef222', youtube_id='' where id=19' 附近使用正确的语法

我的数据库结构是:

CREATE TABLE 音乐( id int(3) NOT NULL,title varchar(255) NOT NULL,singer varchar(255) NOT NULL,描述文本 NOT NULL,长度 varchar(255) NOT NULL,relese_date varchar(255) NOT NULL,喜欢int(3) NOT NULL,歌词文本 NOT NULL,genere int(3) NOT NULL,歌曲文本 NOT NULL,缩略图文本 NOT NULL,语言 int(3) NOT NULL,album_tag varchar(255) NOT NULL,youtube_id varchar(255 ) NOT NULL, feature_tag varchar(255) NOT NULL DEFAULT 'general' ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

CREATE TABLE 语言(id int(11) NOT NULL, language varchar(255) NOT NULL)ENGINE=InnoDB DEFAULT CHARSET=latin1;

我的错在哪里?

标签: phpweb

解决方案


<label for="country1">Choose Language</label>
<div class="btn-group">
    <select id="country1" class="dropdown" name="cat1">
        <?php
                $sql1 = "select * from language where id=$language";
                $languageId = $language;

                $getter1 = mysqli_query($connection,$sql1);
                while ($r=mysqli_fetch_assoc($getter1)){
                    $language=$r['language'];
                }
        ?>
        <!-- you have an empty value which would result to 
            language =, album_tag='fefef222', 
             -->
        <!-- which is wrong.. -->


        <!-- you need the id, just incase the user doesn't alter the select box for another language -->
        <!-- this is the default language -->
        <option class="dropdown-header" value="<?=$languageId?>"><?php echo $language; ?></option>
        <?php
        $querys = "select * from language";
        $sends = mysqli_query($connection, $querys);

        while ($row = mysqli_fetch_assoc($sends)) {
            $id = $row['id'];
            $language = $row['language'];

            echo "<option class='dropdown-item' value='{$id}'>{$language}</option>";
        }
        ?>
    </select>

</div>
<br><br>


<!-- now your  query would run fine -->
<?php

$query = "update music set title='{$title}', singer='{$singer}', description='{$description}', length='{$length}', relese_date='{$date}', lyrics='{$lyrics}', genere={$category}, songs='{$upload_file}', thumbnail='{$upload_file}', language={$language}, album_tag='{$album}', youtube_id='{$youtube}' where id={$hlp}";

?>

推荐阅读