here is short description of what i want to do: i want the member enter the sensor id, but if this sensor id is not saved in the database by the admin in table name "availblesensors" it will not add this user to the database.
i worte this if satatment
$sid=strip_tags($_POST['sid']);
$sidcheck=mysql_query("SELECT sid FROM availblesensors WHERE sid='$sid'");
$count3=mysql_num_rows($sidcheck);
if($count3 = 0){
die("This sensor is not availble!");
}
i think if satament is not worked, because each time i entered sid (either if it is availble in availblesensors or not) it is add the user, but it is saved 0 not the value that i was entered.
and here is my whole code if needed!
<?php
session_start();?>
<?php
require("noCache.php");
?>
<html>
<!DOCTYPE html>
<html lang="en">
<head>
<title>Admin Page</title>
<meta charset="utf-8">
<link rel="stylesheet" href="css/reset.css" type="text/css" media="all">
<link rel="stylesheet" href="css/layout.css" type="text/css" media="all">
<link rel="stylesheet" href="css/style.css" type="text/css" media="all">
<script type="text/javascript" src="js/jquery-1.4.2.js" ></script>
<script type="text/javascript" src="js/cufon-yui.js"></script>
<script type="text/javascript" src="js/cufon-replace.js"></script>
<script type="text/javascript" src="js/Myriad_Pro_400.font.js"></script>
<script type="text/javascript" src="js/Myriad_Pro_700.font.js"></script>
<script type="text/javascript" src="js/Myriad_Pro_600.font.js"></script>
</head>
<body>
<div class="main">
<header>
<div class="wrapper">
<h1><a href="index.php" id="logo"> Biz</a></h1>
</div>
<nav>
<ul id="menu">
<li class="alpha"><a href="index.php"><span><span>Home</span></span></a></li>
<li><a href="About.html"><span><span>About</span></span> </a></li>
<li><a href="Projects.html"><span><span>Projects</span></span></a></li>
<li><a href="Contacts.php"><span><span>Contacts</span></span></a></li>
<li class="omega"><a href="Services.html"><span><span>Services</span></span></a></li>
</ul>
</nav>
</br></br>
</header>
<head>
<!-- CSS Stylesheet -->
<style type="text/css">
html{
}
body{
text-align:center;
}
</style>
</head>
<?php
if($_SESSION['username']){
include ("connection.php");
$id=$_SESSION['uid'];
}?>
<?php
$submit= $_POST['submit'];
$name= strip_tags($_POST['name']);
$dusername= strtolower(strip_tags($_POST['dusername']));
$password= strip_tags($_POST['password']);
$repassword= strip_tags($_POST['repassword']);
$date=date("y-m-d");
$sid=strip_tags($_POST['sid']);
$phone=strip_tags($_POST['phone']);
if($submit){
//open data base
$connet=mysql_connect("localhost","root","hahaha1");
mysql_select_db("senior");
///////////////////////////////////////////////////////////////
// my problem is i want to check the entered sensor by id if was availble or not, by checking if the admin added it into the database or not
$sidcheck=mysql_query("SELECT sid FROM availblesensors WHERE sid='$sid'");
$count3=mysql_num_rows($sidcheck);
if($count3 = 0){
die("This sensor is not availble!");
}
///////////////////////////////////////////////////////////////////////
$namecheck=mysql_query("SELECT dusername FROM sensorusers WHERE dusername='$dusername'");
$count=mysql_num_rows($namecheck);
if($count!=0){
die("username already exist!");
}
$emailcheck=mysql_query("SELECT sid FROM sensorusers WHERE sid='$sid'");
$count2=mysql_num_rows($emailcheck);
if($count2!=0){
die("This sensor already used!");
}
if($name && $dusername && $password && $repassword && $phone && $sid ){
if($password==$repassword){
if (strlen($name)>25 || strlen($dusername)>25){
echo"<h3><font color='red'>Length of username or name is Too Long!</font></h3>";
}
elseif (strlen($password)>12 || strlen($repassword)<6){
echo"<h3><font color='red'>Password must be between 6 and 12 characters!</font></h3>";}
elseif(ctype_alpha($name)=== false)
{
echo"<h3><font color='red'>Name must not contains numbers!</font></h3>";
}
else{
$password=md5($password);
$repassword=md5($repassword);
$queryreg=mysql_query("
INSERT INTO sensorusers VALUES('','$name','$dusername','$password','$date','$sid','$phone','$id')
");
// success
die("You have been Create new sensor user successfully !! <a href='member.php'>click here to return to Your page</a>");
}
}
else{ echo"<h3><font color='red'>The two passwords not match!</font color='red'></h3>"; }
}
else echo"<h3><font color='red'>Please fill in <b>all</b> feilds!</font></h3>";
}
?>
<br>
<div align='left'>
<b>
<center> Create new driver: </center> </br></br></br>
<font color='blue'> Please fill all the information to create a new driver that you want to monitor: </font></br></br></br>
<form name='f' action='createsensoruser.php' method='POST' >
<ul>
<li id="foli9" class=" ">
<label class="desc" id="title9" for="name">
Name
</label>
<div>
<input id="Field9" name="name" value="" type="text" class="" value="" maxlength="255" tabindex="12" size="25" />
</br>
</div>
</li>
<li id="foli9" class=" ">
<label class="desc" id="title9" for="dusername">
Username
</label>
<div>
<input id="Field9" name="dusername" value="" type="text" class="" value="" maxlength="255" tabindex="12" size="25" />
</div>
</li>
<li id="foli55" class=" ">
<label class="desc" id="title55" for="pass">
Password
</label>
<div>
<input id="Field55" name="password" type="password" class="" value="" maxlength="12" tabindex="80" size="25" />
</div>
</li>
<li id="foli70" class=" ">
<label class="desc" id="title70" for="repass">
Re-Enter your password
</label>
<div>
<input id="Field70" name="repassword" type="password" class="" value="" maxlength="12" tabindex="81" size="25" />
</div>
</li>
<li id="foli9" class=" ">
<label class="desc" id="title9" for="sid">
Sensor id
</label>
<div>
<input id="Field9" name="sid" value="" type="text" class="" value="" maxlength="255" tabindex="12" size="25" />
</div>
</li>
<li id="foli9" class=" ">
<label class="desc" id="title9" for="phone">
Phone number
</label>
<div>
<input id="Field9" name="phone" value="" type="text" class="" value="" maxlength="255" tabindex="12" size="25" />
</div>
</li>
</br>
<input name="submit" type="submit" value="submit" />
<input type="reset" value="clear" />
</ul>
</form>
</b>
</div>
if($count3 = 0){
should be:
if($count3 == 0){
Otherwise you are assinging a value to $count3 of '0' opposed to comparing it's value to see if it's equal to '0'. That assignment will otherwise equal to true.
One method I use to prevent accidents like this is to put the constant first such as:
if(0 == $count3){
That way if I miss the other = sign PHP will error, rather than assign the value erroneously.
Try this way:
/* $sid = (int)$_POST['sid']; uncomment this only if $sid is integer */
$sidcheck = mysql_query("SELECT sid FROM availblesensors WHERE sid='$sid'");
$result = mysql_query($sidcheck);
$isSIDinTheDataBase = mysql_num_rows($resultado);
if($isSIDinTheDataBase==0)
{
/* ... */
}
In the future you can use prepared statements or pdo for our sql operations