dreq dreq - 1 year ago 50
MySQL Question

How to show success with multi insert inside function PDO PHP without javascript for processing

I've a function like this :

rangking.inc.php


function insert2(){
$query = "insert into ".$this->table_name." values(?,?,?,'','')";
foreach ($this->ik as $key => $value){
$stmt = $this->conn->prepare($query);
$stmt->bindParam(1, $this->ia);
$stmt->bindParam(2, $value);
$stmt->bindParam(3, $this->nn[$key]);
$stmt->execute();
}
}


but I'm stuck at how to count inserted have been done, and I want to show only one message for all values inserted.

can someone help me how to do?

here a piece of my code to show :

rangking.php


<?php
# include code

if($_POST){

include_once 'path/rangking.inc.php';
$eks = new rangking($db);

$eks->ia = $_POST['ia'];
$eks->ik = $_POST['ik'];
$eks->nn = $_POST['nn'];

# if($eks->insert2()){ using this code just throw me to false condition
?>
<!-- <div class="alert alert-success alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Berhasil Tambah Data!</strong> Tambah lagi atau <a href="rangking.php">lihat semua data</a>.
</div> -->
<?php
# }

# else{
?>
<!-- <div class="alert alert-danger alert-dismissible" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
<strong>Gagal Tambah Data!</strong> Terjadi kesalahan, coba lagi.
</div> -->
<?php
# }
}
?>
# below is all html code to show for form

Answer Source

Because your function isn't returning anything

function insert2(){     
    $i = 0;

    $query = "insert into ".$this->table_name." values(?,?,?,'','')";
    $stmt = $this->conn->prepare($query);

    foreach ($this->ik as $key => $value){
        $stmt->bindParam(1, $this->ia);
        $stmt->bindParam(2, $value);
        $stmt->bindParam(3, $this->nn[$key]);
        $stmt->execute();
        $i += 1;
    }

    return $i;

}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download