这是PHP中的VALUES中的错误。它说每个值都是一个未定义的索引。 我已经尝试了很长时间了,但我无法弄清楚问题是什么。是在我的数据库中还是在代码中? 请帮帮我:)。
<head>
<title>Filmer</title>
</head>
<body>
<h1>Legg til filmer</h1>
<form action="leggtil.php" method="post">
<p>
<label>Tittel: </title>
<input type="text" name="tittel" placeholder="Tittel">
</p>
<p>
<label>Utgitt: </label>
<input type="text" name="utgitt" placeholder="Utgitt">
</p>
<p>
<label>Sjanger</label>
<input type="text" name="sjanger" placeholder="Sjanger">
</p>
<p>
<label>Lengde: </label>
<input type="text" name="lengde" placeholder="Lengde">
</p>
<p>
<label>Alder: </label>
<input type="text" name="alder" placeholder="Aldersgrense">
</p>
<p>
<label>Format: </label>
<input type="text" name="format" placeholder="Format">
</p>
<p>
<input type="submit" value="Legg til">
</p>
</form>
<?php
$host="localhost";
$username="root";
$password="";
$database="filmer";
$table="filmer";
mysql_connect("$host", "$username", "$password") or die(mysql_error());
echo "conncected";
mysql_select_db("$database") or die(mysql_error());
echo "database found";
mysql_query("INSERT INTO '$table'(videoid, tittel, utgitt, sjanger, lengde, alder, format)
VALUES ('', '$_POST[tittel]'. '$_POST[utgitt]', '$_POST[sjanger]', '$_POST[lengde]', '$_POST[alder]', $_POST[format])");
?>
</body>
答案 0 :(得分:1)
你写$ _POST变量的方式是错误的:
$_POST[tittel]
应为$_POST["tittel"]
答案 1 :(得分:1)
你的php不正确,小心引用:
mysql_connect("$host", "$username", "$password") or die(mysql_error());
echo "conncected";
mysql_select_db("$database") or die(mysql_error());
echo "database found";
// remove the quotes around table names. they don't want quotes there
mysql_query("INSERT INTO $table (videoid, tittel, utgitt, sjanger, lengde, alder, format)
VALUES ('','" . $_POST['tittel'] . "','" . $_POST['utgitt'] "','" . $_POST['sjanger'] . "','" . $_POST['lengde'] . "','" . $_POST['alder'] . "','" . $_POST['format'] . "')");
?>// when you refer to an array by string key, you must single or double quote it like $_POST['blah']. since you already have sinqgle quotes around your values (remember we are writing sql) you need to concatenate your variables
答案 2 :(得分:0)
无论表单是否已完成,您的PHP都会每次都执行。表单第一次没有完成,所以$ _POST数组将为空。
您需要使PHP执行成为条件。尝试像这样包装:
If (isset($_POST['submit'])) {
// your code here
}
您还需要为submit
按钮命名:
<input type="submit" value="Legg til" name="submit">
您的查询语法错误。您应该使用反引号来包含标识符,而不是单引号。试试这个:
$myQuery = "INSERT INTO `$table` ( tittel, utgitt, sjanger, lengde, alder, format)
VALUES ('".$_POST['tittel']."', '".$_POST['utgitt']."', '".$_POST['sjanger']."', '".$_POST['lengde']."', '".$_POST['alder']."', '".$_POST[format]."')";
我已从查询中删除videoid
,因为它应设置为自动递增;而且我用一个逗号应该替换掉一个句号;我用单引号重写了数组引用。
最后,您应该检查查询的返回状态:
$result = mysql_query($myQuery) or die(mysql_error());
附注:
mysql_*()
- 不推荐使用这些功能。请改用mysqli_*()
。 mysql_real_escape_string()
转义用户输入,或者考虑转到准备好的语句。