我正在处理一项Uni任务,在使用表单将记录插入到MySQL数据库时遇到了问题。我的设置如下。我可以毫无问题地查看数据库中的条目。我是个新手,先说声抱歉:
conninfo.php
<?php
$strServer="localhost";
$strDatabase="djdatabase"; // CHANGE TO YOUR DATABASE NAME HERE
$strUser="root";
$strPwd=""; // Leave blank for WAMPServer
$strDB=mysql_connect($strServer,$strUser,$strPwd)or die("Could not open database");
$database=mysql_select_db("$strDatabase",$strDB);
?>addnewdata.php
<?php include "conninfo.php";
$newdj=$_POST["dj"]; //pick up from form
$newfn=$_POST["fn"];
$newem=$_POST["em"];
$newwe=$_POST["we"];
$newpi=$_POST["pi"];
$newev=$_POST["ev"];
$query = "INSERT INTO dj(DJName, FirstName, Email, Website, Picture, EventNumber)VALUES('$newdj', '$newfn', '$newem', '$newwe', '$newpi', '$newev)";
mysql_query($query);
header("location:showall.php");
?>enternewdata.php
<?php include "conninfo.php";?>
<html>
<head>
</head>
<body>
<form action="addnewdata.php" method="post">
DJ Name:<input type="text" name="dj"><br>
FirstName: <input type="text" name="fn" /><br>
Email: <input type="text" name="em" /><br>
Website: <input type="text" name="we" /><br>
Picture: <input type="text" name="pi" /><br>
EventID: <input type="text" name="ev" /><br>
<br><br>
<button type="submit">Submit</button>
</form>
</body>
</html>非常感谢您的帮助:)
发布于 2013-05-16 16:16:24
最好使用SET命令来插入数据
$query = "INSERT INTO dj SET
DJName=".$newdj.",
FirstName=".$newfn.",
Email=".$newem.",
Website=".$newwe.",
Picture=".$newpi.",
EventNumber=".$newev."";
$save = mysql_query($query);
if($save){
header("location:showall.php");
}else{
die(mysql_error());
}发布于 2013-05-16 16:17:29
您缺少一个报价',该are会导致由于您未执行任何调试而无法看到的错误。不管怎样,你应该改成这样
'$newwe', '$newpi', '$newev')"; //a quote was missing after '$newv我建议您也可以通过添加or die('INVALID QUERY: ' . mysql_error());来调试查询,这样代码看起来就像
mysql_query($query) or die('INVALID QUERY: ' . mysql_error());既然你说这是一个大学考试,我不知道你是否应该使用mysql_*函数(已经被弃用),但我强烈建议你出于安全原因切换到mysqli或PDO。
发布于 2013-05-16 16:25:54
您在$newev上的查询中遗漏了',这会给出一个错误
$query = "INSERT INTO dj(DJName, FirstName, Email, Website, Picture, EventNumber)VALUES('$newdj', '$newfn', '$newem', '$newwe', '$newpi', '$newev)";https://stackoverflow.com/questions/16582166
复制相似问题