第16行的未定义变量(名称,电子邮件,浏览器,添加)....错误即将来临.....请帮助我删除错误 这是我的代码
<?php
$con = mysqli_connect('localhost','root','');
if(!$con) {
echo 'not connected to database';
}
if(!mysqli_select_db($con,'student')) {
echo 'database not selected';
}
if (isset($_POST['name'])){$name = $_POST['name'];}
if (isset($_POST['email'])){$email= $_POST['email'];}
if (isset($_POST['address'])){$add = $_POST['address'];}
if (isset($_POST['admission'])){$adm = $_POST['admission'];}
/*line:16*/
$sql = "INSERT INTO student_record (name,email,address,joining_date) VALUES('$name','$email','$add','$adm')";
if ( !mysqli_query($con,$sql)) {
echo 'not inserted';
} else {
echo'inserted';
}
?>
答案 0 :(得分:0)
这是因为您没有在if条件之前初始化变量。如果未找到POST数据,则未设置所有4个变量,则出现错误。 Plz试试这个
if($_POST){
$con = mysqli_connect('localhost','root','');
if(!$con)
{
echo 'not connected to database';
}
if(!mysqli_select_db($con,'student'))
{
echo 'database not selected';
}
$error = true;
$errorMessage = '';
if (empty($_POST['name']) || empty($_POST['email']) || empty($_POST['address']) || empty($_POST['admission'])){
$error = false;
if(empty($_POST['name'])){
$errorMessage = 'Please enter name <br/>';
}
if(empty($_POST['email'])){
$errorMessage .= 'Please enter email <br/>';
}
if(empty($_POST['address'])){
$errorMessage .= 'Please enter address <br/>';
}
if(empty($_POST['admission'])){
$errorMessage .= 'Please enter admission <br/>';
}
}
if($error){
$name = mysql_real_escape_string($_POST['name']);
$email = mysql_real_escape_string($_POST['email']);
$add = mysql_real_escape_string($_POST['address']);
$adm = mysql_real_escape_string($_POST['admission']);
/*line:16*/
$sql = "INSERT INTO student_record (name,email,address,joining_date) VALUES('$name','$email','$add','$adm')";
if ( !mysqli_query($con,$sql))
{
echo 'not inserted';
}else{
echo'inserted';
}
}else{
echo $errorMessage;
}
}
答案 1 :(得分:0)
我认为您没有收到任何帖子数据。只需在执行之前添加if条件
if($_POST){ /*Do stuff check for the post['name'] run your SQL query */
由于帖子未定义,因此POST['name']
未设置,并且它不会进入if块而仍未设置。