【问题标题】:PHP Insert from formPHP 从表单插入
【发布时间】:2014-06-30 09:33:46
【问题描述】:

我正在努力将一些记录从 PHP 循环生成的表中插入到我的数据库中。

这是我正在使用的代码:

<?php
$action = isset($_GET['action']) ? $_GET['action'] : "";

if($action=='removed'){
    echo "<div>" . $_GET['name'] . " was removed from cart.</div>";
}

if(isset($_SESSION['cart']) && !empty($_SESSION['cart'])){
    $ids = "";
    foreach($_SESSION['cart'] as $k=>$id){ $ids = $ids . $id . ","; }

    // remove the last comma
    $ids = rtrim($ids, ',');

    $query = "SELECT events.*, agegroup.*, eventtypes.* FROM events  LEFT JOIN agegroup  ON events.AgeGroupID=agegroup.AgeGroupID  LEFT JOIN eventtypes  ON events.EventTypeID=eventtypes.EventTypeID WHERE events.EventID IN ({$ids})";

    $stmt = mysql_query($query);
    $num = mysql_num_rows($stmt);

    if($num>0){

        echo "<table border='0'>";//start table

            // our table heading
            echo "<tr>";
                echo "<th>Product Name</th>";
                echo "<th>Price (GBP)</th>";
                echo "<th>Action</th>";
                  echo "<th>Add Swimmer</th>";
                  echo "<th></th>";
            echo "</tr>";

            //also compute for total price
            $totalPrice = 0;

            while ($row = mysql_fetch_assoc($stmt)){
                extract($row);

                $totalPrice += $EventCost;

                //creating new table row per record

                echo "<tr>";
                echo "<form action='cart.php' method='POST' name='addswimmerform'>";
                    echo "<td>{$EventType}</td>";
                    echo "<td>{$EventCost}</td>";
                    echo "<td>";
                        echo "<a href='removeFromCart.php?id={$EventID}&name={$EventType}'>";
                            echo "Remove";
                        echo "</a>";
                    echo "</td>";
                      echo "<td>Swimmer Drop Down</td>";
                      echo "<td>
                        <input name='EventID' type='text' id='EventID' value='{$EventID}'>
                        <input name='ParentDiaryID' type='text' id='ParentDiaryID' value='{$ParentDiaryID}'>
                        <input name='UserID' type='text' id='UserID' value='{$UserID}'>
                      <input name='addswimmer' type='submit' id='addswimmer' title='Add Swimmer'>
                      <input type='hidden' name='MM_insert' value='addswimmerdetails'>
                      </td>";
                      echo "</form>";
                echo "</tr>";

            }

            echo "<tr>";
                echo "<th>Total Price</th>";
                echo "<th>{$totalPrice}</th>";
                echo "<th></th>";
            echo "</tr>";

        echo "</table>";
        echo "<br /><div><a href='#' class='customButton'>Checkout</a></div>";
    }else{
        echo "<div>No products found in your cart. :(</div>";
    }

}else{
    echo "<div>No products in cart yet.</div>";
}

?>

这是我在页面上方的 INSERT 代码:

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
$editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "addswimmerdetails")) {
$insertSQL = sprintf("INSERT INTO eventregistrations (ParentDiaryID, EventID, SwimmerID, UserID) VALUES (%s, %s, %s, %s)",
                   GetSQLValueString($_POST['$ParentDiaryID'], "int"),
                   GetSQLValueString($_POST['$EventID'], "int"),
                   GetSQLValueString($_POST['$SwimmerID'], "int"),
                   GetSQLValueString($_POST['$UserID'], "int"));

mysql_select_db($database_otters, $otters);
$Result1 = mysql_query($insertSQL, $otters) or die(mysql_error());
}

因此,页面上的表单在页面上的 EventID 和 ParentDiaryID 文本字段中显示了正确的值,但是当我单击按钮插入值时,我收到一条消息,指出 ParentDiaryID 不能为空。

如果我将表单更改为 GET 而不是 POST,我可以看到 URL 中填充的值,所以我知道表单正在传递它们,所以我假设这是我的 INSERT 语句的问题,但我不知道是什么.

任何帮助将不胜感激。

谢谢,戴夫

【问题讨论】:

  • 当您尝试echo $insertSQL 时,是否有您期望的值?
  • 你确定是$_POST['$ParentDiaryID'] 而不是$_POST['ParentDiaryID'] 没有$
  • 天哪,我有时会很愚蠢。是的,我删除了 $ 所以它现在是 $_POST['ParentDiaryID'] 正如建议的那样,它现在插入完全没有问题。谢谢
  • 不要忘记关闭/删除/回答您自己的问题,以免索引混乱:)

标签: php mysql insert sql-insert


【解决方案1】:

基本的菜鸟错误,通过移除 $ 修复。

【讨论】:

    猜你喜欢
    • 2017-04-21
    • 2020-03-25
    • 2014-05-19
    • 2015-08-31
    • 2013-07-30
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 2012-09-04
    相关资源
    最近更新 更多