我创建了一个表单,我正在尝试将数据发布到MySQL databasem,但在提交表单后,我收到以下错误..
注意:未定义的索引:第14行的C:\ xampp \ htdocs \ auto \ insert_ac.php中的月份
注意:未定义的索引:第15行的C:\ xampp \ htdocs \ auto \ insert_ac.php中的listner_name
注意:未定义的索引:第16行的C:\ xampp \ htdocs \ auto \ insert_ac.php中的rj_name
注意:未定义的索引:第17行的C:\ xampp \ htdocs \ auto \ insert_ac.php中的通道
注意:未定义的索引:第18行的C:\ xampp \ htdocs \ auto \ insert_ac.php中的年份
注意:未定义的索引:第19行的C:\ xampp \ htdocs \ auto \ insert_ac.php中的国家/地区
当我检查插入数据库的空白行条目
时<?php
$host="localhost"; // Host name
$username="root"; // Mysql username`
$password=""; // Mysql password
$db_name="auto"; // Database name
$tbl_name="song_request"; // Table name
// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// Get values from form
$month=mysql_real_escape_string($_POST['month']);
$listner_name=mysql_real_escape_string($_POST['listner_name']);
$rj_name=mysql_real_escape_string($_POST['rj_name']);
$channel=mysql_real_escape_string($_POST['channel']);
$year=mysql_real_escape_string($_POST['year']);
$country=mysql_real_escape_string($_POST['country']);
// Insert data into mysql
$sql="INSERT INTO song_request (month, listner_name, rj_name, channel, year, country)VALUES('$month', '$listner_name', '$rj_name', '$channel', '$year', '$country')";
$result=mysql_query($sql) or die ('error Updating database');
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='index.html'>Back to main page</a>";
}
else {
echo "ERROR";
}
// close connection
mysql_close();
?>
我的HTML表单代码是
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Radio Mantra Song Request</title>
<link href="css/jquery.autocomplete.css" rel="stylesheet" type="text/css">
<link href="css/main.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="js/jquery-1.5.2.min.js">
</script>
<script type="text/javascript" src="js/jquery.autocomplete.pack.js">
</script>
<script type="text/javascript" src="js/script.js">
</script>
</head>
<body>
<div class="container">
<form name="form" method="post" action="insert_ac.php">
<p><label>Location:</label> <input id="month" type="text" autocomplete="off"></p>
<p><label>Listner Name:</label> <input id="listner_name" type="text" autocomplete=
"off"></p>
<p><label>RJ Name:</label> <input id="rj_name" type="text" autocomplete="off"></p>
<p><label>Channel:</label> <input id="channel" type="text" autocomplete="off"></p>
<p><label>Show Name:</label> <input id="year" type="text" autocomplete="off"></p>
<p><label>Song:</label> <input id="country" type="text" autocomplete="off"></p>
<h2>Radio Mantra</h2>
<table>
<tr>
<td align="center"><input type="submit" name="submit" value="Submit"></td>
</tr>
</table>
</form>
</div>
</body>
</html>
答案 0 :(得分:6)
您需要在表单元素上使用 name 属性才能访问$ _POST中的值。
<input id="month" name="month" type="text" autocomplete="off">
答案 1 :(得分:0)
除了使用SeanNieuwoudt指出的name
属性之外,您还应该在插入数据库之前检查请求方法是否为POST
。如果您不这样做并且有人决定将您的insert_ac.php页面视为GET请求,它将生成与之前相同的通知并插入空白数据。
例如
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
// go ahead and insert
}
答案 2 :(得分:0)
你的表单元素中应该有一个name属性。
您收到此错误可能是因为未填充值。
Notice: Undefined index: month in C:\xampp\htdocs\auto\insert_ac.php on line 14
Notice: Undefined index: listner_name in C:\xampp\htdocs\auto\insert_ac.php on line 15
Notice: Undefined index: rj_name in C:\xampp\htdocs\auto\insert_ac.php on line 16
Notice: Undefined index: channel in C:\xampp\htdocs\auto\insert_ac.php on line 17
Notice: Undefined index: year in C:\xampp\htdocs\auto\insert_ac.php on line 18
Notice: Undefined index: country in C:\xampp\htdocs\auto\insert_ac.php on line 19
如果您有类似$_POST["test"]
的内容,则必须通过执行此操作来检查此索引是否存在:
if(isset($_POST["test"]))
所以就像使用以下
一样if(isset($_POST['submit']))
{
$month=mysql_real_escape_string($_POST['month']);
$listner_name=mysql_real_escape_string($_POST['listner_name']);
$rj_name=mysql_real_escape_string($_POST['rj_name']);
$channel=mysql_real_escape_string($_POST['channel']);
$year=mysql_real_escape_string($_POST['year']);
$country=mysql_real_escape_string($_POST['country']);
// Insert data into mysql
$sql="INSERT INTO song_request (month, listner_name, rj_name, channel, year, country)VALUES('$month', '$listner_name', '$rj_name', '$channel', '$year', '$country')";
$result=mysql_query($sql) or die ('error Updating database');
}