Got error Undefined id - on the page

Hi
So My code works how it should
i just got the error Undefined variable: id
with the below code

this is the insert for report also the form that goes in a modal

<?php
    if($_SERVER['REQUEST_METHOD'] == 'POST'){
    if(isset($_POST['submitnow'])){
$site = mysqli_real_escape_string($link, $_POST['site']);
$name = mysqli_real_escape_string($link, $_POST['name']);
$email = mysqli_real_escape_string($link, $_POST['email']);
$report = mysqli_real_escape_string($link, $_POST['report']);

    $sql = "INSERT INTO report (site, name, email, report) VALUES ('$site', '$name', '$email','$report')";
    $data = mysqli_query($link, $sql) 
     or die(mysqli_error($link));         
     }
     $_SESSION['report']="<strong>  Thanks For the Report :) </strong>";     
     header("location: profile.php");
     exit;  
    }
  ?>


<form name ="submit" action =""  method = "POST">
    <div class="row">
      <div class="col-md-12 pr-6">
       <div class="form-group">
         <center><label>the site Name is</label></center>
          <input id="site" name="site" disabled type="text" class="form-control" placeholder="" value="<?php echo $list["name"]; ?>">
          </div>
            </div>
                <div class="col-md-12 px-6">
                  <div class="form-group">
                  <center><label> Name </label></center>
                  <input id="name" name="name" type="text" class="form-control" placeholder="" value="" required="required">
                  </div>
                  </div>
                  <div class="col-md-12 px-6">
                  <div class="form-group">
                  <center><label> Email </label></center>
                  <input id="email" name="email" type="text" class="form-control" placeholder="" value="" required="required">
                  </div>
                  </div>
                  <div class="col-md-12 px-6">
                  <div class="form-group">
                  <center><label> Reason </label></center>
                  <textarea id="report" name="report" class="form-control" placeholder="enter the reason" rows="4" required="required"></textarea>
                  </div>
                  </div>
                  </div>    
                 <div class="col-md-12">
               <center><input type="submit" name="submitnow" class="btn btn-info btn-send" value="Report"></center>

          </div>
        </div>
        </form>  
        
        <?php
    if(isset($_SESSION['report'])){
        echo $_SESSION['report'];
        unset($_SESSION['report']);
    }
  ?>

This is what is displaying the data on the same page

 <?php
  if (isset($_GET['id'])) 
  $id = ($_GET['id']); 
  $sql = "SELECT * FROM links WHERE id='$id' LIMIT 1"; this area is where i get the error
  $result = mysqli_query($link, $sql);
  if( $result )    
  while($list = mysqli_fetch_assoc($result)) {
    ?> 
   <?php } ?>

Without using curley braces the if statement is only going to extend to the next line so the query still runs whether the GET id parameter is set or not. It is a poor practice in Php to not use them and can easily cause problems as you see. It is also harder to read the code.

2 Likes

Hi @eyesforcode and a warm welcome to the forum.

Try adding these lines to the beginning of your script and test for variables using the debug function.

<?php 
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors','1');

function debug($val='not set')
{
echo '<pre>'; // add linefeeds
print_r($val);
echo '</pre>';
}//

// ...
//...

debug($_GET);
die;// halt execution

Hi thanks

The Beginning off the script at the top?

if so this is what it displays

**Parse error** : syntax error, unexpected '<', expecting end of file in **profile.php** on line **45**

Fix what I said and you wont need debugging code.

I am tentatively tapping on a tablet and cannot test the script.

Try remming out sections of the PHP script using /* script which is remmed */

Once the edit is removed then reduce the amount of script which is remmed… until the error shows again. Fix the error and remove all rems.

<offtopic>
remmed? Lol, you are aging yourself John. I am also of the rem gen. Old schoolers we are.
</offtopic>

2 Likes

Sorry but i am dumb also Learning :lol:

We all started at the beginning just like you. Just keep at it.

1 Like

At the beginning of the script add the following line about line #6. It should show the line number and halt execution. If it works ok then move the line down until the error appears again or up until the line number shows.

echo __line__; die; // ok up to here

That Worked it showed me 160 number so keep moving it?

Yes it gets a lot easier once you get used to it.

Try moving it half way and if no error move to the remaining half of the script.

If the error shows then move half way back again.

Having no errors doesn’t necessarily mean the script does what you want it to…

Where/how exactly are you setting the GET id? It is nowhere in your code.

Don’t give up because learning PHP is well worth the effort and can make web-pages hop, skip and jump :slight_smile:

I always find it is best to have a working example with no errors so have knocked up the following example:

<?php
declare(strict_types=1);
error_reporting(-1);
ini_set('display_errors', '1');


//========================================================
  function debug($val='not set')
  {
  echo '<pre>DEBUG: '; // add linefeeds
    print_r($val);
    // var_dump($val);
  echo '</pre>';
  }//

//========================================================
  function getPostValue($val)
  :string 
  {
   $result  = isset($_POST["$val"]) && ! empty($_POST["$val"])  
            ? $_POST["$val"]
            : 'Not set???'
            ;
  #echo '<br> $result ==> '.$val . ' --- ' .$result .'<br>';
    return $result;
  }


