php - 通过 php 填写 mysql 表时出错

标签 php mysql sql syntax

我尝试编写一个代码,通过 php 将一个条目添加到我的 MySQL 表(称为“rechnungen”)。所以我在 html 中做了一些输入,最后我尝试将信息插入到我的表中(使用 INSERT INTO... 命令)。这就是我所做的:

<?php
    Session_Start();

    $username=$_SESSION['username'];
    $password=$_SESSION['password'];
    $dbname=$_SESSION['dbname'];
    $servername=$_SESSION['hostname'];

    /*conn dev*/
    $conn = mysql_connect($servername, $username, $password);

    if($conn === false){
    header("Location: LogIn.php");
    }
?>

    <!DOCTYPE html>
    <html>
    <head>
    <link rel="stylesheet" type="text/css" href="style.css">
        <title></title>
    </head>
    <body>
        <main>
            <form method="POST" action="">
                <div class="form_neueRechnung">
                    <!-- part 1 -->
                    <input type="text" name="suche_Vname_Patienten" placeholder="Vorname" required="">
                        <input type="text" name="suche_Nname_Patienten" placeholder="Nachname" required="">
                            <input type="number" id="id_Patient" name="id_patient" placeholder="Pat. Nr." Value="
                                <?php echo $KID_output; ?>" required="">
                            </td>
                            <input type="radio" name="Behandlung" value="Osteopathie" onclick="andere()" required="">
                                <input type="radio" name="Behandlung" value="Krankengymnastik" onclick="andere()" required="">
                                    <input type="radio" name="Behandlung" id="andere_Behandlung" value="andere" onclick="andere()" required="">
                                        <input type="text" name="andereBehandlung_text" id="andereBehandlung_text" placeholder="andere" style="visibility:hidden">
                                            <!-- part 2 -->
                                            <input type="radio" name="rezept_rechnung" id="mit_rezept" value="mit_Rezept" onclick="rezept()" required="">
                                                <input type="radio" name="rezept_rechnung" id="ohne_rezept" value="ohne_Rezept" onclick="rezept()" required="">
                                                    <input type="text" id="ohne_rezept_text" name="ohne_rezept_text" placeholder="freier Text">
                                                        <!-- part 3 -->
                                                        <input type="time" name="termin1_von" required="">
                                                            <input type="time" name="termin1_bis" required="">
                                                                <input type="date" name="termin1_date" required="">
                                                                    <!-- submit -->
                                                                    <input type="submit" class="submit" value="Rechnug erstellen" name="submit" id="submit">
                                                                    </div>
                                                                    <div class="form_fieldset" id="rezept_einstellungen" style="visibility:hidden">
                                                                        <input type="date" id="rezept_datum" name="rezept_datum">
                                                                            <input type="text" id="rezept_verordnung" name="rezept_verordnung">
                                                                                <input type="text" id="rezept_diagnose" name="rezept_diagnose">
                                                                                </div>
                                                                            </form>
                                                                            <script type="text/javascript">     

            function andere() {

            if (document.getElementById('andere_Behandlung').checked) {
                document.getElementById('andere_BehandlungArt').style.visibility = 'visible';
            } else {
                document.getElementById('andere_BehandlungArt').style.visibility = 'hidden';
            }
            }

            function rezept() {

            if (document.getElementById('mit_rezept').checked) {
                document.getElementById('rezept_einstellungen').style.visibility = 'visible';
            } else {
                document.getElementById('rezept_einstellungen').style.visibility = 'hidden';
            }

            if (document.getElementById('ohne_rezept').checked) {
                document.getElementById('ohne_rezept_text').style.visibility = 'visible';
            } else {
                document.getElementById('ohne_rezept_text').style.visibility = 'hidden';
            }
            }

        </script>
                                                                            <?php
            mysql_connect("$servername","$username","$password") or die("connection failed!");
            mysql_select_db($dbname) or die ("no database found");
            $query = mysql_query("SELECT * FROM `rechnungen`");

            while($row = mysql_fetch_array($query)){
                $RID = $row['RechnungsID'];
            }
            $RechnungsID = max($RID ,$RID)+1;

            echo $RechnungsID;

            $mit_ohne_Rezept = "";
            if(isset($_POST['submit'])) {
                if($_POST['rezept_rechnung'] == "mit_Rezept") {
                $mit_ohne_Rezept = "1";
                }
                else {
                $mit_ohne_Rezept = "0";
                }
            }


            if(isset($_POST['submit'])){
                $KundenID=$_POST['id_patient'];
                $Behandlung=$_POST['Behandlung'];
                $Rezept_datum=$_POST['rezept_datum'];
                $Rezept_Verordnung=$_POST['rezept_verordnung'];
                $Rezept_Diagnose=$_POST['rezept_diagnose'];
                $ohneRezept_text=$_POST['ohne_rezept_text'];

                mysql_select_db($dbname,$conn);                        
                $result = "INSERT INTO rechnungen (`RechnungsID`, `KundenID`, `Behandlung`, `mit_ohne_Rezept`, `Rezept_datum`, `Rezept_Verordnung`, `Rezept_Diagnose`, `ohneRezept_text`)
                                VALUES ('$RechnungsID','$KundenID','$Behandlung','$mit_ohne_Rezept','$Rezept_datum','$Rezept_Verordnung','$Rezept_Diagnose','$ohneRezept_text)";      
                if (mysql_query($result)) {
                echo ("finished!");
                } else {
                echo "error". mysql_error();
                }
            }    
            mysql_close($conn);
            ?>
                                                                        </main>
                                                                    </body>
                                                                </html>

我知道这是一个很长的代码,但我不知道问题出在哪里。我收到此错误:

errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''sdfas)' at line 2

请帮助我。我很绝望。

最佳答案

','$ohneRezept_text)";

看起来问题就在这里。

缺少引号?

这就是错误的意思

您也不需要将变量括在引号中,当然可以,但仍然很痛苦。如果您的输入包含引号,它将直接跳过。使用addslashes()

关于php - 通过 php 填写 mysql 表时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30969063/

相关文章:

php - 如何在 php/mysql 中显示数据库中的图像。如果使用 C#/Vb.net 将图像插入数据库

php - Mysql只显示日期之间的消息

sql - Oracle 通过包含停止条件进行连接

sql - 消息 2760,级别 16,状态 1,第 5 行指定的架构名称 "items"不存在或您无权使用它

php - 没有错误,状态 200,但没有数据进入我的表

javascript - 从 javascript 获取值到 php 变量

MySQL计数语句

MySQL:在带有 INNER JOIN 的 Where 子句中面临问题

mysql - sql数据库中的条件

php - Laravel,数百万行插入和搜索