<?php
$conn=mysqli_connect("localhost","root","xxxxxx","test");
$conn->query('set names gb2312');
if(empty($conn))
{
die("数据库连接失败");
}
$UserName=$_POST['username'];
$pwd=$_POST['password'];
$email=$_POST['email'];
$sex=$_POST['sex'];
$time=time();$str="select * from abc where username='".$username."'";
$result1=$conn->query($str);
$row=$result1->fetch_row();
if($row)
{
$temp="已有人注册此名,请重新选择名字!";
echo $temp;
echo"<a href=c.php>返回</a>";
}
else {
$sql="INSERT INTO abc VALUES('$username', '$password','$email','$sex','$time')";
$result=$conn->query($sql);
if($result==true)
{
echo "注册成功,请登陆";
echo "<script>window.location.href='c.php'</script>";
}
else {echo "注册失败".mysql_error();}
}
?>以上网上找的,简化了下的注册代码
mysql下数据库test(1)表wgr,字段:username password email sex time
运行后
提示错误:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\dl\c\insert.php on line 16
谢谢!!!!
$conn=mysqli_connect("localhost","root","xxxxxx","test");
$conn->query('set names gb2312');
if(empty($conn))
{
die("数据库连接失败");
}
$UserName=$_POST['username'];
$pwd=$_POST['password'];
$email=$_POST['email'];
$sex=$_POST['sex'];
$time=time();$str="select * from abc where username='".$username."'";
$result1=$conn->query($str);
$row=$result1->fetch_row();
if($row)
{
$temp="已有人注册此名,请重新选择名字!";
echo $temp;
echo"<a href=c.php>返回</a>";
}
else {
$sql="INSERT INTO abc VALUES('$username', '$password','$email','$sex','$time')";
$result=$conn->query($sql);
if($result==true)
{
echo "注册成功,请登陆";
echo "<script>window.location.href='c.php'</script>";
}
else {echo "注册失败".mysql_error();}
}
?>以上网上找的,简化了下的注册代码
mysql下数据库test(1)表wgr,字段:username password email sex time
运行后
提示错误:
Warning: mysql_fetch_row(): supplied argument is not a valid MySQL result resource in D:\AppServ\www\dl\c\insert.php on line 16
谢谢!!!!
fetch_row()中的代码是什么,有没有链接数据库,更主要的是里面有没有判断$result1啊
<?php
$conn=mysqli_connect("localhost","root","xxxxxx","test");
$conn->query('set names gb2312');
if(empty($conn)){
die("数据库连接失败");
}
$userName=$_POST['username']; //注意变量的大小写,否则下面的$username没有值,查询会失败
$pwd=$_POST['password'];
$email=$_POST['email'];
$sex=$_POST['sex'];
$time=time();$str="select * from abc where username='".$userName."'";
$result1=$conn->query($str);
$row=$result1->fetch_row();
if($row){
$temp="已有人注册此名,请重新选择名字!";
echo $temp;
echo"<a href=c.php>返回</a>";
}
else {
$sql="INSERT INTO abc VALUES('$username', '$password','$email','$sex','$time')";
$result=$conn->query($sql);
if($result==true)
{
echo "注册成功,请登陆";
echo "<script>window.location.href='c.php'</script>";
}
else {echo "注册失败".mysql_error();}
}
?>
<?php
$conn=mysqli_connect("localhost","root","xxxxxx","test");
$conn->query('set names gb2312');
if(empty($conn)){
die("数据库连接失败");
}
$username=$_POST['username']; //注意变量的大小写,否则下面的$username没有值,查询会失败
$pwd=$_POST['password'];
$email=$_POST['email'];
$sex=$_POST['sex'];
$time=time();$str="select * from abc where username='".$username."'";
$result1=$conn->query($str);
$row=$result1->fetch_row();
if($row){
$temp="已有人注册此名,请重新选择名字!";
echo $temp;
echo"<a href=c.php>返回</a>";
}
else {
$sql="INSERT INTO abc VALUES('$username', '$password','$email','$sex','$time')";
$result=$conn->query($sql);
if($result==true)
{
echo "注册成功,请登陆";
echo "<script>window.location.href='c.php'</script>";
}
else {echo "注册失败".mysql_error();}
}
?>
$row=mysql_fetch_row($result1);不行,反而会有这行报错的提示。
改成$row=mysql_fetch_row($result1);也会有这行报错的提示。所以是不是问题不是出在这一行?
mysql下数据库test(1)表名是abc(更正一下原贴),字段:username password email sex time 不知道这样设置对不对?
麻烦各位看一下,谢谢!!
<body>
<form action="crr.php" method="post">
Firstname: <input type="text" name="firstname" />
Lastname: <input type="text" name="lastname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
---------------------
crr.php:<?php
$con = mysql_connect("localhost","root","123456");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_db("test", $con);$sql="INSERT INTO abc (Username, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";mysql_close($con)
?>以上说明数据库应该没有问题.
------------------------------------------------------------------
但是用注册格式的表单就不行:如下:我全部贴出来了,请看一下:
1.注册表单::(有多余的内容)
<html>
<head>
<title>申请帐号</title>
</head>
<form method="post" action="insert.php" name="send" onSubmit="return Check()">
<td width="210" class="p11" valign="bottom"><font color="#FF6699">*</font>为必填项</td>
<p>用户名:
<input type="text" name="username" size="20" class="c3a">
<font color="#FF6699">*</font>
</p>
<p>密码:
<input type="password" name="password" size="20" class="c3a">
<font color="#FF6699">*</font></p>
<p>确认密码:
<input name="cpassword" type="password" id="cpassword">
<font color="#FF6699">*</font></p>
<p>email:<label>
<input name="email" type="text" id="email">
</label>
<font color="#FF6699">*</font></p>
<label></label>
性别:男
<input type="radio" name="sex" value="male" />
<label></label>
<p>女
<input type="radio" name="sex" value="female" />
保密
<input type="radio" name="sex" value="secret" />
</p>
<p>
<input type="submit" value="下一步" >
<input type="reset" value="重来">
</p>
</form>
<script language="javascript">
function Check()// 验证表单数据有效性的函数
{
if (document.send.username.value=="")
{
window.alert('请输入用户名!');
return false;
}
return true;
}
</script>
</body>
</html>
------------------------------------------
2.
insert.php:<?php
$conn=mysqli_connect("localhost","root","123456","test");
$conn->query('set names gb2312');
if(empty($conn))
{
die("数据库连接失败");
}
$username=$_POST['username'];
$str="select * from abc where Username='".$username."'";
$result1=$conn->query($str);
$row=$result1->fetch_row();
if($row)
{
$temp="已有人注册此名,请重新选择名字!";
echo $temp;
echo"<a href=c.php>返回</a>";
}
else {
$sql="INSERT INTO abc VALUES('$username')";
$result=$conn->query($sql);
if($result==true)
{
echo "注册成功,请登陆";
}
else {echo "注册失败".mysql_error();}
}
?>
注册运行后提示注册失败!但是如果用己插入的数据注册,可以检测出会提示:"已有人注册此名,请重新选择名字!"; ----------------------------------------------------------------------
mysql下数据库test(1)表abc,字段:Username Password Email Sex Time
哪位可以认真看下我这里的问题,初学PHP,不懂的太多,万分感谢!!
Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a result object. For other successful queries mysqli_query() will return TRUE. 所以应该改成
if($result=$conn->query($sql)){
echo "注册成功,请登陆";
}else{
echo "注册失败".mysql_error();
}
if($result==$conn->query($sql)){
echo "注册成功,请登陆";
}else{
echo "注册失败".mysql_error();
}很奇怪,可以提求注册成功,请登陆,但是查看数据库里却没有注册到!晕啊,空喜一场
$conn=mysqli_connect("localhost","root","xxxxxx","test");
$conn->query('set names gb2312');
if(empty($conn))
{
die("数据库连接失败");
}
$UserName=$_POST['username'];//这里的变量是$UserName,下面怎么是$username?
$pwd=$_POST['password'];//这里的变量是$pwd,下面怎么是$password?
$email=$_POST['email'];
$sex=$_POST['sex'];
$time=time();$str="select * from abc where username='".$username."'";
$result1=$conn->query($str);
$row=$result1->fetch_row();
if($row)
{
$temp="已有人注册此名,请重新选择名字!";
echo $temp;
echo"<a href=c.php>返回</a>";
}
else {
$sql="INSERT INTO abc VALUES('$username', '$password','$email','$sex','$time')";
$result=$conn->query($sql);
if($result==true)
{
echo "注册成功,请登陆";
echo "<script>window.location.href='c.php'</script>";
}
else {echo "注册失败".mysql_error();}
}
?>
把当刚略的代码和问题贴出来,有知道的希望帮忙一下:
注册时应该有
页面文件省略的部分:
<input name="code" type="text" class="c3a" size="8">
<img src="code.php?" alt="点击刷新" onClick="this.src=this.src+'?'" />
-------------------------------------
insert.php省略部分
else {
if( $_SESSION['sname']==$code)
{
echo $temp="认证码成功";
$sql="INSERT INTO users VALUES('$username', '$password','$email','$sex','$time')";
$result=$conn->query($sql);
if($result==true)
{
$_SESSION['mail']="注册成功,请登陆";
echo "<script>window.location.href='login1.php'</script>";
}
else {echo "注册失败".mysql_error();}
}
else {echo "认证码错误";}
}
请问这里的code.php? 该怎么写?