// CHECK GET and POST DEFAULTS
  $id     = isset($_GET['id']) ? $_GET['id'] : NULL; 

  $lang   = getPostValue ('lang');  
  $first  = getPostValue ('firstname');  
  $first  = getPostValue ('firstname');  
  $last   = getPostValue ('lastname');  
  $submit = isset($_POST['submit']) ? $_POST['submit'] : NULL; 


  $msgs = '';
  if( $submit ) // isset($_POST['submit']) ) 
  {
    $msgs = '<table>';
      $msgs .= '<tr> <td>Language: </td><td>' . $lang .'</td> </tr>';

      if($lang === "english")
      {
        $msgs .= "<tr> <td> First name: </td><td>" . $first . "</td> </tr>";
        $msgs .= "<tr> <td> Last name:  </td><td>" . $last . "</td> </tr>";
      } elseif( $lang === "spanish" ) {
        $msgs .= "<tr> <td> Nombre:   </td><td>"  . $first . "</td> </tr>";
        $msgs .= "<tr> <td> Apellido: </td><td>"  . $last  . "</td> </tr>";
      }
      else {
        $msgs .= "<tr> <td> Nombre:   </td><td>"  . $first . "</td> </tr>";
        $msgs .= "<tr> <td> Apellido: </td><td>"  . $last  . "</td> </tr>";
      }
      $msgs .= '<tr> <td> <br> id: </td><td>' . $id .'</td> </tr>';
    $msgs .= '</table>';
  } // MISSING CLOSING BRACKET

// CHECK GET
  if ($id) 
  {
    $sql = " SELECT *
             FROM links 
             WHERE id='$id' 
             LIMIT 1"; 
             // this area is where i get the error
    if( isset($link) ) 
    {
      $result = mysqli_query($link, $sql);
      if( $result )    
      {
        while($list = mysqli_fetch_assoc($result))
        {
          // echo '<br>' .__line__;
        }//endwhile
      } else {
        $msgs .= '<br> Yes we have no $result ???';
      }
    } else {
      $msgs .= '<br> Yes we have no $link ???';
    }
  } else {
    $msgs .= '<br> Yes we have no $link ???';
}//endif  


?><!doctype html>
<html>
<head>
<title> Just testing </title>
<style>
  .fll {float: left;} 
  .flr {float: right;}
  .ftr {position: fixed; left: 0; bottom: 0;}
  .w99 {width: 99%;}
</style>
</head>
<body>

  <h1> just testing </h1>

  <form action = "" method = "post">
    <p> 
      <label> Language: </label>
      <input type="radio" name="lang" value="english" /> English
      <input type="radio" name="lang" value="spanish" /> Spanish
      <input type="radio" name="lang" value="gibberish" /> No language
    </p>
    <p>
      <label> First name: </label>
      <input type="text" name="firstname"  /> 
    </p>
    <p>
      <label> Last name: </label>
      <input type="text" name="lastname"  />  
    </p>
    <p>
      <input type="submit" name="submit" value="Submit"  />
    </p>
  </form>

  <hr>
    <dl> <dt> <b> DEBUG: </b> $msgs </dt>
      <dd>
        <?= $msgs ?>
      </dd>
    </dl>    
  <hr>  

  <?php debug($_POST); ?>

  <div class="ftr w99">
    <hr>
    <a class="fll" href="#"> HTML validation </a> 
    <a class="flr" href="#"> CSS validation </a> 
  </div>  
</body>
</html>

here is what i am using on the . front end so the user can go to that details page

<?php 
     $limit = 10000;
     if (isset($_GET["id"])) { $id  = $_GET["id"]; } else { $id=1; };
     $start_from = ($id-1) * $limit;
     $sql = " SELECT * FROM links where id ORDER BY ID  DESC  LIMIT $start_from, $limit";    
     $result = mysqli_query($link, $sql);
     while($user = mysqli_fetch_assoc($result)) {
     
     ?>

<td><a href='profile.php?id=<?php echo $user['id']; ?>/<?php echo $user["name"];?>/' class='btn btn-info'>Details</a></br></td>

       <?php } ?>                                      

Thank You John i will try that
see if it puts any errors

i just wanna save the user time so they dont have to
go back to front page then report page again to
enter the sites name and its also good for the
admin as he has less files to edit

Edit

Fatal error: strict_types declaration must be the very first statement in the script in /opt/lampp/htdocs/test.php on line 12```

The example I supplied is a standalone file that I have tried, tested and validated for all the possibilities I could try.

It looks like the example script has been inserted into your script and the declare(strict_types=1); is no longer the first line of the PHP file to be declared and an error was thrown.

Options:

  1. Move the declaration to the top off the file and ensure it is the first statement.
  2. include/require the example file
  3. Remm out the declare statement, proceed to program blinkered and ignore potential errors and warnings

Please note the third option usually changes at five-0-clock on Friday afternoon and prevents the script from running because of errors and warnings :slight_smile:

I am confused because the error states that the strict_types declaration is on line 12.

The example I supplied the strict_types declaration is the first statement and on line 2.

Please copy and post the contents of the following file:

/opt/lampp/htdocs/test.php

it shows now

DEBUG: $msgs
Language:	english
First name:	grrg
Last name:	rg

id:	

Yes we have no $link ???
DEBUG: Array
(
    [lang] => english
    [firstname] => grrg
    [lastname] => rg
    [submit] => Submit
)
1 Like