简体   繁体   English

Mysql不会使用php插入查询

[英]Mysql does't insert query using php

This could sound stupid (maybe because it is) but I'm having so much problem in an insert query, this is the thing, I have in my first page a query that inserts into a table, the problem is that in my page2, I have EXACTLY the same query, but in this page doesn't work, the weirdest thing is that I echoed a message in the true statement of the query and it echoes, meaning that the query supposedly is already made, but is not, because when I check into the DB, is not there, and other thing! 这可能听起来很愚蠢(也许是因为它)但是我在插入查询中遇到了很多问题,这就是问题,我在我的第一页中有一个插入表中的查询,问题是在我的page2中,我有完全相同的查询,但在这个页面不起作用,最奇怪的是我在查询的真实语句中回显了一条消息并且它回响,这意味着该查询据说已经制作,但不是,因为当我检查数据库时,不存在,还有其他的东西! if I want to update the values, it works!, but not in create, I don't know why, but is bothering me a lot!, if someone could help me i really would appreciated, thanks. 如果我想更新价值,它可以工作!,但不是在创造,我不知道为什么,但困扰我很多!,如果有人可以帮助我,我真的很感激,谢谢。

Here is the code: 这是代码:

      if($radio==2){
        echo $_SESSION["in"];
        $sqlb = "SELECT * FROM table WHERE idus='$idus';";
        $resb = mysqli_query($con, $sqlb);
        $res_b = mysqli_fetch_array($resb);

        if( !$res_b)  {//if not exist on table, create
             $sqly="INSERT INTO table (x,y,z)
             VALUES ('$x','$y','$z');";  
            if ($con->query($sqly) === TRUE) {
                $_SESSION["in"]=1;
            } else { 
                echo "Error: " . $sqly . "<br>" . $con->error;
            }

        }else{ //update if exist on table
            $sqly="UPDATE table
            SET y='$y',z='$z'
            WHERE idus='$idus';";  
            if ($con->query($sqly) === TRUE) {
            } else { 
                echo "Error: " . $sqly . "<br>" . $con->error;
            }

        }

        header('Location: page2.php');
    }  

The weird thing of all is that passes through true when doing the query and do nothing, also when pasting the exact same query directly into the DB, it works. 奇怪的是,在执行查询时传递true并且什么都不做,当将完全相同的查询直接粘贴到DB中时,它也可以工作。

It's hard to tell because we can't see all the code, but I think you should replace these lines, using as a reference the update query a few lines below: 这很难说,因为我们看不到所有的代码,但我认为你应该替换这些代码,使用下面几行的更新查询作为参考:

$sqly="INSERT INTO table (x,y,z)
             VALUES (x,y,z);";  

To: 至:

$sqly="INSERT INTO table (x,y,z)
             VALUES ('$x','$y','$z');";  

An explanation: x, y and z are not defined as SQL variables, but in another query below you use them as php variables, so I updated the code putting them as they should. 解释:x,y和z未定义为SQL变量,但在下面的另一个查询中,您将它们用作php变量,因此我更新了代码,将它们放在应有的位置。 Hope it helps. 希望能帮助到你。

Check your column names and properties carefully. 仔细检查列名称和属性。 may be x column is something wrong if update which have y,z is working fine. 可能是x列有问题,如果更新有y,z工作正常。

I did not understand the question correctly but by looking at your code. 我没有正确理解这个问题,但通过查看你的代码。 You could achieve same behaviour by using INSERT...ON DUPLICATE query. 您可以通过使用INSERT...ON DUPLICATE查询来实现相同的行为。 see more here 在这里看到更多

if($radio==2){
        echo $_SESSION["in"];
        $sqlb = 'INSERT INTO table (x,y,z) VALUES ($x, $y, $z) ON DUPLICATE KEY UPDATE x=$x, y=$y, z=$z';
        if ($con->query($sqlb) === TRUE) {
            $_SESSION["in"]=1;
        } else { 
            echo "Error: " . $sqlb . "<br>" . $con->error;
        }

        header('Location: page2.php');
}  

Check the variables $x ,$y, $z variables . 检查变量$ x,$ y,$ z变量。 If its is not empty then your code works otherwise insertion don't work 如果它不是空的那么你的代码工作,否则插入不起作用

我不知道究竟是什么问题,但我通过删除我的所有数据库结构修复它,然后重新做一遍,谢谢你的帮助!

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

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