/    Sign up×
Community /Pin to ProfileBookmark

I don’t understand why or what this error is..

Okay, To start off, my script is running without any errors displaying, but a big problem is It isn’t displaying the alert to people who havn’t been notified, on the row where my ip is, It is “0” for notified, but… it still only displays the else…
Here is the code. Can anyone explain why it isn’t displaying the alert to people with the value 0 in the asked row.

[code=php]<?php
// notify new forum hits that arcade highscores is a member only feature.
include ‘../index/start.php’;
//$db is in start, it is the database connection.
//$IP is also there. it is “$IP = $_SERVER[‘REMOTE_ADDR’];”.
$rowcheck=$db->query(“SELECT * FROM visitors WHERE ip='{$IP}’ LIMIT 1″);
while ($notified=$db->fetch_row($rowcheck)) {
// got the row, now check if it has been notified.
if ($notified[‘asked’] = 0) {
echo ‘<script type=”text/javascript”>
<!–

var answer = confirm (“Highscores in the arcade will only be recorded for members! Do you want to register?”)
if (answer)
alert (“Thats Great, Taking you to register page now..”)
window.location=”http://weconncet.co.cc/forum/index.php?action=register”
else
alert (“Okay. This will not pop up again. If you decided to register, just press register.”)

// –>
</script>’;
$db->query(“UPDATE visitors SET asked=1 WHERE visid={$notified[‘visid’]}”);
}
else if (($notified[‘asked’] > 1) || ($notified[‘asked’] < 0)) {
// Something is wrong, fix it and refresh.
$db->query(“UPDATE visitors SET asked=0 WHERE visid={$notified[‘visid’]}”);
$where = $_SERVER[‘REQUEST_URI’];
echo ‘ERROR! – A unexplainable error has occured with the database, The error has been repaired. Reloading…’;
header( “refresh:1;url=$where” );
}
else {
//if this displays they must already be notified.
echo ‘Already notifed.(Content is displayed purely for debugging.)’;
}
}

?>[/code]

I can’t figure it out.. Help would be appriciated, Also, I’m 17 and still learning, so if you have any improvements for other parts, please share them. Thank you.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@themartyApr 11.2011 — You're using an assignment operator (=) instead of a comparison operator (==).

It's a good practice to write these comparisons the other way around:

[code=php]if (0 == $notified['asked'])[/code]

Also: i wouldn't use ip as the source for your selection. It doesn't work for people that don't have a fixed ip and it also gives problems for different people behind a proxy
Copy linkTweet thisAlerts:
@mikeroqApr 11.2011 — Try this code:
[code=php]
<?php
// notify new forum hits that arcade highscores is a member only feature.
include '../index/start.php';
//$db is in start, it is the database connection.
//$IP is also there. it is "$IP = $_SERVER['REMOTE_ADDR'];".
$rowcheck=$db->query("SELECT * FROM visitors WHERE ip='$IP' LIMIT 1");
while ($notified=$db->fetch_row($rowcheck))
{
// got the row, now check if it has been notified.
$visid = $notified['visid'];
$asked = $notified['asked'];
if ($asked == 0)
{
echo "<script type='text/javascript'>
<!--

var answer = confirm ('Highscores in the arcade will only be recorded for members! Do you want to register?')
if (answer)
alert ('Thats Great, Taking you to register page now..')
window.location='http://weconncet.co.cc/forum/index.php?action=register'
else
alert ('Okay. This will not pop up again. If you decided to register, just press register.')

// -->
</script>";
$db->query("UPDATE visitors SET asked = '1' WHERE visid = '$visid'");
}
else if ($asked != 1 || $asked != 0)
{
// Something is wrong, fix it and refresh.
$db->query("UPDATE visitors SET asked = '0' WHERE visid = '$visid'");
$where = $_SERVER['REQUEST_URI'];
echo "ERROR! - A unexplainable error has occured with the database, The error has been repaired. Reloading...";
header( "refresh:1;url=$where" );
}
else
{
//if this displays they must already be notified.
echo 'Already notifed.(Content is displayed purely for debugging.)';
}
}
?>
[/code]


Best to use cookies for tracking since IP addresses can change. You might want to look into that.
Copy linkTweet thisAlerts:
@mentaljasonauthorApr 11.2011 — 
[code=php]

else if ($asked != 1 || $asked != 0)

[/code]


[/QUOTE]


The porblem with this is that; 0 = Not done, 1 = Done. it can only be one of them, so if asked doesn't equal 1 or asked doesn't = 0 fix it... but ... It will always = 1 or 0.. so this will happen every time... ?
Copy linkTweet thisAlerts:
@mentaljasonauthorApr 11.2011 — Im not sure how to edit posts, So i hope this automaticaly merges them,

instead of
[code=php] else if ($asked != 1 || $asked != 0) [/code] because that will always match one or more because it can only ever be 1 or 0, This is to get rid of ones that somehow break to negitaves or 2 or more, so maybe
[code=php] else if ($asked != 1 & $asked != 0) [/code]
Would work better? because then if it doesn't = 1 and 0 then it is out of the range.. right?

================================================================================

| =================UPDATE=============== |
================================================================================



Tweaked and working.
[code=php]
<?php
// notify new forum hits that arcade highscores is a member only feature.
include '../index/start_globaly.php';
//$db is in start, it is the database connection.
//$IP is also there. it is "$IP = $_SERVER['REMOTE_ADDR'];".
$rowcheck=$db->query("SELECT * FROM visitors WHERE ip='$IP' LIMIT 1");
while ($notified=$db->fetch_row($rowcheck))
{
// got the row, now check if it has been notified.
$visid = $notified['visid'];
$asked = $notified['asked'];
if ($asked == 0)
{
echo '<script type="text/javascript">
var x=window.confirm("Highscores in the arcade will only be recorded for members! Do you want to register?")
if (x)
window.location=("http://weconncet.co.cc/forum/index.php?action=register")
else
window.alert("Okay. This will not pop up again. If you decided to register, just press register.")
</script>';
$db->query("UPDATE visitors SET asked = '1' WHERE visid = '$visid'");
}
else if ($asked != 1 && $asked != 0)
{
// Something is wrong, fix it and refresh.

$db->query("UPDATE visitors SET asked = '0' WHERE visid = '$visid'");
$where = $_SERVER['REQUEST_URI'];
echo "ERROR! - A unexplainable error has occured with the database, The error has been repaired. Reloading...";
header( "refresh:1;url=$where" );
}
else
{
echo '';
}
}
?> [/code]
Copy linkTweet thisAlerts:
@OctoberWindApr 11.2011 — So 0 is "not done", 1 is "done" and anything other than 0 or 1 needs to be reset to 0?

You should take care of your "knowns" first, and deal with the "unknowns" last, as there are generally more "unknowns" than "knowns".

[code=php]
if ($asked == 0)
{
// do the stuff
}
else if ($asked == 1)
{
// it's already been done
}
else
{
// reset the stuff
}
[/code]


Otherwise, you can use a switch, which can sometimes be easier than a multiple-else-if:

The switch statement is similar to a series of IF statements on the same expression. In many occasions, you may want to compare the same variable (or expression) with many different values, and execute a different piece of code depending on which value it equals to. This is exactly what the switch statement is for.
[/quote]

[code=php]
switch($asked)
{
case 0:
// do stuff
break; // be sure to "break" out of the switch-case

case 1:
// stuff has already been done
break;

default:
// default works like the last "else" above, catching anything
// that didn't match a defined case.
break;
}
[/code]
Copy linkTweet thisAlerts:
@mentaljasonauthorApr 11.2011 — Thanks for all your support, It is working and implimented, I needed to do a few changed because i kept getting "Headers already sent error". But anyways, It is working, and i thank you for your support!
Copy linkTweet thisAlerts:
@mikeroqApr 11.2011 — Yes I should have used the and operator, it was pretty early :/
×

Success!

Help @mentaljason spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...