簡體   English   中英

我的 SQL 查詢有什么問題?

[英]What's wrong with my SQL query?

我需要快速查看一下我的 SQL 查詢。 已經讓我頭疼了。 比較我得到的一些建議,我無法為我的 SQL 找出正確的格式。

這是我的代碼:

$query = "INSERT INTO `[my_db_name]`.`members` (`id` ,`first` ,`last` ,`add1` ,`add2` ,`phone_home` ,`phone_cell` ,`phone_dad` ,`phone_mom` ,`email1` ,`email2`)
    VALUES ('NULL' , '".$first."', '".$last."', '".$add1."', '".$add2."', '".$phone_home."', '".$phone_cell."', '".$phone_dad."', '".$phone_mom."', '".$email1."', '".$email2."')";

`mysql_query($query) or die ('Error updating database, Error:' . mysql_error());

提前致謝!

編輯:

目前我有這個作為形式:

<table border="0" cellpadding="0" cellspacing="0">

            <form  enctype="multipart/form-data" method="POST" action="add-member.php">

            <tr class="labels">

                <td class="f">Add a Member</td>
                <td class="l"></td>

            </tr>

            <tr>

                <td class="f"><label for="first">First Name:</label></td>
                <td class="l"><input id="first" type="text"/></td>

            </tr>


            <tr>

                <td class="f"><label for="last">Last Name:</label></td>
                <td class="l"><input id="last" type="text"/></td>

            </tr>

            <tr>

                <td class="f"><label for="add1">Address 1:</label></td>
                <td class="l"><input id="add1" type="text"/></td>

            </tr>

            <tr>

                <td class="f"><label for="add2">Address 2:</label></td>
                <td class="l"><input id="add2" type="text"/></td>

            </tr>

            <tr>

                <td class="f"><label for="phone_home">Home Phone:</label></td>
                <td class="l"><input id="phone_home" type="text"/></td>

            </tr>

            <tr>

                <td class="f"><label for="phone_cell">Cell Phone:</label></td>
                <td class="l"><input id="phone_cell" type="text"/></td>

            </tr>

            <tr>

                <td class="f"><label for="phone_dad">Dad Cell:</label></td>
                <td class="l"><input id="phone_dad" type="text"/></td>

            </tr>

            <tr>

                <td class="f"><label for="phone_mom">Mom Cell:</label></td>
                <td class="l"><input id="phone_mom" type="text"/></td>

            </tr>

            <tr>

                <td class="f"><label for="email1">Email 1:</label></td>
                <td class="l"><input id="email1" type="text"/></td>

            </tr>

            <tr>

                <td class="f"><label for="email2">Email 2:</label></td>
                <td class="l"><input id="email2" type="text"/></td>

            </tr>

        </table>

這是插入查詢:

<?php

    $first = $_POST['first'];
    $last = $_POST['last'];
    $add1 = $_POST['add1'];
    $add2 = $_POST['add2'];
    $phone_home = $_POST['phone_home'];
    $phone_cell = $_POST['phone_cell'];
    $phone_dad = $_POST['phone_dad'];
    $phone_mom = $_POST['phone_mom'];
    $email1 = $_POST['email1'];
    $email2 = $_POST['email2'];

    include ("../lib/login.php");

    mysql_connect($hostname, $username, $password)
     or die("Unable to connect to MySQL");

    mysql_select_db ([dbname]);

    $query = "INSERT INTO `[dbname]`.`mem_dir` (`id` ,`first` ,`last` ,`add1` ,`add2` ,`phone_home` ,`phone_cell` ,`phone_dad` ,`phone_mom` ,`email1` ,`email2`)
            VALUES (NULL , '$first', '$last', '$add1', '$add2', '$phone_home', '$phone_cell', '$phone_dad', '$phone_mom', '$email1', '$email2')";

    mysql_query($query) or die ('Error updating database, Error:' . mysql_error());

    echo "Database successfully uploaded with:<br/><ul>
    <li>" . $first . "</li>
    ...
    </ul>";

?>

似乎提交了條目,但沒有提交值......我厭倦了看着它卻沒有看到它。 我感謝所有的幫助。 謝謝!

編輯(再次):

正如Salman A所指出,問題實際上是形式,以及每個輸入都用“id”而不是“name”標識的事實。

謝謝大家,我真的很感謝所有的幫助! 我想我不正確的 SQL 格式是另一個話題,嗯?

不應引用NULL

您可以簡化值列表(因為這似乎是PHP):

VALUES (NULL , '$first', '$last', '$add1', '$add2', '$phone_home', '$phone_cell', '$phone_dad', '$phone_mom', '$email1', '$email2')";

它的形狀聞起來! 改變所有:

<input id="yadayada">

至:

<input name="yadayada">
<!--   ^^^^---- you must use the name attribute -->

可選的:

  • 表格標簽放錯了位置; 我建議你把<table>放在<form> ,而不是反過來。
  • 我沒有看到</form><input type="submit">
  • 除非您正在上傳文件,否則不需要multipart/form-data編碼。

NULL值不會被轉義,這就是問題(如果相對SQL值真的是NULL,而不是字符串“NULL”)

$query = "INSERT INTO `[my_db_name]`.`members` (`id` ,`first` ,`last` ,`add1` ,`add2` ,`phone_home` ,`phone_cell` ,`phone_dad` ,`phone_mom` ,`email1` ,`email2`)
    VALUES (NULL , '".$first."', '".$last."', '".$add1."', '".$add2."', '".$phone_home."', '".$phone_cell."', '".$phone_dad."', '".$phone_mom."', '".$email1."', '".$email2."')";

在你的字符串中也加一個分號。

你從數據庫得到的錯誤是什么(查看mysql日志)

您打算輸入字符串 'NULL' 或 NULL 值嗎? 如果它是值,那么它不應該用引號引起來。 我認為這可能是問題

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM