通过php将信息添加到数据库时出错

时间:2014-04-08 19:00:19

标签: php mysql database

我正在尝试通过PHP向数据库添加信息。但是,当您提交表单时,它会显示“添加程序时出错”。我查看了代码,似乎无法找到导致错误的原因。有什么建议吗?

这是表单代码:

<form name="Add" id="Add" method="post" action="programadd.php">
  <p>Program Name: 
    <input name="program" type="text" id="program" />
</p>
  <p>Air Date 
    <input name="air date" type="text" id="airdate" />
</p>
  <p>Description 
    <input name="description" type="text" id="description"  />
</p>
<p>Production
    <input name="production" type="text" id="production" />
</p>
<p>Promotions
    <input name="promotion" type="text" id="promotion"  />
</p>
<p>Community
    <input name="community" type="text" id="community" />
</p>
<p>Web
    <input name="web" type="text" id="web"  />
</p>
  <p>
    <input type="submit" name="Submit" value="Submit" />
</p>
</form>

这是将信息添加到数据库中的代码。

<?php require_once("db_connx.php");
$program = $_POST['Program'];
$airdate = $_POST['Air Date'];
$description =  $_POST['Description'];
$production = $_POST['Production'];
$promotion = $_POST['Promotions'];
$community = $_POST['Community'];
$web = $_POST['Web'];


if (mysql_query ("INSERT INTO 'Content Calendar' (Program, 'Air Date', Description, Production, Promotions, Community, 'Web') VALUES ('$program', '$airdate', '$description','$production', '$promotion', '$community', '$web')"))
{ echo "Program successfully added to the database <br />"; 
 }
else
{ echo "Error adding program"; }
require_once("db_connx_close.php");
?>

3 个答案:

答案 0 :(得分:1)

不要在列名上使用单引号,而是使用反引号或让它们保持原样。

正确的方式......

mysql_query ("INSERT INTO `Content Calendar` (`Program`, `Air Date`, `Description`, `Production`, `Promotions`, `Community`, `Web`) VALUES ('$program', '$airdate', '$description','$production', '$promotion', '$community', '$web')")

mysql_*)扩展程序自PHP 5.5.0起已弃用,将来会被删除。相反,应使用MySQLiPDO_MySQL扩展名的准备好的语句来抵御SQL注入攻击!

答案 1 :(得分:1)

以下是问题

INSERT INTO 
'Content Calendar' <--
(
  Program, 
 'Air Date', <--
 Description, 
 Production, 
 Promotions, 
 Community, 
'Web' <--

它们都应该用``as

括起来
INSERT INTO 
    `Content Calendar`
    (
      Program, 
     `Air Date`, 
     Description, 
     Production, 
     Promotions, 
     Community, 
     Web
  

Thumb规则:

     

如果您有一个表名或列名称为&#34;某些名称&#34;意思是空间   在那之间,你必须用后面的勾号“

将它们包围起来      

如果您使用任何保留关键字,也必须附上   在反叛内   http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html

答案 2 :(得分:0)

更改这些行

if (mysql_query ("INSERT INTO 'Content Calendar' (Program, 'Air Date', Description, Production, Promotions, Community, 'Web') VALUES ('$program', '$airdate', '$description','$production', '$promotion', '$community', '$web')"))
{ echo "Program successfully added to the database <br />"; 
 }
else
{ echo "Error adding program"; }

if (mysql_query ("INSERT INTO `Content Calendar` (`Program`, `Air Date`, `Description`, `Production`, `Promotions`, `Community`, `Web`) VALUES ('$program', '$airdate', '$description','$production', '$promotion', '$community', '$web')"))
{ echo "Program successfully added to the database <br />"; 
 }
else
{ echo "Error adding program"; }