我有一张来自数据库(controller.php)的表....
<?php
class masterClass
{
public $db_host = "127.0.0.1";
public $db_user = "root";
public $db_pass = "";
public $db_name = "bachelor_bd";
public $db;
public function __construct()
{
try
{
$this->db = new PDO("mysql:host={$this->db_host};dbname={$this->db_name}", $this->db_user, $this->db_pass);
$this->db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
{
$e->getMessage();
}
}
public function bazar_deposit_show_parson($bid)
{
$sql = $this->db->prepare("SELECT * FROM `bachelor_member` WHERE bachelor_id=:bid");
$sql->execute(array(':bid' => $bid));
while ($data = $sql->fetch(PDO::FETCH_ASSOC))
{
echo "<div class='float_left col-10'>
<div class='user-title col-3'><strong>$data[name]</strong></div>
<div class='user-title col-2'>
<input type='text' id='boxthree' name='deposit[]'>
</div>
<input type='hidden' name='mmid[]' value='$data[id]'>
<div class='edit col-1'><button type='submit' name='start_depo' class='btn btn-warning btn-lg btn-block'>Save</button>
</div>
</div>";
}
}} ?>
听到帖子页
<?php
// Header Area Goes Hear
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "../layout/header.php");
if (isset($_POST['submit']))
{
$row_data_id = array();
foreach($_POST['mmid'] as $row=>$mmid)
{
$mmid= $mmid;
$row_data_id[] = $mmid;
}
$row_data_deposit = array();
foreach($_POST['deposit'] as $row=>$deposit)
{
$deposit= $deposit;
$deposit= ($_POST['deposit'][$row]);
$row_data_deposit[] = $deposit;
}
$bachelor->update_bazar_deposit($row_data_id,$row_data_deposit);
if($bachelor)
{
echo "Update Successful"
}
}
//Bachelor Zone Left Sidebar
require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "../layout/sidebar.php");?>
<div class="user-form post-block col-6">
<form action="bazar_deposit.php?b_zone=<?php $_GET['b_zone']; ?>&m_category=<?php $_GET['m_category']; ?>" method='POST'>
<?php if(isset($_GET['b_zone'])) { $bid=$_GET['b_zone']; $bachelor->bazar_deposit_show_parson($bid); } ?>
<button type='submit' name='submit' class='btn btn-warning btn-lg btn-block'>Save Recode</button>
</form>
</div>
<!-- Bottom navigation -->
<?php require_once(dirname(__FILE__) . DIRECTORY_SEPARATOR . "../layout/footer.php"); ?>
然后当我提交表单时,它会命中(controller.php)
public function update_bazar_deposit($mmid,$deposit)
{
$sql = $this->db->prepare("UPDATE `bazar_deposit` SET deposit=:deposit WHERE bachelor_member_id=:mmid");
$sql->bindparam(':mmid', $mmid[]);
$sql->bindparam(':deposit', $deposit[]);
$sql->execute();
return $sql;
}
我是PDO和OOP的新手。好的,当我设置值$ mmid [1]和$ deposit [1]时,数据库取值。但这种形式重复将是几次。取决于用户。用户可以在此表格上重复1,2,3,4或多次。但我无法更新我的数据库所有由mmid引用的行。我只能更新一行。我能做什么。请帮助..... !!!!
答案 0 :(得分:1)
准备好查询后循环遍历数组。
类似的东西:
public function update_bazar_deposit($mmid,$deposit)
{
$sql = $this->db->prepare("UPDATE `bazar_deposit` SET deposit=:deposit WHERE bachelor_member_id=:mmid");
for($i=0;$i<count($mmid);$i++) {
$sql->bindparam(':mmid', $mmid[$i]);
$sql->bindparam(':deposit', $deposit[$i]);
$sql->execute();
}
return $sql;
}