简体   繁体   English

PHP中的SQL插入两行而不是一行

[英]SQL in PHP Inserting Two Rows Instead of One

The following code is inserting two records into my database, but I only want it to insert one. 以下代码将两个记录插入到我的数据库中,但我只希望它插入一个。 Why is it inserting the row twice? 为什么要两次插入行?

<?
if (!$con)
{
    die('Could not connect: ' . mysql_error());
}

$i=1;

while($i<=1)
{
    if (isset($_POST['submit'])) 
    {
        $sql="INSERT INTO customers 
                  (company, salutation, first_name, 
                   last_name, phone, email, fax, 
                   street, town, county, postcode, 
                   type, notes)
              VALUES
                  ('$_POST[company]',
                   '$_POST[salutation]',
                   '$_POST[first_name]',
                   '$_POST[last_name]',
                   '$_POST[phone]',
                   '$_POST[fax]',
                   '$_POST[email]',
                   '$_POST[street]',
                   '$_POST[town]',
                   '$_POST[county]',
                   '$_POST[postcode]',
                   '$_POST[type]',
                   '$_POST[notes]')";

        if (!mysql_query($sql,$con))
        {
            die('Error: ' . mysql_error());
        }
    }

    $i++;
}
?>

At a guess, I'd say it's because it's being executed twice. 大概我猜是因为它被执行了两次。 (Not being smarmy - it's most likely a control flow problem, as there doesn't seem to be anything that would cause the above to insert twice as it stands.) (不要开玩笑-这很可能是控制流问题,因为似乎没有什么可以使上述内容按原样插入两次。)

That said, there's quite a few worrying things in there such as a lack of input escaping, etc. eg: What's the purpose of the $i variable? 就是说,那里有很多令人担忧的事情,例如缺少输入转义等。例如:$ i变量的目的是什么?

Does the CUSTOMERS table have at least a primary key defined to stop duplicates? CUSTOMERS表是否至少定义了一个主键来停止重复项?

Is your PHP webapp smart enough to stop someone from reaching this page if an entry in the CUSTOMERS table for the column combination already exists in the table? 如果CUSTOMERS表中的列组合条目已经存在,则您的PHP Webapp是否足够聪明,可以阻止某人访问此页面?

I can't say from only this. 我不能仅凭此说。 And that code is barely readable indentation is worse than just non-existant and the way you are embedding your post variables in your query doesn't make it any clearer either (and it might actually cause a security vulnerability). 而且该代码几乎不可读,缩进比不存在缩进更糟糕,并且您将post变量嵌入查询中的方式也无法使其变得更清晰(这实际上可能导致安全漏洞)。

Try writing echo $sql; 尝试编写echo $sql; after your $sql = ... monster. $sql = ... You will be able to see if two queries are executed or if it is just one query that inserts two rows, and in the last case it will probably give you quite a hint why it did that. 您将能够查看是否执行了两个查询,或者只是一个查询插入了两行,在最后一种情况下,它可能会给您一个提示,说明为什么这样做。 If it does not help you, post your results of that there, and I or someone else here can have a look at it. 如果对您没有帮助,请在此处发布您的结果,我或这里的其他人可以查看一下。 But note that it will not be easy or pleasant for us with such ungraceful code. 但是请注意,使用这样不完善的代码对我们来说并不容易或令人愉悦。

I have worked through your code and I don't see any reason for the double insert. 我已经完成了您的代码,但没有看到重复插入的任何原因。 Is there something else we should know about? 还有什么我们应该知道的吗? It should not be causing the problem, but why is there a while loop in your code? 它不应该引起问题,但是为什么您的代码中存在while循环? Is there any way this page is being submitted twice? 此页面有两次提交的方式吗? Is there a trigger in place? 是否有触发器?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM