php - 如何使用单个提交按钮插入 en 更新
问题描述
我在下面的代码中遇到了这个问题。我想要做的是在数据库中使用单个提交按钮插入数据,如果我需要更改某些内容,我需要能够在当天仅对数据库执行更新插入的行。但到目前为止,我能够插入数据,但是当我尝试更改某些内容时,它只在 db 中插入另一行。
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();
}
解决方案
为了在我的代码中实现这一点,我只需在表单中添加一个隐藏输入以将$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>';
?>
推荐阅读
- reactjs - localstorage中的令牌交换从react js中的其他用户那里获取数据
- python - 网络抓取的数据有时只有效
- ms-word - 您可以将嵌套的重复内容控件与填充 Microsoft Word 模板操作一起使用吗?
- sql - 显示包含基于其他表中的值的动态值的列
- javascript - 如何获取用户信息,然后将它们保存到对象中?
- pytorch - RuntimeError:CUDA 错误:触发了设备端断言 - BART 模型
- database-design - 无头 CMS 是否适合交互式学习应用程序?
- c++ - 在这种情况下,我应该明确告诉编译器移动字符串吗?
- angular - 数字管道只取小数点前三位
- php - 如果在结帐 Woocommerce 期间未设置名字和姓氏,请选择公司名称