0

Ok so this is gotten me. Im trying to get a sql insert to work only if 2 varibles do not equal each other. So easy right just compare the 2 varibles and if they arent equal do the code. Well even if the 2 are equal the code runs. I have checked the output of both varibles and they are indeed the same data. So Im looking for a QA to look and see if i missed something. I have tried all versions of not euqal that I know of <> != and !== it just keeps executing with all. Thanks for any suggestions.

if ($callsign != $sameCallSign)
        {
            mysql_query("INSERT INTO narratives SET username = '$username',  Entry = 'Call Sign changed to $callsign $sameCallSign', Admin = '$session->[username]', Date = NOW()")
            or die(mysql_error());
        }
2
Contributors
10
Replies
38
Views
4 Years
Discussion Span
Last Post by BrickZ28
Featured Replies
  • >The $POST gets info from the form page I have. The $check is looking at the array from line 22. My understanding and from the output it is indeed pulling the data from the database. Frommy output I see that both varibles hold the same data. SO that being said … Read More

  • I got ya. I however was able to make it work. What I did was on the form page I passed a hidden input which stores the current data. On the update page I did a $POST for the varibles and compared those so now I have this $cs = … Read More

0

@BrickZ28

Well even if the 2 are equal the code runs. I have checked the output of both varibles and they are indeed the same data.

Instead of this:

$callsign != $sameCallSign

Try this:

$callsign === $sameCallSign

mysql_query("INSERT INTO narratives SET username = '$username', Entry = 'Call Sign changed to $callsign $sameCallSign', Admin = '$session->[username]', Date = NOW()")

Why SET? It should be VALUES.

0

Thank you for your feedback however when I inserted your suggestions it did not work and did not allow for anything to be inputted into the table at all.

As for my use of SET that is just how I have done it. The syntax from my understanding is the same and I have done alot of copy/paste.

0

As for my use of SET that is just how I have done it.

I didn't ask you. Does the query work with the SET and it insert the data to the db?

0

So easy right just compare the 2 varibles and if they arent equal do the code.

You are using an Comparison Operators:

http://www.php.net/manual/en/language.operators.comparison.php

$callsign != $sameCallSign 

Inequality TRUE if $callsign is not equal to $sameCallSign.

OK I'm bit confused.

The code won't run if it's not equal.

It usually equal then the code runs:

$callsign == $sameCallSign      
$callsign === $sameCallSign 

But you want it to run with this !=?

Then you need to create a else if statement.

You only have 1 argument right now you need another argument so it will work.

if ($callsign != $sameCallSign)
{
mysql_query("INSERT INTO narratives SET username = '$username', Entry = 'Call Sign changed to $callsign $sameCallSign', Admin = '$session->[username]', Date = NOW()")
or die(mysql_error());
}
else if
{
//data
}
0

The file this is on has other code after the IF statement. Basically this is from a user edit form and depending on what has changed with the user it may show up on an acomplishment screen. For example if the users callsign changes it will display on a public page. However if only the user persmissions are changed I dont need to update anything else. So the code is saying if the callsign that is currently stored is the same that has come from the previous page dont enter into narratives, hence the !=.

So if the the 2 are not equal (The callsign has changed) then update narratives. In this case the else is not needed since the 2 varibles are != it will just bypass the IF statment and perform the rest of the code on the page. I tried the suggestion of adding a ELSE IF but if i dont change the callsign then the code performs. Chaning the callsign will cause the it to run as well.

From this output

Call Sign changed to A3130 A3130

you can see the 2 are equal however the code runs and by everything i have done it continues to do so regardless of the comparison check. I have even used different varible data and this has teh same result. I am including the entire page because I dont understand why its not working.

<?php
 //includes for session
require 'dbconnect.php';

//if coming from user_edit then get info from that page 
if(isset($_POST['update']))
{
$unit = ($_POST['Unit']);
$username = ($_POST['username']);
$callsign = ($_POST['callsign']);
$rank = ($_POST['rank']);
$email = ($_POST['email']);
$userlevel = ($_POST['userlevel']);
$vatsim = ($_POST['vatsim']);
$home = ($_POST['home']);
$acft = ($_POST['Airframe']);
$majcom = ($_POST['majcom']);
$sq = ($_POST['squadron']);


//query database to see what user member currently has.  If memeber has what what is brought to this page ten doesnt post to narative.  This will allow only new item only new important items to diaplay on narrative page. 
$get = mysql_query("SELECT * FROM users WHERE username = '$username'");
$check = mysql_fetch_assoc($get);
$sameCallSign = $check['Call Sign'];
$sameUsername = $check['username'];
$sameUnit = $check['Unit'];
$sameRank = $check['rank'];
$sameEmail = $check['email'];
$sameLevel = $check['userlevel'];
$sameVat = $check['vatsim'];
$sameHome = $check['home'];
$sameAcft = $check['Airframe'];
$sameMaj = $check['MAJCOM'];
$sameSq = $check['squadron'];


//if not equal then update narratives
if ($callsign != $sameCallSign)
        {
            mysql_query("INSERT INTO narratives SET username = '$username', Entry = 'Call Sign changed to $callsign $sameCallSign', Admin = '$session->[username]', Date = NOW()")
            or die(mysql_error());
        }
else if ($callsign == $sameCallSign)
{
    echo "CS THE SAME";
    }
//updates user squadron id to properly show pilots on viewPilotAll.php
$sql1 = mysql_query("SELECT id FROM units WHERE Squadron = '$unit'"); 
if (!$sql1) {
    die("Query to show fields from table failed");} 
$squa = mysql_fetch_assoc($sql1);
$squ = $squa[id];

//updates teh user info   
mysql_query("UPDATE users SET Unit = '$unit',  `Call Sign`='$callsign', rank = '$rank', Aircraft = '$acft', email = '$email', userlevel = '$userlevel', squadron = '$squ' WHERE username = '$username' ")
or die(mysql_error());


header("Location: ../Login/main.php");}


