php - 简单的 PHP 表单不提交

标签 php mysql pdo mysqli innodb

页面似乎没有写入数据库。我觉得这可能是数据库错误,而不是表单提交本身。我没有发现任何错误,而且我似乎无法弄清楚为什么数据库不接受我的参数。请看下面的代码和数据库结构。如果您需要更多信息,请告诉我。

    <?php
session_name('shipshapeLogin');
session_set_cookie_params(2*7*24*60*60);
session_start();

define('INCLUDE_CHECK',true);

require('connect.php');
//require_once('business.php');

if(empty($_SESSION['id']))
{
    header("Location: index.php");
    exit;
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Ad Posting</title>
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>
    <script src="https://code.jquery.com/jquery-1.9.1.js"></script>
    <script src="https://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <link rel="stylesheet" type="text/css" href="advertise.css" />
    <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
    <script>
  $(function() {
    $( "#from" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
      onClose: function( selectedDate ) {
        $( "#to" ).datepicker( "option", "minDate", selectedDate );
      }
    });
    $( "#to" ).datepicker({
      defaultDate: "+1w",
      changeMonth: true,
      numberOfMonths: 3,
      onClose: function( selectedDate ) {
        $( "#from" ).datepicker( "option", "maxDate", selectedDate );
      }
    });
  });
  </script>
</head>
<body>
<div class="container">
<form action="" method="post" class="advertiseform">
<div>
<input class="field" type="text" name="business" id="business" disabled="disabled" placeholder="Business Name" value="<?php echo $_SESSION['username']?>"  required="true" />
</div>
<div>
<input class="field" type="text" name="title" id="title" placeholder="Title" value="" size="23" required="true" />
</div>
<div>
<textarea input class="field" type="textarea" name="ad" id="ad" placeholder="Body" required="true">
</textarea>
</div>
<div>
<input type="text" id="from" name="from" placeholder="Valid from:" required="true">
</div>
<div>
<input type="text" id="to" name="to" placeholder="To:" required="true">
</div>
<div>
<button type="button" id="clearbtn">Clear Advertisement</button>
<input type="submit" name="submit" value="Post Advertisement" class="submit" />
</div>
</form>
</div>

<?php


$business_name = $_POST['business'];
$title = $_POST['title'];
$body = $_POST['ad'];
//$valid_date= $_POST['from']." to ". $_POST['to'];
$fromDate = $_POST['from'];
$toDate = $_POST['to'];


$query = "INSERT INTO Businesses_ads ( business_name, title, from, to, body, posted) VALUES ( :business_name, :title, :from, :to, :body, NOW())";


    $query_params = array(

        ':business_name' => $business_name,
        ':title' => $title,
        ':from' => $fromDate,
        ':to' => $toDate,
        ':body' => $body,
    );

        try{

        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);

        }
        catch(PDOException $ex)
        {
            echo $ex;
        }

?>
</body>
</html>

enter image description here

最佳答案

tofromreserved words必须用反引号包裹

改变:

( business_name, title, from, to, body, posted)

到:

( business_name, title, `from`, `to`, body, posted)

关于php - 简单的 PHP 表单不提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22277231/

相关文章:

php - 打印为字符串的 SQL 字段

php - PDO - 查询不返回结果

MySQL - ORDER BY 子句导致 SQL_CALC_FOUND_ROWS 选择不返回数据

java - Mahout 和 MySQL

php - PDO <-> SQL Server exec(在 Laravel 中)返回空数组

php - 如何通过 Eloquent 查询原始数据?

javascript - 将 Excel 列映射到父子 JSON

php - onclick 数据到 JavaScript

php - 扩展布局 .xml 文件在与父主题不同的 <vendor> 子主题中不起作用 : Magento 2. 0.4

mysql - 在 MYSQL 中以重复行中的所有唯一值获取重复结果作为一组