2015-11-11 64 views
1

我有一个从数据库(Controller.php这样)来到一个表....更新多行到一个MySQL表使用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引用。我只能更新一行。我能做什么。帮助请..... .....

回答

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;  
} 
+0

会发生什么是空?它仍然会覆盖以前的值,是吗? –

+0

不,因为如果它们都是'null',那么查询就会出错。当它循环遍历数组时,如果当前为null,我不相信'bindparam'会保持以前的值。 – Jon

+0

很多很多谢谢jon其工作很好.... !!!! –