我一直在努力向我的sql数据库添加数据我尝试了很多不同的方法,但仍然遇到了同样的错误。
注意:未定义的索引:第63行的N:\ ftp \ compc \ d11os \ Project \ addModule.php中的ModuleId
注意:未定义的索引:第64行的N:\ ftp \ compc \ d11os \ Project \ addModule.php中的标题
注意:未定义的索引:第65行的N:\ ftp \ compc \ d11os \ Project \ addModule.php中的CreditLevel
注意:未定义的索引:第66行的N:\ ftp \ compc \ d11os \ Project \ addModule.php中的CreditPoints
注意:未定义的索引:第67行的N:\ ftp \ compc \ d11os \ Project \ addModule.php中的状态
我在做什么呢?我知道它可能很小但是我看不到它并真的需要帮助吗?
这是我正在使用的代码
<?php
$con=mysqli_connect("localhost","ROOT","ROOT","ROOTdb");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$ModuleId = mysqli_real_escape_string($con, $_POST['ModuleId']);
$Title = mysqli_real_escape_string($con, $_POST['Title']);
$CreditLevel = mysqli_real_escape_string($con, $_POST['CreditLevel']);
$CreditPoints = mysqli_real_escape_string($con, $_POST['CreditPoints']);
$Status = mysqli_real_escape_string($con, $_POST['Status']);
$Award = mysqli_real_escape_string($con, $_POST['Award']);
$sql="INSERT INTO module(ModuleId, Title, CreditLevel, CreditPoints, Status, Award)
VALUES ('$ModuleId', '$Title', '$CreditLevel' ,'$CreditPoints', '$Status', '$Award')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
使用
的Html代码<html>
<head>
<link rel="stylesheet" type="text/css" href="style.css">
<img src="logo.jpg" alt="University of Ulster Logo" width="332" height="132">
<h1 style="font-family:Bell MT;color:blue;font-size:28px;">Add a New Module.</h1>
</head>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0">
<tr><td>Module:</td><td>
<input type="text" name="name" maxlength="60" required>
</td></tr>
<tr><td>Title:</td><td>
<input type="text" name="telephoneNo" maxlength="60" required>
</td></tr>
<tr><td>CreditLevel:</td><td>
<input type="text" name="email" maxlength="60" required>
</td></tr>
<tr><td>CreditPoints:</td><td>
<input type="text" name="course" maxlength="60" required>
</td></tr>
<tr><td>Status:</td><td>
<input type="text" name="Staus" maxlength="100" required>
</td></tr>
<tr><td>Award:</td><td>
<input type="text" name="Award" maxlength="100" required>
</td></tr>
<td colspan="2" style="text-align:center">
<input type="submit" name='submit' value='Submit'>
</form>
答案 0 :(得分:0)
问题出现了,因为您使用的变量未在$_POST
数组中定义。
尝试类似
的内容if (isset($_POST['ModuleId']))
{
$ModuleId = mysqli_real_escape_string($con, $_POST['ModuleId']);
} else {
$ModuleId = -1; // Default value or Error flag
echo "Module Id is not specified.";
}
以及其他变量等等。
在插入之前检查所有值是否存在可能是个好主意。例如,
if ($ModuleId != -1) {
$sql="INSERT INTO module(ModuleId, Title, CreditLevel, CreditPoints, Status, Award)
VALUES ('$ModuleId', '$Title', '$CreditLevel' ,'$CreditPoints', '$Status', '$Award')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
}
mysqli_close($con);
//编辑
您的问题似乎源于不检查页面是否已回复。首次加载页面时,它会尝试插入数据 - 但由于您尚未提交表单,因此无需插入数据。
用简单的支票包裹你的PHP代码
<?php
if (isset($_POST['submit']))
{
$con=mysqli_connect("localhost","ROOT","ROOT","ROOTdb");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// escape variables for security
$ModuleId = mysqli_real_escape_string($con, $_POST['ModuleId']);
$Title = mysqli_real_escape_string($con, $_POST['Title']);
$CreditLevel = mysqli_real_escape_string($con, $_POST['CreditLevel']);
$CreditPoints = mysqli_real_escape_string($con, $_POST['CreditPoints']);
$Status = mysqli_real_escape_string($con, $_POST['Status']);
$Award = mysqli_real_escape_string($con, $_POST['Award']);
$sql="INSERT INTO module(ModuleId, Title, CreditLevel, CreditPoints, Status, Award)
VALUES ('$ModuleId', '$Title', '$CreditLevel' ,'$CreditPoints', '$Status', '$Award')";
if (!mysqli_query($con,$sql)) {
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
}
?>