I have koneksi_class with code.
How to looping $value in query INSERT INTO $table VALUES ($value0, $value1, $value2)
<?php
function tambahAnggota($value0, $value1, $value2, $value3, $value4, $value5) {
$table = $_GET['tujuan'];
$query = "INSERT INTO $table VALUES ('$value0', '$value1','$value2')";
$hasil = mysql_query($query);
if ($hasil)
echo"<meta http-equiv='refresh' content='0; url=?tujuan=$table'>";
else
echo "Pesan error: ".mysql_error();
}
?>
func_get_args
— Returns an array comprising a function's argument list
func_num_args
— Returns the number of arguments passed to the function
Using this methods, you can achieve looping all the arguments like this:
<?php
function tambahAnggota($value0, $value1, $value2, $value3, $value4, $value5)
{
$numargs = func_num_args();
$values = [];
for ($i = 0; $i < $numargs; $i++) {
array_push($values, '\''.func_get_arg($i).'\'');
}
$strValues = implode(',', $values);
$table = $_GET['tujuan'];
$query = "INSERT INTO $table VALUES ($strValues)";
$hasil = mysql_query($query);
if ($hasil)
echo "<meta http-equiv='refresh' content='0; url=?tujuan=$table'>";
else
echo "Pesan error: " . mysql_error();
}
tambahAnggota(1, 2, 3, 4, 5, 6);