PHP FORM INSERT INTO不插入记录

时间:2013-05-16 08:10:18

标签: php sql insert

我正在进行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>

非常感谢您的帮助:)

3 个答案:

答案 0 :(得分: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());
}

答案 1 :(得分: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或{{ 1}}如果出于安全原因可以。

答案 2 :(得分:0)

您错过'$newev的查询错误

$query = "INSERT INTO dj(DJName, FirstName, Email, Website, Picture,    EventNumber)VALUES('$newdj', '$newfn', '$newem', '$newwe', '$newpi', '$newev)";