1

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

elunap
  • 761
  • 3
  • 8
  • 24
  • make sure there's a session in other pages as well – Kevin Feb 19 '16 at 05:05
  • 1
    It's also strange that you use the procedural functions for the SELECT but the OO methods for the INSERT and UPDATE. –  Feb 19 '16 at 05:13

5 Answers5

3

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. Hope it helps.

Alan Berdinelli
  • 300
  • 2
  • 8
0

Check your column names and properties carefully. may be x column is something wrong if update which have y,z is working fine.

Zoedia
  • 245
  • 2
  • 12
  • I checked that so hard.. that I actually copy-pasted the query from the one which is working. – elunap Feb 19 '16 at 05:27
  • copy-pasted the query from another php file? try copy "not working query" and run it on sql server directly with some test data and see what u get. – Zoedia Feb 19 '16 at 05:34
  • Did that too! I use the query that "doesn't work" directly on the DB, and it works! – elunap Feb 19 '16 at 05:42
  • trace $res_b value more carefully... if sql query statement is correct, the error could be at "if" condition. – Zoedia Feb 19 '16 at 07:28
0

I did not understand the question correctly but by looking at your code. You could achieve same behaviour by using INSERT...ON DUPLICATE query. 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');
}  
Community
  • 1
  • 1
0

Check the variables $x ,$y, $z variables . If its is not empty then your code works otherwise insertion don't work

Eldhose Elias
  • 309
  • 5
  • 20
0

I don't know what actually was the problem, but I fix it by deleting all my DB structure and re do it again, thanks for all your help!

elunap
  • 761
  • 3
  • 8
  • 24