所以我有用户提交的信息通过url传递然后进入表单,如果他们想要删除此记录但是删除查询没有执行,他们点击提交?
这是我的代码:
<?php
$unique_id = $_SESSION['unique_id'];
$date = $_GET['date'];
$steps = $_GET['steps']; } ?>
<h2>Delete Steps</h2>
<div class="form">
<form id="deletesteps" action="deletesteps.php" method="post" autocomplete="off">
<div class="field-wrap">
<label>
Date:
</label>
<input type="date" required autocomplete="off" name="date" value="<?php echo $date ?>" readonly="readonly"/>
</div>
<div class="field-wrap">
<label>
Steps:
</label>
<input type="number" required autocomplete="off" name="steps" value="<?php echo $steps ?>" readonly="readonly"/>
</div>
<div class="field-wrap">
<button type="submit" class="button button-block" name="submit" />Delete?</button>
</div>
</form>
</div>
<?php
if(isset($_POST['submit'])){
$sql = $link->query("DELETE FROM step_count WHERE steps = '$steps' AND date = '$date' AND unique_id = '$unique_id'");
header('Location: nextpage.php'); } ?>
答案 0 :(得分:1)
//first you should know , what date format comes from Php code in $_GET['date'].
// if date format is - 28/02/2018
$date = date("Y-m-d", strtotime(str_replace('/', '-', $_GET['date'])));
答案 1 :(得分:0)
可以尝试下面的内容:
<?php
if(isset($_POST['submit'])){
$date = $_POST['date'];
$steps = $_POST['steps'];
$sql = $link->query("DELETE FROM step_count WHERE steps = '$steps' AND date = '$date' AND unique_id = '$unique_id'");
答案 2 :(得分:0)
尝试以下
<?php
if( isset( $_POST['submit'], $_POST['steps'], $_POST['date'], $_POST['_token'] ) ){
if( $_SESSION['_token'] != $_POST['_token'] ) die( 'Invalid token!' );
$date = strtotime( $_POST['date'] );
$steps = intval( $_POST['steps'] );
$unique_id = intval($_SESSION['unique_id']);
$sql = $link->query("DELETE FROM step_count WHERE steps = '$steps' AND UNIX_TIMESTAMP(date) = '$date' AND unique_id = '$unique_id'");
echo "Deleted:" . $sql? 'Yes':'No';
}else{
if( isset( $_GET['date'], $_GET['steps'] ) ){
$_SESSION['_token'] = uniqid();
?>
<h2>Delete Steps</h2>
<div class="form">
<form id="deletesteps" action="deletesteps.php" method="post" autocomplete="off">
<input type="hidden" name="_token" value="<?php echo $_SESSION['_token'] ?>">
<div class="field-wrap">
<label>
Date:
</label>
<input type="date" required autocomplete="off" name="date" value="<?php htmlentities( $date ) ?>" readonly="readonly"/>
</div>
<div class="field-wrap">
<label>
Steps:
</label>
<input type="number" required autocomplete="off" name="steps" value="<?php echo htmlentities($steps) ?>" readonly="readonly"/>
</div>
<div class="field-wrap">
<button type="submit" class="button button-block" name="submit" />Delete?</button>
</div>
</form>
</div>
<?php
}else{
die('date and steps are required!');
}
}