在没有日期插入的情况下重复插入表中

时间:2014-03-25 18:55:42

标签: php mysql mysqli insert

我的脚本存在插入问题。首先,它插入每个条目两次,它为donation_date插入00-00-00(在我的表中是DATE类型)。以下是我的代码。

首先是表格:     

<table border=1>
<tr>
<td align=center>Donation Input Form</td>
</tr>
<tr>
<td>
  <table>
  <?
  $querytype = "SELECT type FROM donation_type";
$typeresult = mysqli_query($mysqli, $querytype);

 $queryevent = "SELECT event FROM events";
$eventresult = mysqli_query($mysqli, $queryevent);

  $personid=$_SERVER['QUERY_STRING'];

  $order = "SELECT * FROM persons 
  where personid='$personid'";

  $result = mysqli_query($mysqli,$order);
  $row = mysqli_fetch_array($result);
  ?>
  <form method="post" action="donor_update.php">
  <input type="hidden" name="personid" value="<? echo "$row[personid]"?>"> 
  <tr>ID:<? echo "$row[personid]"?><br /></tr>
  <tr>Donor Name:<? echo "$row[firstname] $row[surname]"?></tr>
    <tr>        
      <td>Donation Type:</td>
      <td>
    <select name='donation_type' class='required'>
    <option VALUE='donation_type'>Donation Type*</option>
    <?php
    while($row = mysqli_fetch_assoc($typeresult)) {
   echo ("<option VALUE=\"{$row['type']}\" " . ($result == $row['type'] ? " selected" : "") . ">{$row['type']}</option>");
      }
       ?>
      </td>
     </select>
      </tr>          
      <tr>        
      <td>Donation Event:</td>
      <td>
      <select name='event' class='required'>
     <option VALUE='event'>Donation Event*</option>
     <?php
     while($row = mysqli_fetch_assoc($eventresult)) {
     echo ("<option VALUE=\"{$row['event']}\" " . ($result == $row['event'] ? " selected" :   "") . ">{$row['event']}</option>");
      }
      ?>
     </select>
      </td>         
     </tr>

         <tr>
       <td>Date:</td>
      <td>
        <input type="date" name="donation_date" size="30" 
      value="">
      </td>
    </tr>

    <tr>
      <td>Donation Amount ($):</td>
      <td>
        <input type="number" name="amount" size="40" 
      value="<? echo "$row[surname]"?>">
      </td>
    </tr>


    <tr>
      <td align="right">
        <input type="submit" 
      name="submit value" value="Submit">
      </td>
      </tr>
      </form>
     </table>
     </td>
    </tr>
    </table>
    </body>
    </html>

然后转到此脚本:

<?
require_once("models/config.php");

$personid =(trim($_POST['personid']));
$amount =(trim($_POST['amount']));
$donation_date = date('Y-m-d', strtotime($_POST['donation_date']));
$donation_type =(trim($_POST['donation_type']));
$event =(trim($_POST['event']));

 $order = "INSERT INTO donations (personid, amount, donation_date, donation_type, event)    VALUES (?, ?, ?, ?, ?)";
 $stmt = mysqli_prepare($mysqli, $order);
 mysqli_stmt_bind_param($stmt, "siiss", $personid, $amount, $donation_date,     $donation_type, $event);
 mysqli_stmt_execute($stmt); 

 $result = mysqli_stmt_execute($stmt);
 if ($result === false) {
 echo "Error entering data! <BR>";
 echo mysqli_error($mysqli);
 } else {
 echo "<b>Donation entered</b><BR>";     
echo "Amount: $$amount <BR>";
echo "Donation Type: $donation_type <BR>";
echo "Event: $event <BR>";
echo "Date: $donation_date <BR>";
}
?>

这里可能会发生什么?

提前致谢。

2 个答案:

答案 0 :(得分:0)

插入2次是因为

mysqli_stmt_execute($stmt); 
$result = mysqli_stmt_execute($stmt);

删除第一个。

现在日期为0000-00-00,您需要将输入日期转换为Y-m-d格式。

你有

$donation_date = date('Y-m-d', strtotime($_POST['donation_date']));

通过回显$ donation_date来缩小调试范围,看看你得到了什么。

在绑定参数中,捐赠日期为i,因为日期被视为字符串,所以s应为{{1}}。

答案 1 :(得分:0)

您正在使用

<input type="date" name="donation_date" size="30" 
      value=""> 

尝试删除值=&#34;&#34;并检查它,它可能会产生问题。