0

I am stuck with a Warning: Invalid argument supplied for foreach() in C:\xampp\htdocs\risman\admin\edit.php on line 53 in a system I am building and don't have the slightest clue. It must have something to do with the fact that I newbie at coding.

It reads as follows on line 5 :

if($_POST['rowid']) {
    $id = $_POST['rowid'];
    $sql = "SELECT * FROM user WHERE id = $id";
    $result = $koneksi->query($sql);
    foreach ($result as $baris) { ?>

Thanks to all the wise guys around here

Rizman Idris
  • 1
  • 1
  • 3
  • I'm guessing `$koneksi->query($sql)` failed so `$result` is **not** an array... I'm pretty sure this'll get flagged as a [duplicate](https://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php). – CD001 Jul 06 '17 at 15:43
  • so.. what must I do ? – Rizman Idris Jul 06 '17 at 15:43
  • Probably $result is not an array , try print_r($result); die (); and check it – Karim Samir Jul 06 '17 at 15:44
  • *"so.. what must I do ?"* ... debug your code - nobody else can do that for you really (at least not from this snippet). For more info: http://blog.teamtreehouse.com/how-to-debug-in-php – CD001 Jul 06 '17 at 15:49
  • $id should probably be surrounded by quotes in the query. If $result is not a valid array, it means your query failed. – TBowman Jul 06 '17 at 15:49

1 Answers1

0

The problem is because: probably $result is not an array. Please try this:

if($_POST['rowid']) {
$id = $_POST['rowid'];
$sql = "SELECT * FROM user WHERE id = $id";
$result = $koneksi->query($sql);
var_dump($result);
//foreach ($result as $baris) { ?> 

The result should be an array something like that:

 array(2) { [0]=> 
                  array(3) { ["ID"]=> int(1) ["NAME"]=> string(5)   "BLARZ" ["PASS"]=> string(3) "123" } [1]=>
                 array(3) { ["ID"]=> int(2) ["NAME"]=> string(5) "OTHER" ["PASS"]=> string(3) "789" } } 
Blarz
  • 294
  • 3
  • 6