1

I Have a Table Which came from data base (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>";
    }
}} ?>

And Hear Is Post Page

<?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"); ?>

And then when i submit my form it hits (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;    
}

I am new in PDO And OOP. OK, when i set the value $mmid[1] and $deposit[1] the database take the value. but this form repeat will be several time.its depend on user. user can repeat 1,2,3,4 or many times on this form filed. But i can't update my database all rows those Referred by mmid. i can update only one row. what can i do. Help pls.....!!!!

1 Answer 1

2

Loop through the array after you have prepared the query.

Something like:

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;    
}
Sign up to request clarification or add additional context in comments.

2 Comments

What would happen if the value being bound was null? It would still override the previous value, yes?
No, because if they are both null it the query will error. As it loops through the array, I don't believe bindparam will keep the previous value if the current is null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.