[mysql]简单的 PHP 表单没有提交

标签: MySQL PHP
发布时间: 2015/6/12 0:56:50
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

页面似乎不会写入数据库。 我有一种感觉,这可能是一个数据库错误,而不是表单提交本身。 没有错误,我发现,我似乎无法弄清楚为什么数据库不接受我的参数。 请参见代码和下面的数据库结构。 请让我知道是否你需要更多的信息。

    <?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

解决方法 1:

tofrom 是必须包裹中的保留的字

变化:

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

自:

( business_name, title, `from`, `to`, body, posted)
赞助商