gpt4 book ai didi

php - 如何用php将两种不同形式的输入存储在mySQL数据库中?

转载 作者:行者123 更新时间:2023-11-29 11:57:31 25 4
gpt4 key购买 nike

我的页面上有两个表单,我想将提交的数据存储在数据库中。

 <?php

$cat = $_POST['cat'];
$id = $_POST['id'];

if(isset($_POST['myForm'])){
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql_cat = "UPDATE animals set cat = ? WHERE id = '$id'";
$q = $pdo->prepare($sql_cat);
$q->execute(array($cat));
Database::disconnect();
header("Location: index.php");

}



$dog = $_POST['dog'];
$id = $_POST['id'];

if(isset($_POST['myForm2'])){
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql_dog = "UPDATE animals set dog = ? WHERE id = '$id'";
$q = $pdo->prepare($sql_dog);
$q->execute(array($dog));
Database::disconnect();
header("Location: index.php");

}


?>



<form action="index.php" method="post" name="myForm">

<div id="modal-cat" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">

<h3>Cat</h3>
</div>
<div class="modal-body">
<p>
<input id="cat" type="text" name="cat" />
<input type="hidden" name="id" value="<?php echo $id ?>"/>
</p>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
</form>


<form action="index.php" method="post" name="myForm2">

<div id="modal-returned" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">

<h3>Dog</h3>
</div>
<div class="modal-body">
<p>
<input id="returned_number" type="text" name="returned_number" />
<input type="hidden" name="id" value="<?php echo $id ?>"/>
</p>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
</div>
</div>
</form>

现在发生的情况是,当我提交 cat 时,cat 的值已正确存储在数据库中。但是,如果我在 dog 之后提交,那么狗也会正确存储在数据库中,但 cat 会被删除。你能帮我解决这个问题吗?

最佳答案

    <?php

$id = $_POST['id'];

if (isset($_POST['myForm'])) {
$cat = $_POST['cat'];

$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql_cat = "UPDATE animals set cat = ? WHERE id = '$id'";
$q = $pdo->prepare($sql_cat);
$q->execute(array($cat));
Database::disconnect();
header("Location: index.php");
}


if (isset($_POST['myForm2'])) {
$dog = $_POST['dog'];
$pdo = Database::connect();
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql_dog = "UPDATE animals set dog = ? WHERE id = '$id'";
$q = $pdo->prepare($sql_dog);
$q->execute(array($dog));
Database::disconnect();
header("Location: index.php");
}
?>


<form action="index.php" method="post" >

<div id="modal-cat" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">

<h3>Cat</h3>
</div>
<div class="modal-body">
<p>
<input id="cat" type="text" name="cat" />
<input type="hidden" name="id" value="<?php echo $id ?>"/>
</p>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" name="myForm">Save</button>
</div>
</div>
</div>
</div>
</form>


<form action="index.php" method="post">

<div id="modal-returned" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">

<h3>Dog</h3>
</div>
<div class="modal-body">
<p>
<input id="returned_number" type="text" name="returned_number" />
<input type="hidden" name="id" value="<?php echo $id ?>"/>
</p>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary" name="myForm2">Save</button>
</div>
</div>
</div>
</div>
</form>

关于php - 如何用php将两种不同形式的输入存储在mySQL数据库中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32967418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com