Jump to content

[SOLVED] help needed!! IF and Else function..can only execute if statement but not else


Recommended Posts

$timecounter = mysql_query("select timecounter from admin where username='$username' ");

$result=mysql_query("select * from admin where username='$username' ");

 

if(mysql_num_rows($result)!='0'){ // If match.

 

//inserting time into database

$hourdiff = "8";

$timeadjust = ($hourdiff * 3600);

$b = (time() + $timeadjust);

$c = date("g:i:s A D, F jS Y",$b);

 

 

 

"below is it if and else function"

 

 

 

if ($timecounter != "1"){

$update = mysql_query("UPDATE admin SET timein = '$c'  where username='$username' ");

$update = mysql_query("UPDATE admin SET timecounter='1'  where username='$username' ");

}

 

else{

$update = mysql_query("UPDATE admin SET timeout = '$c' where username='$username' ");

$update = mysql_query("UPDATE admin SET timecounter='0'  where username='$username' ");

}

 

The if part was able to execute where timecounter was able to set to 1 and timein set to $c but was not able to execute else part..is there anything wrong which my statements.

You need to close your bery first IF statement...

 

Try this:

 

$timecounter = mysql_query("select timecounter from admin where username='$username' ");
$result=mysql_query("select * from admin where username='$username' ");

if(mysql_num_rows($result)!='0'){ // If match.

//inserting time into database
$hourdiff = "8";
$timeadjust = ($hourdiff * 3600);
$b = (time() + $timeadjust);
$c = date("g:i:s A D, F jS Y",$b);



"below is it if and else function"



if ($timecounter != "1"){
$update = mysql_query("UPDATE admin SET timein = '$c'  where username='$username' ");
$update = mysql_query("UPDATE admin SET timecounter='1'  where username='$username' ");
}

else{
$update = mysql_query("UPDATE admin SET timeout = '$c' where username='$username' ");
$update = mysql_query("UPDATE admin SET timecounter='0'  where username='$username' ");
}
}

Also, $timecounter is a result, not a number.

 

$timecounter_result = mysql_query("select timecounter from admin where username='$username' ");
$timecounter = mysql_fetch_result($timecounter_result, 0, 'timecounter');
print "Timecounter has value $timecounter for user $username<br>";

if ($timecounter != "1"){

$update = mysql_query("UPDATE admin SET timein = '$c'  where username='$username' ");

$update = mysql_query("UPDATE admin SET timecounter='1'  where username='$username' ");

}

 

else{

$update = mysql_query("UPDATE admin SET timeout = '$c' where username='$username' ");

$update = mysql_query("UPDATE admin SET timecounter='0'  where username='$username' ");

}

 

This is completely not smart.They are almost the same except the 1 an 0 part. Why not do it like this:

 

if ( $timecounter != 1) {
   $timecounter = 1;
}
else {
    $timecounter = 0;
}

$update = mysql_query("UPDATE admin SET timeout = '$c' where username='$username' ");
$update = mysql_query("UPDATE admin SET timecounter='$timecounter'  where username='$username' ");

 

Your code wasn't that bad. You just need to understand that if you are manipulating a variable you manipulate it. Dont' manipulate it by a mysql query. Your way uses more code. Mine is shorter and does the same thing.

whoops! I couldn't modify my post so I"m making a new one, sorry:

 

You can make it EVEN shorter:

if ( $timecounter != 1) {
   $timecounter = 1;
}
else {
    $timecounter = 0;
}

$update = mysql_query("UPDATE admin SET timeout = '$c', timecounter='$timecounter'  where username='$username' ");

 

You can update the two variables at the same time.

 

$timecounter = mysql_query("select timecounter from admin where username='$username' ");
$result=mysql_query("select * from admin where username='$username' ");

 

INTO -->

$row = mysql_query("select timecounter from admin where username='$username' ");

 

You must understand that mysql selects "rows" not cells. You then do mysql_fetch_assoc or mysql_fetch_row to get separate data from a row.

 

Hope this helps.

 

$timecounter_result = mysql_query("select timecounter from admin where username='$username' ");

 

 

//Fatal error: Call to undefined function mysql_fetch_result() in C:\xampp\htdocs\chua\login\index.php on line 38

// This line below is line 38 //

 

$timecounter = mysql_fetch_result($timecounter_result, 0, 'timecounter');

 

if($timecounter==0){

$update = mysql_query("UPDATE admin SET timein = '$c',timecounter='1' where username='$username' ");

}

else{

$update2 = mysql_query("UPDATE admin SET timeout = '$c',timecounter='0' where username='$username' ");

}

 

anyone 1 knows what happened?

your query is failing. Try this:

<?php
$sql = "select timecounter from admin where username='$username'";
$timecounter_result = mysql_query($sql) OR die(mysql_error().' Query: '.$sql);

anyway tks for following up with me..a bit still lost

 

$timecounter_result = "select timecounter from admin where username='$username'";

$row = mysql_fetch_array($timecounter_result);////////////////This is line 38////////////////////

$timecounter = $row['timecounter'];

 

 

if($timecounter==0){

$update = mysql_query("UPDATE admin SET timein = '$c',timecounter='1' where username='$username' ");

print "$timecounter2";

}

else{

$update2 = mysql_query("UPDATE admin SET timeout = '$c',timecounter='0' where username='$username' ");

}

 

 

 

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in C:\xampp\htdocs\chua\login\index.php on line 38

 

Warning: session_register() [function.session-register]: Cannot send session cache limiter - headers already sent (output started at C:\xampp\htdocs\chua\login\index.php:38) in C:\xampp\htdocs\chua\login\index.php on line 51

 

Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\htdocs\chua\login\index.php:38) in C:\xampp\htdocs\chua\login\index.php on line 52

 

Warning: Unknown: Your script possibly relies on a session side-effect which existed until PHP 4.2.3. Please be advised that the session extension does not consider global variables as a source of data, unless register_globals is enabled. You can disable this functionality and this warning by setting session.bug_compat_42 or session.bug_compat_warn to off, respectively. in Unknown on line 0

 

hmmm, or any helps for doing it a better way

 

i doing

-time in and time out.

-using a 'time counter' default '0'

when scaning it will checks 'time counter' = 0 or 1

if 'time counter' 0 = scan time in and sets 'time counter' =1

elseif time counter 1=scan 'time out' and sets 'time counter' = 0

 

am i doing correctly..

your query is failing. Try this:

<?php
$sql = "select timecounter from admin where username='$username'";
$timecounter_result = mysql_query($sql) OR die(mysql_error().' Query: '.$sql);

Here is the full thing, from query to result:

 

$sql = "select timecounter from admin where username='$username'";
$timecounter_result = mysql_query($sql) OR die(mysql_error().' Query: '.$sql);
$row = mysql_fetch_array($timecounter_result);
$timecounter = $row['timecounter'];

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.