我目前正在尝试使用php和Mysql数据库创建一个网站。我尝试使用以下标记语言和PHP代码将我的网站连接到mysql数据库。但是它一直显示以下错误消息:
Notice: Undefined index: fname in C:\xampp\htdocs\signup.php on line 11
Notice: Undefined index: lname in C:\xampp\htdocs\signup.php on line 11
Notice: Undefined index: email in C:\xampp\htdocs\signup.php on line 11
Notice: Undefined index: password in C:\xampp\htdocs\signup.php on line 11
Notice: Undefined index: cpassword in C:\xampp\htdocs\signup.php on line 11
这是我的代码:
<?php
$con=mysqli_connect("localhost","root","p11","daot");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO registration (FirstName, LastName, EmailAddress,Password,ConfirmPassword)
VALUES
('$_POST[fname]','$_POST[lname]','$_POST[email]','$_POST[password]','$_POST[cpassword]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="mastercss.css">
<title>SIGN UP PAGE</title>
</head>
<body>
<?php include 'header.php'; ?>
<div class="leftbar">
</div>
<div class="content">
<h1 class="h1">complete the following form to register</h1>
<fieldset style="width:450px; background:gray;">
<form autocomplete="on" method="post" action="insert.php">
<label for="fname">First Name:</label>
<input type="text" name="fname" required><br><br>
<label for="lname">Last Name:</label>
<input type="text" name="lname"><br><br>
<label for="email">Email:</label>
<input type="email" name="email"><br><br>
<label for="password">Password:</label>
<input type="password" name="password"><br><br>
<label for="cpassword">Confirm Password</label>
<input type="password" name="cpassword"><br><br>
<!--<label for="sex">Sex</label><input type="radio" name="sex" value="female"> Female
<input type="radio" name="sex" value="male">Male<br>
<label for="select">Birthday</label>
<select name="birthday_Month" id="month">
<option value="0" selected="1">Month</option>
<option value="1">January</option>
<option value="2">February</option>
<option value="3">March</option>
</select>
<select name="birthday_day" id="month">
<option value="0" selected="1">Day</option>
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select>
<select name="birthday_year" id="year">
<option value="0" selected="1">year</option>
<option value="2010">2010</option>
<option value="2011">2011</option>
<option value="2012">2012</option>
</select><br><br>-->
<input type="submit" value="SIGN UP" style="width:100: height:100" name="Submit">
</form>
</fieldset>
</div>
<div class="rightbar"><br><br>
<a href="https://www.twitter.com"><img src="tw1.jpg">
<a href="https://www.facebook.com"><img src="fb2.jpg"></a>
</div>
<?php include "footer.php";?>
</body>
</html>
我的代码出了什么问题?
答案 0 :(得分:0)
因为,如果你只按提交按钮,它会发布值,除此之外没有帖子值。这就是你得到未定义索引错误的原因,因此添加isset($_POST['Submit'])
将解决问题
if(isset($_POST['Submit'])){
$sql="INSERT INTO registration (FirstName, LastName, EmailAddress,Password,ConfirmPassword)
VALUES
('".$_POST['fname']."','$_POST[lname]','$_POST[email]','$_POST[password]','$_POST[cpassword]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
}
答案 1 :(得分:0)
第一次加载此页面时,它没有发布数据($ _ POST [fname],$ _ POST [lname] ...)。所以它给你这个通知。
您应该检查您的服务器请求方法 如果它是帖子,那么你有发布数据,你可以运行你的PHP代码。
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$con=mysqli_connect("localhost","root","p11","daot");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO registration (FirstName, LastName, EmailAddress,Password,ConfirmPassword)
VALUES
('$_POST[fname]','$_POST[lname]','$_POST[email]','$_POST[password]','$_POST[cpassword]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
}