dtc88867 2016-03-17 18:53
浏览 15
已采纳

PHP在提交时不会将sql数据发送到数据库?

I am new to PHP and I am making a insert function which will insert the html input into my Sql database but at the moment when I submit nothing happens and no information is entered into the database. Any help on how I can get the in-putted information to submit to the database would be greatly appreciated.

<html>
<head>

</head>
<body>

  <div align = "center">


           <form method = "POST" class="basic-grey">
           <h1><i>Insert</i></h1>
              <label>Title  :<input type = "text" name="title"/></label>
              <label>Content  :<input type = "text" name="content"/></label>
              <label>User  :<input type = "text" name="user"/></label>

              <label><input type = "submit" value = "submit"/></label>
           </form> 

           <div style = "font-size:11px; color:#cc0000; margin-top:10px"><?php echo $error ?></div>

           </div>
</html>
<?php

include_once 'db_config.php';

session_start();
if (!(isset($_SESSION['login_user']) && $_SESSION['login_user'] != '')) {

header ("Location: login2.php");

 }


if(isset($_POST["submit"])) {



$title = $_POST['title'];
$content = $_POST['content'];
$user = $_POST['user'];

$sql = "INSERT INTO 'diary' ('ID', 'TITLE', 'CONTENT', 'USER') 
VALUES (NULL, '$title','$content', '$user',)";

if ($conn->query($sql) === TRUE) {
header("location: results.php");
 } else {
 echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();


?>
  • 写回答

3条回答 默认 最新

  • dongyouzhui1969 2016-03-17 19:02
    关注

    Your script doesn't won't work because you don't have anything named 'submit' for if(isset($_POST["submit"])) { to test. Fix that by naming your submit button:

    <label><input type="submit" name="submit" value="submit"/></label>
    

    Once clicked now the button's name will appear in the $_POST array.


    In addition: session_start() should be right after your opening PHP tag.

    Your script is at risk for SQL Injection Attacks. Learn about prepared statements for PDO and MySQLi and consider using PDO, it's really pretty easy.

    Your error checking will reveal that you have quoted instead of back ticked table and column names and that you have an extra comma. Change your insert query as follows:

    $sql = "INSERT INTO `diary` (`ID`, `TITLE`, `CONTENT`, `USER`) VALUES (NULL, '$title','$content', '$user')";
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图