【问题标题】:Duplicate insert into table without date insert在没有日期插入的情况下重复插入到表中
【发布时间】:2014-05-03 20:11:55
【问题描述】:

我在使用此脚本时遇到了插入问题。首先,它将每个条目插入两次,并为donation_date 插入00-00-00(这是我表中的日期类型)。下面是我的代码。

首先是表格:

<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>";
}
?>

这里可能发生了什么?

提前致谢。

【问题讨论】:

    标签: php mysql mysqli insert


    【解决方案1】:

    插入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,因为日期被视为字符串。

    【讨论】:

    • 什么意思?改为$donation_date = date('Y-m-d', ($_POST['donation_date'])); ?
    • 我的意思是你有捐赠日期作为文本框,人们可以添加任何格式的日期,所以 strtotime() 可能无法将所有格式输入日期转换为 Ymd,所以要求回显该值并查看转换后你会得到什么。
    • 在我的表单中,我将它作为 HTML5 日期选择框。就我的代码而言,它与 2014-03-24 的日期相呼应(我的随机选择)。另外,“i”是日期的正确参数开关吗?
    • 啊发现您在绑定参数中有“i”作为捐赠日期。将其设为“s”,因为日期被视为字符串!
    • 谢谢!就是这样。对于那些学习,永远不要在绑定参数中使用 i 作为日期。
    【解决方案2】:

    你正在使用

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

    尝试删除 value="" 并检查它,它可能会产生问题。

    【讨论】:

      猜你喜欢
      • 2023-03-31
      • 2014-08-26
      • 1970-01-01
      • 2016-02-19
      • 2011-10-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-28
      相关资源
      最近更新 更多