?>
0

you can see the 2 are equal however the code runs and by everything i have done it continues to do so regardless of the comparison check. I have even used different varible data and this has teh same result. I am including the entire page because I dont understand why its not working.

I'm sure why you used () parentheses you don't really need that.

The issue you are having is this:

$callsign = $_POST['callsign'];
$sameCallSign = $check['Call Sign'];

For

$sameCallSign = $check['Call Sign'];

instead of $check try $_POST

$sameCallSign = $_POST['Call Sign'];

Now your query:

You need to used INSERT to VALUES instead INSERT to SET

From this:

"INSERT INTO narratives SET username = '$username', Entry = 'Call Sign changed to $callsign $sameCallSign', Admin = '$session->[username]', Date = NOW()")

To this:

("INSERT INTO narratives (callsign, Call Sign) VALUES ('$callsign', '$sameCallSign') SELECT * FROM narratives WHERE username = '$username' AND Admin = '$session->[username]' AND Date = NOW()") 

By doing that I think it will now work. The reason is because the query is reading

$callsign != $sameCallSign

as being the same data in the database but by INSERT the data into VALVE then it will read the difference:

if ($callsign != $sameCallSign)
{
mysql_query("INSERT INTO narratives (callsign, Call Sign) VALUES ('$callsign', '$sameCallSign') SELECT * FROM narratives WHERE username = '$username' AND Admin = '$session->[username]' AND Date = NOW()") or die(mysql_error());
}
else if ($callsign == $sameCallSign)
{
echo "CS THE SAME";
}

I hope that make sense.

Edited by LastMitch: grammer

0

I do understand what your saying I think but Im not sure your reccomendation are what Im looking for i dont think. I say this only because my code has extra lines in it to show output for testing.

The $POST gets info from the form page I have. The $check is looking at the array from line 22. My understanding and from the output it is indeed pulling the data from the database. Frommy output I see that both varibles hold the same data. SO that being said and using your code the $post didnt get data as i suspected and the query you reccomended gave me a sytnax error which is no biggie but since the 2 varibles were equal the code still ran. For some reason the code isnt playing nice. I am going to scrap this for now and just find a different work around as this is such a simple statement and 4 days later no progress just isnt time smart.

I thank you for your help it really is appreciated. Now if I may pick your brain, what is the difference from your point on the Set vs VALUE, curious as I have seen both and actually have both on my site just never realized that I was doing it. Thanks again!!

1

The $POST gets info from the form page I have. The $check is looking at the array from line 22. My understanding and from the output it is indeed pulling the data from the database. Frommy output I see that both varibles hold the same data. SO that being said and using your code the $post didnt get data as i suspected and the query you reccomended gave me a sytnax error which is no biggie but since the 2 varibles were equal the code still ran. For some reason the code isnt playing nice. I am going to scrap this for now and just find a different work around as this is such a simple statement and 4 days later no progress just isnt time smart.

You are getting the error because it's inserting to the wrong table. I think it's because of the INSERT INTO narratives it should be INSERT INTO users? 4 days is long time.

Now if I may pick your brain, what is the difference from your point on the Set vs VALUE, curious as I have seen both and actually have both on my site just never realized that I was doing it. Thanks again!!

The difference is this:

SET means get a specific data(column) in the table that you want to be UPDATE

UPDATE table_name
SET column1=value1, column2=value2
WHERE column=value 

VALUES means confirm the data(column) in the table to be INSERT

INSERT INTO table_name (column1, column2)
VALUES (value1, value2) 

So I hope I explain it clearly. That's why I wrote the query base on this:

"INSERT INTO narratives SET username = '$username', Entry = 'Call Sign changed to $callsign $sameCallSign', Admin = '$session->[username]', Date = NOW()")

So you want everything to be updated to be in specfic column(s)?

Edited by LastMitch: grammer

1

I got ya. I however was able to make it work. What I did was on the form page I passed a hidden input which stores the current data. On the update page I did a $POST for the varibles and compared those so now I have this

$cs = ($_POST['cs']);


//if not equal then update narratives
if ($callsign != $cs)
        {
            mysql_query("INSERT INTO narratives SET username = '$username', Entry = 'Call Sign changed to $callsign from $cs', Admin = '$admin', Date = NOW()") or die(mysql_error());
        }

This worked as I need it too. Not sure what the deal is but it just didnt like that. Now I hope that it works with the other varibles I need to do it with but I dont see why it shouldnt. Thanks for your time I really do appreciate it!!!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.