我的SQL查询出了什么问题?

时间:2022-06-10 00:03:59

I need a quick look at my SQL query. It's already given me a headache. Comparing some advice I have gotten, I could not figure out the proper format for my SQL.

我需要快速查看我的SQL查询。它已经让我头疼。比较我得到的一些建议,我无法弄清楚我的SQL的正确格式。

This is my code:

这是我的代码:

$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());

Thanks in advance!

提前致谢!

EDIT:

编辑:

Currently I have this as the form:

目前我有这个形式:

<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>

This is the insert query:

这是插入查询:

<?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>";

?>

Seems to submit the entry, but not the values... I'm tired of looking at it and not seeing it. I appreciate all the help. Thanks!

似乎提交了条目,但没有提交价值......我厌倦了看它而没有看到它。我感谢所有的帮助。谢谢!

EDIT (AGAIN):

编辑(再次):

As was pointed out by Salman A, the problem was in fact the form, and the fact that each input was identified with an "id", instead of a "name".

正如Salman A所指出的,问题实际上是形式,以及每个输入都被标识为“id”而不是“名称”的事实。

Thanks everyone, I really appreciate all the help! I guess my incorrect SQL formatting is another topic, eh?

谢谢大家,非常感谢所有的帮助!我想我不正确的SQL格式是另一个话题,是吗?

4 个解决方案

#1


2  

Its the form that smells! Change all:

它的形状闻起来!改变所有:

<input id="yadayada">

To:

至:

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

Optional:

可选的:

  • The form tag is misplaced; I suggest that you put the <table> inside the <form>, not the other way round.
  • 表格标签放错了位置;我建议你把 放在 中,而不是反过来。
  • I do not see a </form> and <input type="submit">.
  • 我没有看到 和
  • You do not need a multipart/form-data encoding unless you're uploading files.
  • 除非您正在上传文件,否则不需要多部分/表单数据编码。

#2


2  

NULL should not be quoted.

不应引用NULL。

You can simplify the values list (since this seems to be PHP):

您可以简化值列表(因为这似乎是PHP):

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

#3


0  

NULL values aren't escaped, that's the problem (in case the relative SQL value is really NULL, not a string "NULL")

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."')";

#4


0  

Put a semicolon inside your string as well.

在你的字符串中也加一个分号。

What's the error you're getting from the database (look in mysql logs)

你从数据库得到的错误是什么(查看mysql日志)

#1


2  

Its the form that smells! Change all:

它的形状闻起来!改变所有:

<input id="yadayada">

To:

至:

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

Optional:

可选的:

  • The form tag is misplaced; I suggest that you put the <table> inside the <form>, not the other way round.
  • 表格标签放错了位置;我建议你把 放在 中,而不是反过来。
  • I do not see a </form> and <input type="submit">.
  • 我没有看到 和
  • You do not need a multipart/form-data encoding unless you're uploading files.
  • 除非您正在上传文件,否则不需要多部分/表单数据编码。

#2


2  

NULL should not be quoted.

不应引用NULL。

You can simplify the values list (since this seems to be PHP):

您可以简化值列表(因为这似乎是PHP):

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

#3


0  

NULL values aren't escaped, that's the problem (in case the relative SQL value is really NULL, not a string "NULL")

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."')";

#4


0  

Put a semicolon inside your string as well.

在你的字符串中也加一个分号。

What's the error you're getting from the database (look in mysql logs)

你从数据库得到的错误是什么(查看mysql日志)