1

i create a code like this

$peringkat=0;
$ranking = array("$ATTT[0]" => "$HA[0]", "$ATTT[1]" => "$HA[1]");
arsort($ranking);
foreach ($ranking as $alternatif => $nilaialternatif) {
$peringkat++;
echo "
<th class='akhir'><input class='rankingakhir' type='text' name='alternatif[]' value='$alternatif' size='12' readonly></th>
<td class='akhir'><input class='nilaiakhir' type='text' name='nilaialternatif[]' value=".number_format($nilaialternatif, 3, '.', ',')." size='12' readonly></td>
<td class='akhir'><input class='rankakhir' type='text' name='rankingalternatif[]' value='$peringkat' size='12' readonly></td></tr>";

}
echo '<div class="akhir"><input class="akhir" type="submit" value="Simpan Ranking ke Database"></div></table></div></div>

my database code like this

<?php  //Sript PHP
$Host = "localhost";  
$User = "root";             //Memilih user/pengguna
$Password = "";             //Password, Biasanya Kosong
$db       = "spkjazuly";         //Memilih Database
$konek=mysql_connect($Host,$User,$Password)or die (mysql_error()); 
mysql_select_db ($db,$konek) or die (mysql_error("TIdak Terhubung Ke Server Mysql")); //Menghubungkan ke Mysql dan memilih Database
mysql_query("insert INTO ranking (idr,alternatif,nilai,ranking) VALUE(DEFAULT,'$_GET[alternatif]','$_GET[nilaialternatif]','$_GET[rankingalternatif]')") //Perintah Mysql untuk mengisi tabel Tamu
or die(mysql_error("Tidak Berhasil Menyimpan !"));  //Or Die (mysql_error()) Pesan Error
echo "<h1>Berhasil Menyimpan Data</h1>"; 
?>

i try run var_dump and the result is like this

array(3) { ["alternatif"]=> array(2) { [0]=> string(12) "Alternatif 1" [1]=> string(12) "Alternatif 2" } ["nilaialternatif"]=> array(2) { [0]=> string(5) "0.628" [1]=> string(5) "0.372" } ["rankingalternatif"]=> array(2) { [0]=> string(1) "1" [1]=> string(1) "2" } }

error displaying enter image description here table live desain enter image description here database table enter image description here

Community
  • 1
  • 1
Jazuly
  • 321
  • 1
  • 4
  • 15
  • First of all Do not use **mysql_*** as ther are deprecated. Use mysqli or PDO instead – Mayank Pandeyz Apr 01 '17 at 01:32
  • Your code is [wide open to SQL injection](http://stackoverflow.com/documentation/php/5828/pdo/2685/preventing-sql-injection-with-parameterized-queries). Please [stop using mysql_ functions as they were removed from PHP](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) – Machavity Apr 01 '17 at 01:33

2 Answers2

0

First of all Do not use mysql_* as ther are deprecated. Use mysqli or PDO instead. The error is here:

VALUE(DEFAULT,'$_GET[alternatif]','$_GET[nilaialternatif]','$_GET[rankingalternatif]')")

as

["alternatif"]=> array(2)

and you can't directly insert an array in a table column.

Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
0

There are two ways to answer your question:

(1) Convert an array to a string representation using serialize()

In your example, this would be something like:

mysql_query("insert INTO ranking (idr,alternatif,nilai,ranking) ". 
"VALUE(DEFAULT,'". serialize($_GET['alternatif']) ."','". serialize($_GET['nilaialternatif']) ."','". serialize($_GET['rankingalternatif']) ."')") 

Of course, when selecting these values from the table, you must use deserialize() to convert them back into the original array structure.

Note that serialize() and deserialize() are PHP specific function while it is bad practice to have database values depend on a specific implementation language. In order to avoid that, you may prefer to use json_encode() and json_decode().

However, now that I have explained how to technically convert arrays into string in order to store them in a database, please consider my second answer:

(2) Don't do this

You may better think over your database design. Even without knowing exactly what you plan to do, I dare to say, that storing arrays as a single database value is a strong indication of bad database design. While this does not actually answer your question, I would recommend to store each array element in a separate table row. Depending on your needs, you may have to add an additional row id or group id column to your table. Then iterate over the array to store the data. For example:

for ($i = 0; $i < count($_GET[alternatif]; $i++) {
  mysql_query("insert INTO ranking (idr,alternatif,nilai,ranking) ". 
  "VALUE(DEFAULT,'". $_GET['alternatif'][$i] ."','". $_GET['nilaialternatif'][$i] ."','". $_GET['rankingalternatif'][$i] ."')") 
}

In addition to that, I recommend you take a look at the alternatives to the mysql_* functions at http://php.net/manual/de/mysqlinfo.api.choosing.php, as those are deprecated and will be removed in PHP 7.

not2savvy
  • 2,902
  • 3
  • 22
  • 37