我是php的新手。我的课程中有一个网站项目。我被卡住了,不能再动了。需要帮助解决此问题。我试图访问我的代码上的电子邮件和密码,这个错误表明它不是这样的。
我有一个这样的错误:
注意:未定义的索引:第6行C:''examplep''htdocs''CONFIG.PHP中的电子邮件注意:C:''examplep''htdocs''CONFIG.PHP中的未定义索引:密码第7行信息无法写入数据库!
(使用解决
if($_SERVER['REQUEST_METHOD'] == 'POST') {
//...
)但现在任何信息都不会进入数据库
config.php:
<?php
ob_start();
include "dbconnect.php";
$email=$_POST["email"];
$password=$_POST["password"];
$username=$_POST["username"];
$submit = isset($_POST['submit']) ? $_POST['submit'] : null;
$sql="select * from users where email = '".$email."' and password = '".$password."' and username = '".$username."'";
$query=mysql_query($sql);
$counter=mysql_num_rows($query);
if ($counter!=0){
echo "<font size= 3 >This user has been registered before. Please check information.</font>";
}else{
$sql2 = "INSERT INTO users(email, password, username) VALUES ('".$email."','".$password."','".$username."'";
$add = mysql_query($sql2);
if($submit){
echo "<br>You have registered successfully!
Please click. ";
header("Location:regedIndex.html");
}else{
echo "Info could not write on database!";
}
ob_end_flush();
}
?>
register.html:
<html>
<head>
<script type="text/javascript" src="jquery-1.9.1.min.js"></script>
<link type="text/css" rel="stylesheet" href="popupbox.css"/>
<link type="text/css" rel="stylesheet" href="main.css"/>
<script type="text/javascript" src="https://www.google.com/jsapi">
</script>
</head>
<body>
<div id="logo">
<div id="left" style="width: 500px;
height: 100px; margin: 10px 15px 10px 8px;">
<a href="index.php">
<img src="images/logo.png">
</a>
<div id="right">
</div>
</div>
<div id="login">
<div id="triangle"></div>
<h1>Register</h1>
<form action="config.php" method="post">
<input type="email" name="email" placeholder="Email" />
<input type="text" name="username" placeholder="Username" />
<input type="password" name="password" placeholder="Password" />
<input type="submit" value="Register"/>
</form>
</div>
</br></br></br></br></br></br></br></br></br></br></br></br></br></br>
<div id="contacts">
<footer id="footer">
<ul class="icons">
<li>
<a href="https://twitter.com/voteproject"> <img src="images/twitter.svg"></a>
<a href="https://facebook.com/voteproject"> <img src="images/facebook.svg"></a>
<a href="https://github.com/CS320VoteProject/VoteProject"> <img src="images/github.svg"></a>
</li>
</ul>
</footer>
</div>
</body>
</html>
添加if($_SERVER['REQUEST_METHOD'] == 'POST')
以检查表单是否已提交。
例如
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST') {
ob_start();
include "dbconnect.php";
$email=$_POST["email"];
$password=$_POST["password"];
$username=$_POST["username"];
$submit = isset($_POST['submit']) ? $_POST['submit'] : null;
$sql="select * from users where email = '".$email."' and password = '".$password."' and username = '".$username."'";
$query=mysql_query($sql);
$counter=mysql_num_rows($query);
if ($counter!=0){
echo "<font size= 3 >This user has been registered before. Please check information.</font>";
}else{
$sql2 = "INSERT INTO users(email, password, username) VALUES ('".$email."','".$password."','".$username."'";
$add = mysql_query($sql2);
if($submit){
echo "<br>You have registered successfully!
Please click. ";
header("Location:regedIndex.html");
}else{
echo "Info could not write on database!";
}
ob_end_flush();
}
}
?>
错误告诉索引(通常是数组键)email
不存在。这是一行:
$email=$_POST["email"];
它引发此错误的原因是,$_POST
superglobal仅在表单提交后填充,而不是在第一次加载时填充。然而,如果是这种情况,您不会进行检查,因此会出现错误。你可以通过简单地检查你的代码周围的请求方法来避免它:
<?php
ob_start();
include "dbconnect.php";
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email=$_POST["email"];
$password=$_POST["password"];
// etc.
}
(非常重要)的一点是:请停止使用mysql_
函数。自2013年以来,它们一直被弃用,并在本月早些时候刚刚发布的PHP7中完全弃用。这意味着不再支持该代码如果您尝试在最新的服务器上运行此脚本,它将不会运行另请参阅为什么我不应该在PHP中使用mysql_*函数?。