PHP 从表单插入

标签 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>";
}

?>

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

$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 语句的问题,但我无法弄清楚是什么.

如有任何帮助,我们将不胜感激。

谢谢戴夫

最佳答案

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

关于PHP 从表单插入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24487042/

相关文章:

html - 在 Windows 中安装 OTRS 3.2

php - Bootstrap 表单使用 AJAX 和 PHP 添加新记录

mysql - SQL 返回 double 数据(可能是错误的 SQL 查询)

mysql - Node/Express + MySQL : Inserts not displaying instantly

javascript - Firefox 对待 ajax 的方式与 Chromium 不同?

javascript - 在php中提交表单后未收到值

php - 在 MySQL 中使用 mysqli 插入数据

html - 将简单文本插入到 iframe 中?不是 .htm 文件

php - 第一次点击时提交按钮不起作用

php - 仅在特定页面上从 WooCommerce 购物车中删除特定产品