douxia6554 2015-06-30 06:52
浏览 58
已采纳

无法将POST内容添加到mysql数据库

<?php

require "config.php";

/*
CREATE TABLE  `addnews` (
 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
 `auther` VARCHAR( 255 ) NOT NULL ,
 `title` VARCHAR( 255 ) NOT NULL ,
 `content` LONGTEXT NOT NULL
) ENGINE = MYISAM ;
*/

$a = $_POST['author'];
$t = $_POST['title'];
$c = $_POST['content'];

if(isset($_POST["add"]) and $_POST["add"] == "news"){
    $insert = mysql_query('INSERT INTO addnews 
    (author,title,content)
    VALUES
    ("$a","$t","$c")') or die("error");
    if (isset($insert )){
        echo "<h3>Done</h3>";
    }
};

echo "
<form action='".$_SERVER['PHP_SELF']."' method='post'>
Author : <input type='text' name='author' /><br>
Title : <input type='text' name='title' /><br>
Content : <textarea name='content'></textarea>
<input type='submit' value='Add news' />
<input type='hidden' name='add' value='news' />
</form>
";


mysql_close($connectdb);
?>

i am getting error from this statment i think

if(isset($_POST["add"]) and $_POST["add"] == "news"){
    $insert = mysql_query('INSERT INTO addnews 
    (author,title,content)
    VALUES
    ("$a","$t","$c")') or die("error happend while trying to add information to database");
    if (isset($insert )){
        echo "<h3>Done</h3>";
    }
};

output is : error happend while trying to add information to database

and no problem with config.php file (the file that connect to database) i am using phpmyadmin

  • 写回答

5条回答 默认 最新

  • douguanyan9928 2015-06-30 07:25
    关注
    1. strings in sql are surrounded by ' (single quote) , not by " (double quote)
    2. strings in php will act two ways
      1. those in ' (single quote) will write literally as tyou typed them ($a stays $a - not $a value)
      2. those in " (double quote) will interpret values inside - so $a will be substituted with $a's value
    3. when failing DB operation - it is usually useful to see what was wrong - use mysql_error for that
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