0

我正在处理 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);

?>

添加新数据.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");
?>

输入newdata.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>

非常感谢您的帮助 :)

4

3 回答 3

0

最好使用 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-16T08:16:24.260 回答
0

您缺少报价',导致您无法看到错误,因为您没有进行任何调试。无论如何,你应该改成这个

'$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-16T08:17:28.953 回答
0

你错过'了你的查询$newev给你一个错误

$query = "INSERT INTO dj(DJName, FirstName, Email, Website, Picture,    EventNumber)VALUES('$newdj', '$newfn', '$newem', '$newwe', '$newpi', '$newev)";
于 2013-05-16T08:25:54.017 回答