简体   繁体   English

如何检查PHP中是否已单击提交按钮

[英]How to check if a submit button has already been clicked in PHP

I basically have an updatecart.php script which INSERTS data into a database table once the submit button from the HTML page has been clicked, it then goes to display.php where the updated content is shown. 我基本上有一个updatecart.php脚本,一旦单击了HTML页面上的提交按钮,该脚本会将数据插入数据库表中,然后转到display.php,其中显示更新的内容。 The problem I have is that I have a quantity field in my displayed data and if the user clicks the submit button (add stuff to shopping cart) then I want the quantity to increase by how much quantity the user has chosen in in the dropdown menu in the HTML page. 我的问题是我显示的数据中有一个数量字段,如果用户单击提交按钮(向购物车中添加东西),那么我希望数量增加用户在下拉菜单中选择的数量在HTML页面中。 Right now if the user clicks the submit button again nothing happens as it is the same data being added to the database table. 现在,如果用户再次单击“提交”按钮,则不会发生任何事情,因为将相同的数据添加到数据库表中。

Code: 码:

HTML: HTML:

<form name = "AddToCart" action ="../updatecart.php" method='post'>
  Quantity:
  <select name="Quantity">
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  </select> &nbsp;&nbsp;&nbsp;
  <input type ="submit" name ="Add" value="Add To Cart"/>
  </form>

updatecart.php: updatecart.php:

<?php

// Connection to database omitted 

$Quantity = $_POST['Quantity'];

$query = "INSERT INTO Cart VALUES
('1', 'The Fiqh of Dawah', '18.00', 'Books', '$Quantity')";
mysql_query($query);

header("location:displaycart.php");

?>

display.php: display.php:

//Not necessary to show this as it simply uses `echo` to create a table and display the whole table data with `SELECT * FROM Cart`

You need to check if the item already exists in the cart by doing a SELECT first, and then choose between an INSERT (if no rows exists for this item in the cart) or an UPDATE (if this item is already in the cart). 您需要通过先执行SELECT来检查商品是否已存在于购物车中,然后在INSERT (如果购物车中该商品不存在任何行)或UPDATE (如果此商品已在购物车中)之间进行选择。

In MySql, You can also do it by using INSERT ... ON DUPLICATE KEY UPDATE Syntax . 在MySql中,还可以通过使用INSERT ... ON DUPLICATE KEY UPDATE语法来做到这一点。 But You will need to have a primary key to do so. 但是您将需要有一个主键才能这样做。

you can use isset($_POST['Add']) to check the submit button submitted or not 您可以使用isset($_POST['Add'])检查是否提交了提交按钮

like 喜欢

if(isset($_POST['Add']))
{
$Quantity = $_POST['Quantity'];

$query = "INSERT INTO Cart VALUES
('1', 'The Fiqh of Dawah', '18.00', 'Books', '$Quantity')";
mysql_query($query);

header("location:displaycart.php");
}

You can do a check in your updatecart.php at the top to check if Submit has been clicked. 您可以在顶部的updatecart.php中进行检查,以检查是否已单击“提交”。

Something like 就像是

isset($_POST['Add']) {
// insert data into DB
}
else {
// redirect somewhere as they havent clicked Submit button
}

You should also be running your user data through a check/sanitize before you input into your DB. 在输入数据库之前,还应该通过检查/清理来运行用户数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM