FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


mysql gives the wrong result [solved]





devroom
Code:
<?php
$dbhost = 'localhost';
$dbuser = 'XXXXXXXXXXX';
$dbpass = 'XXXXXXXXXXX';
$dbname = 'XXXXXXXXXXX';
$connection = mysql_connect($dbhost,$dbuser,$dbpass) or die(mysql_error());
$database = mysql_select_db($dbname) or die(mysql_error());

header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Ban</title>
</head>

<body>
<?php
if (isset($_POST['submit'])) {
$query = mysql_query("SELECT count(id) FROM fdv_akobook_banned WHERE ip='$_POST[ipaddress]'") or exit (mysql_error());
$result = mysql_result($query, 0);

if ($result == 1 && ($_POST['delete']=='true')) {
$delete = mysql_query("DELETE FROM fdv_akobook_banned WHERE ip='$_POST[ipaddress]'") or exit (mysql_error());
echo "<p>ip $_POST[ipaddres] is verwijderd</p>\n\n<a href='index.php'>Index</a>"; }

elseif ($result == 1) {
echo "<p>ip $_POST[ipaddres] is al gebanned</p>\n\n<a href='index.php'>Index</a>"; }

elseif ($result<1) {
$insert = mysql_query("INSERT INTO fdv_akobook_banned (`id`,`ip`) VALUES ('','$_POST[ipaddress]')") or exit (mysql_error());
echo "<p>ip $_POST[ipaddres] is in de banlist geplaatst</p>\n\n<a href='index.php'>Index</a>"; }

else {echo 'error';}

} else {

if (isset($_GET['ip'])) { ?>
<form action="index.php" method="post">
<input name="delete" type="hidden" value="true" />
<table>
  <tr>
   <th colspan="2">ARE U SURE U WANT TO DELETE THAT IP?</th>
  </tr>
  <tr>
   <td>IP</td><td><input name="ipaddress" type="text" size="20" maxlength="15" value="<?php echo $_GET['ip'];?>" readonly="true" /></td>
  </tr>
  <tr>
   <td colspan="2"><input name="submit" type="submit" value="Yes" /></td>
  </tr>
</table>
</form>
<p><a href='index.php'>Index</a></p>
<?php } else {
?>
<form action="index.php" method="post">
<table>
  <tr>
   <th colspan="2">BAN IP</th>
  </tr>
  <tr>
   <td>IP</td><td><input name="ipaddress" type="text" size="20" maxlength="15" /></td>
  </tr>
  <tr>
   <td colspan="2"><input name="submit" type="submit" value="Ban" /></td>
  </tr>
</table>
</form>
<hr width="55%" align="left">
<?php

$count = mysql_query("SELECT count(id) FROM fdv_akobook_banned");

$ips = mysql_query("SELECT id,ip FROM fdv_akobook_banned order by id DESC");
$i = 1;

echo '<table border="1" cellspacing="0">
  <tr>
   <th width="10%">ID</th><th width="40%">IP</th>
  </tr>
';

while ($field = mysql_fetch_assoc($ips)) {

$colortr = $i % 2;

if ($colortr==0) {
$tr_style = 'style="background:#EEE"';
} else {
$tr_style = 'style="background:#DDD"';}

echo "  <tr $tr_style>\n";
echo "   <td>$field[id]</td>";
echo "<td><a href='index.php?ip=$field[ip]'>$field[ip]</a></td>\n  </tr>\n";
$i++;
}//end of ips
echo "</table>\n";}
}
?>
</body>
</html>


i want to write a ban script because i get spam in my guestbook Evil or Very Mad
Now is the problem that i cant put any new ip in the list because mysql says $result = 1. but it has to give an 0. does somebody knows where the error is? I dont get any errormessages from mysql.

Please help, because im ******* irritated.
thank you

edit: thx guys it works now.
to other ppl: above u see the corrected script
BlackSkad
$result will always have the value 1, because you have a wrong condition in the first (and third) if-statement.
To check if a variable and a value are equal, you need to use == instead of a single = If you use ( $result = 1 && ...) , you say: give the variable the value 1, which will always be true.
A little lack of attention can screw your whole script Smile

BlackSkad
hexkid
devroom wrote:
Code:
// ...
$doesipexcists = mysql_query("SELECT count(id) FROM fdv_akobook_banned WHERE ip='$_POST[ipaddres]'") or exit (mysql_error());
$result = mysql_num_rows($doesipexcists);
// ...


i want to write a ban script [...]
Now is the problem that i cant put any new ip in the list because mysql says $result = 1. but it has to give an 0. does somebody knows where the error is? I dont get any errormessages from mysql.


The query will always return one row. That row will (apparenly) have the value of 0 (zero) or 1 (one). You need to get the value, not the number of rows returned. Try this
Code:
// ...
$sql = "SELECT count(id) FROM fdv_akobook_banned WHERE ip='$_POST[ipaddres]'";
$resource = mysql_query($sql) or exit(mysql_error());
$result = mysql_result($resource, 0);
// ...



EDIT: And, of course, what BlackSkad said is also very valid!
Related topics
Where can I find a free domain?
[solved] Weird mysql error
[mySQL] Error I cannot Debug
a challenge?
mysql wont delete [solved]
Lightscribe Drive
Date/Time Entries (PHP & MySQL)
U.S. Burns Bibles in Afghanistan
MySQL help
How to keep hair black?
What search engin do you use
What's the most complicated piece of code you've ever wrote
The Sunday Assembly has arrived in Australia
Does Apache/XAMPP result in security vulnerabilty?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.