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