我在下面的代码中遇到了这个问题。我想做的是使用数据库中的单个提交按钮INSERT数据,如果需要更改某些内容,则在插入数据时需要仅在当天执行UPDATE到插入行上的数据库。但是到目前为止,我仍然可以插入数据,但是当我尝试更改某些内容时,它将插入数据库中的另一行。
if(isset($_POST['submit'])) {
$TO_P20O = filter_input(INPUT_POST, 'TO_P20O', FILTER_SANITIZE_STRING);
$TO_P21O = filter_input(INPUT_POST, 'TO_P21O', FILTER_SANITIZE_STRING);
$to_ochtend = $conn->prepare(" SELECT * FROM tablePL WHERE created >= '".$huidige_dag."' order by id ");
$to_ochtend->execute();
$to_ochtend = $to_ochtend->fetch(PDO::FETCH_ASSOC);
$query = "INSERT INTO tablePL (id, TO_P20O, TO_P21O, created ) VALUES (NULL, ?, ?, NOW())";
$stmt = $conn->prepare($query);
$stmt->bindParam(1, $TO_P20O, PDO::PARAM_STR);
$stmt->bindParam(2, $TO_P21O, PDO::PARAM_STR);
$stmt->execute();
echo "<script type='text/javascript'>
alert('Data inserted successfully.');
window.location.replace(\"$domain/succes\");
</script>";
}
else {
$id = filter_input(INPUT_POST, 'id', FILTER_SANITIZE_NUMBER_INT);
$update_query = "UPDATE tablePL SET TO_P20O = ?, TO_P21O = ?, created = NOW() WHERE id = ? AND created >= ? LIMIT 1";
$stmt = $conn->prepare($update_query);
$stmt->bindParam(1, $TO_P20O, PDO::PARAM_STR);
$stmt->bindParam(2, $TO_P21O, PDO::PARAM_STR);
$stmt->bindParam(3, $id, PDO::PARAM_INT);
$stmt->bindParam(4, $huidige_dag, PDO::PARAM_STR);
$stmt->execute();
}
答案 0 :(得分:0)
要在我的代码中完成此操作,我只需在表单中添加一个隐藏输入,即可将$action
变量传递到解析页面。
echo '<form action="parse-content-types.php" method="post">';
echo '<input type="hidden" id="returnto" name="returnto" value="manage-content-types.php">';
echo '<input type="hidden" id="action" name="action" value="add">';
include('form-content-types.php');
echo '<div class="row">';
echo '<div class="col_12 center alpha omega">';
echo '<input type="submit" class="button blue" value="Submit this Content Type">';
echo '</div>';
echo '</div>';
echo '</form>';
这将是parse-content-types.php:
<?php
$level = '../../';
include($level.'core/ini/start.php');
$returnto = $_POST['returnto'];
$action = $_POST['action'];
$content_type_id = $_POST['content_type_id'];
$content_type = $_POST['content_type'];
$content_type_path = $_POST['content_type_path'];
$content_type_desc = $_POST['content_type_desc'];
if($action == 'add'){
$insertq = $db->prepare('INSERT INTO content_types (
content_type,
content_type_path,
content_type_desc
)
VALUES (?,?,?)');
$insertq->execute(array(
$content_type,
$content_type_path,
$content_type_desc
));
$msg = 's|The item: '.$content_type.' has been inserted into the Content Types Database Table.';
}
elseif($action == 'edit'){
$sql = 'update content_types set
content_type_id=:content_type_id,
content_type=:content_type,
content_type_path=:content_type_path,
content_type_desc=:content_type_desc
where content_type_id=:THEID';
$upd=$db->prepare($sql);
$upd->bindParam(':content_type_id',$content_type_id,PDO::PARAM_STR);
$upd->bindParam(':content_type',$content_type,PDO::PARAM_STR);
$upd->bindParam(':content_type_path',$content_type_path,PDO::PARAM_STR);
$upd->bindParam(':content_type_desc',$content_type_desc,PDO::PARAM_STR);
$upd->bindParam(':THEID',$content_type_id,PDO::PARAM_STR);
$upd->execute();
$msg = 's|The item: '.$content_type.' has been updated in the Content Types Database Table.';
}
echo '<Script language="javascript">window.location="'.$returnto.'?msg='.$msg.'"</script>';
?>