PHP从表单插入

时间:2014-06-30 09:33:47

标签: php mysql insert sql-insert

我正在努力将一些记录从一个从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语句的问题,但我无法弄清楚是什么

任何帮助都将不胜感激。

谢谢,戴夫

1 个答案:

答案 0 :(得分:0)

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