这里是我想要做的简短描述: 我希望成员输入传感器ID,但如果管理员在表名“availblesensors”中没有将此传感器ID保存在数据库中,则不会将此用户添加到数据库中。
如果坐下来,我会这样做。$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!");
}
我认为如果不能使用satament,因为每次我输入sid(无论是否可用于availblesensors)都是添加用户,但它保存为0而不是我输入的值。
如果需要,这是我的整个代码!
<?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>
答案 0 :(得分:0)
if($count3 = 0){
应该是:
if($count3 == 0){
否则你正在向'count'的'0'赋值,而不是比较它的值,看它是否等于'0'。否则该任务将等于真。
我用来防止这样的事故的一种方法是将常数放在第一位,例如:
if(0 == $count3){
这样一来,如果我错过了其他=符号,PHP将会出错,而不是错误地分配值。
答案 1 :(得分:0)
尝试这种方式:
/* $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)
{
/* ... */
}
将来您可以使用预准备语句或pdo进行SQL操作