问题:我正在尝试在数据库(MySQL)中注册用户的注册表单 该代码应该注册: - 用户名 - 密码 - 名字 - 姓 - 电子邮件
我收到这样的错误:
Notice: Undefined index: fname in C:\xampp\htdocs\Railways\reg.php on line 89
Notice: Undefined index: lname in C:\xampp\htdocs\Railways\reg.php on line 90
Notice: Undefined index: uname1 in C:\xampp\htdocs\Railways\reg.php on line 91
Notice: Undefined index: pswrd1 in C:\xampp\htdocs\Railways\reg.php on line 92
Notice: Undefined index: email in C:\xampp\htdocs\Railways\reg.php on line 93
Fatal error: Call to a member function query() on null in C:\xampp\htdocs\Railways\reg.php on line 98
以下是代码:
<?php
global $conn;
include_once('connect.php');
$first=$_POST['fname'];
$last=$_POST['lname'];
$usernam=$_POST['uname1'];
$passwrd=$_POST['pswrd1'];
$email=$_POST['email'];
$sql=" INSERT INTO users(username,password,firstname,lastname,email) VALUES ('".$usernam."','".$passwrd."','".$first."','".$last."','".$email."')";
if($conn->query($sql))
{
echo "<h1 style='text-align:center'> Registered Succesfully </h1>";
}
else
{
echo "<h1 style='text-align:center' id='text'>Error</h1>";
echo $conn->connect_error;
}
?>
这是HTML:
<form action="reg.php" method="POST">
<label id="text"> Firstname: <br> <br> <input id="input" type="text" name="fname" placeholder="Firstname" id="t3"> </label>
<br> <br> <br>
<label id="text"> Lastname: <br> <br> <input id="input" type="text" name="lname" placeholder="Lastname" id="t4"> </label>
<br> <br> <br>
<label id="text"> Username: <br> <br> <input id="input" type="text" name="uname1" placeholder="Username" id="t1"> </label>
<br> <br> <br>
<label id="text"> Password: <br> <br> <input id="input" type="text" name="pswrd1" placeholder="Password" id="t2"> </label>
<br> <br> <br>
<label id="text"> Email: <br> <br> <input id="input" type="text" name="email" placeholder="Email" id="t1"> </label>
<br> <br> <br>
<input style="background-color:red;padding:7px;margin-left:40px;"type="submit" value="Register" name="submit" id="s1">
</td></tr>
</table>
</div>
</form>
connect.php代码
<?php
$conn_error="could not connect";
$host="localhost";
$user="root";
$pass="";
$db="railways";
$sql =mysqli_connect($host,$user,$pass,$db) or die($conn_error);
echo'conneced';
&GT;
答案 0 :(得分:1)
添加检查是否设置了帖子提交然后运行您的查询,否则如果您直接打开此页面$_POST
变量为空,并且您获得错误的原因是您的$_POST
数组为空。
<?php
include_once('connect.php');
global $conn;
if(isset($_POST['submit'])) // this check
{
$first=$_POST['fname'];
$last=$_POST['lname'];
$usernam=$_POST['uname1'];
$passwrd=$_POST['pswrd1'];
$email=$_POST['email'];
$sql=" INSERT INTO users(username,password,firstname,lastname,email) VALUES ('".$usernam."','".$passwrd."','".$first."','".$last."','".$email."')";
if($conn->query($sql))
{
echo "<h1 style='text-align:center'> Registered Succesfully </h1>";
}
else
{
echo "<h1 style='text-align:center' id='text'>Error</h1>";
echo $conn->connect_error;
}
}
?>
答案 1 :(得分:0)
如果未设置$_POST
,则需要将这些值定义为
这样的事情:
$first=isset($_POST['fname']) ? $_POST['fname'] : "" ;
$last=isset($_POST['lname']) ? $_POST['lname'] : "";
$usernam=isset($_POST['uname1']) ? $_POST['unmae1'] : "";
$passwrd=isset($_POST['pswrd1']) ? $_POST['pswrd1'] : "";
$email=isset($_POST['email']) ? $_POST['email'] : "";
if(isset($_POST['submit'])){
$sql=" INSERT INTO users(username,password,firstname,lastname,email) VALUES ('".$usernam."','".$passwrd."','".$first."','".$last."','".$email."')";
if($conn->query($sql)){
echo "<h1 style='text-align:center'> Registered Succesfully </h1>";
}
else{
echo "<h1 style='text-align:center' id='text'>Error</h1>";
echo $conn->connect_error;}
}