-2

Hello there I have some problem with my code:

if (!empty($_GET['id'])){
    $id = $_GET['id'];
}else if (!empty($_POST['id'])){
    $id = $_POST['id'];
}

$get_id = 'SELECT * FROM kunde WHERE id=' . $id;
$r = mysql_query($get_id);
$t = mysql_fetch_array($r);

$_SESSION['id'] = $t['id'];

I also have my hidden field:

<input type="hidden" name="id" value="<?php echo $_SESSION['id']; ?>">

But I got always the same error:

Notice: Undefined variable: id in C:\xampp\htdocs\Uebungsklausur\PHP\change.php on line 30

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\xampp\htdocs\Uebungsklausur\PHP\change.php on line 32

Please help me guys.. I just want to get the ID's from the table "kunde"

EDIT: Thank you guys I solved it. The problem was in the form tag. That was the first code: <form method="post"> And now it's: '<form method="post" action=" change.php?id=' . $row[0] . '">';

Syntrax
  • 3
  • 1
  • 3
  • 3
    Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Phiter Nov 21 '16 at 23:03
  • You should use `isset();` to check whether the variables `$_GET['id']` or `$_POST['id']` exist or not. –  Nov 21 '16 at 23:12
  • if(isset($_GET['id'])){ echo "GET_ID exists"; }else{ echo "Error"; } if(isset($_POST['id'])){ echo "POST_ID exists"; }else{ echo "Error"; } I got "Error" but I dont know why.. – Syntrax Nov 21 '16 at 23:29
  • personally I test for both. Variable exists AND is not empty! `if ( (isset($VAR)) && (!empty($VAR)) ){ // use $VAR` – Duane Lortie Nov 21 '16 at 23:58

3 Answers3

1

This code is too complicated for now since there isn't enough information. The code is too vague since I haven't got a clue what you want to do with it and where the code is supposed to lead you to.

the following will maybe help you:

if you're trying to get it form the table customer, you're doing it wrong. You are now getting it from table 'kunde'. You're also asking for an ID at the WHERE statement, which you don't have since you're trying to get it with the actual statement?

The following code is what I think you ment to do.

Make an input that sends the ID with it, if it exists.

<input type="hidden" name="id" value="<?php if (isset($_SESSION['id'])) { echo $_SESSION['id']; } ?>">

Then on the following code check if 'id' is set and return with id?

if (isset($_POST['id'])) {
$id = $_POST['id'];
// REST OF CODE (The $get_id, $r, $t and setting $_SESSION)
}
if (isset($_GET['id'])) {
$id = $_GET['id'];
// REST OF CODE (The $get_id, $r, $t and setting $_SESSION)
}
Paul de Koning
  • 422
  • 1
  • 5
  • 16
0

Let's address some things on your provided code:
1. You use GET and POST at the same time? I wonder which is the one that you actually used.
2. I assume that you're getting the ID in from the $_SESSION['id']. Another way is that you can get the $id from it directly.

With those things in mind:

Let's assume you're using GET to fetch the ID.

First, change this:

if (!empty($_GET['id'])){
    $id = $_GET['id'];
}else if (!empty($_POST['id'])){
    $id = $_POST['id'];
}

to this

    if (isset($_GET['id'])){
        $id = $_GET['id'];
    }
   else if (!isset($GET['id'])) {
        $id = $_SESSION['id'];
    }

But please make clear of what you are asking.

Rav
  • 1,327
  • 3
  • 18
  • 32
0

This happened because $id is not declared out of IF statement, i.e, the $id variable exists only inside if or else statement and outside it doesn't exist.

To fix it you can:

1# Declare $id outside of if statement

That makes your code run

   $id; // or $id = null
   if (!empty($_GET['id'])){
       $id = $_GET['id'];
   }else if (!empty($_POST['id'])){
       $id = $_POST['id'];
   }

   $get_id = 'SELECT * FROM kunde WHERE id=' . $id;
   $r = mysql_query($get_id);
   $t = mysql_fetch_array($r);

   $_SESSION['id'] = $t['id'];

2# Use ternary operator

It's very simple to use:

   $id = (!empty($_GET['id'])) ? $_GET['id'] : $_POST['id'];

   $get_id = 'SELECT * FROM kunde WHERE id=' . $id;
   $r = mysql_query($get_id);
   $t = mysql_fetch_array($r);

   $_SESSION['id'] = $t['id'];

3# Use $_REQUEST global

$_REQUEST global it's a "summary" of $_POST and $_GET

   $id = $_REQUEST['id'];

   $get_id = 'SELECT * FROM kunde WHERE id=' . $id;
   $r = mysql_query($get_id);
   $t = mysql_fetch_array($r);

   $_SESSION['id'] = $t['id'];