views:

40

answers:

2

I am trying to write to my database if a variable is equal to 0. The problem is that it still writes to the database even when the variables equals 1. What is wrong??

echo $new_user;

if ($new_user == 0) {

  //SENT NEW USER WELCOME MESSAGE

  $adminid = '9';
  $welcomemessagetitle = 'Welcome to The site';
  $welcomemessagecontent = 'Hello and welcome';

  $addmessages = "INSERT into `user_messages`(`to_user`,`from_user`,`title`,`content`)
    VALUES ('$userid','$adminid','$welcomemessagetitle','$welcomemessagecontent');";

  $query = mysql_query($addmessages) or die(mysql_error());

  //SET USER AS NOT NEW USER

  $newuservalue = '1';

  $notnewuser = "UPDATE users SET new_user = $newuservalue WHERE id = $userid" ;

  $query2 = mysql_query($notnewuser) or die(mysql_error());

} elseif ($new_user == 1) {};

UPDATE FULL CODE::

<?php 
session_start();
include "../includes/db_connect.php";
///profile/index.php
if($_SESSION['id'])
{
  $username = $_SESSION['username'];
  $userid = $_SESSION['id'];
  //WRITE FIRST TIME LOGIN INFORMATION TO DATABASE
  $sql="SELECT new_user from `users` WHERE `id`= $userid ";
  $res=mysql_query($sql) or die(mysql_error());
  while($row=mysql_fetch_assoc($res)) $new_user = $row['new_user'] ;
  echo $new_user;
  if ($new_user == 0) {
    //SENT NEW USER WELCOME MESSAGE
    $adminid = '9';
    $welcomemessagetitle = 'Welcome to Escorvee';
    $welcomemessagecontent = 'Hello and welcome';
    $addmessages = "INSERT into `user_messages`(`to_user`,`from_user`,`title`,`content`)
      VALUES ('$userid','$adminid','$welcomemessagetitle','$welcomemessagecontent');";

     $query = mysql_query($addmessages) or die(mysql_error());
    //SET USER AS NOT NEW USER
    $newuservalue = '1';
    $notnewuser = "UPDATE users SET new_user = $newuservalue WHERE id = $userid" ;
    $query2 = mysql_query($notnewuser) or die(mysql_error());
  } elseif ($new_user == 1) {};
}
?>
+1  A: 

It the variable $new_user is the value 1 then your code won't be executed so I would guess one of the following applies:

  • $new_user isn't 1.
  • The database is being modified from another location.
  • Your script is being called twice.

To work out which you will have to provide more information in your question.

Mark Byers
hmm.. Thanks for that. When I echo $new_user it shows 1. It also shows 1 in the database.
@user342391: Except that you never enter `$new_user` into the database. You create a new variable `$newuservalue` which you set to 1 and insert it, so as far as I can tell, you never use `$new_user` past the `if` statements... Oh, and `var_dump($new_user)` instead of `echo`, it's more useful since you also get the type...
ircmaxell
check updated code. Will do var:dump
Cheers mark. var_dump showed it was a string not an integer. Problem solved!!! Thanks
A: 

Use === instead of ==.

fredley
`1 == 0` will always yield the same as `1 === 0` irregardless of the type on either side. So using the `===` is pointless in this case (since it appears the bug is likely somewhere else)......
ircmaxell