-2

I have this code that I am trying to model from a previous class assignment. The very first part declares a short variable but I am not sure why it is not working. Any ideas?

<?php

if(isset($_POST['in_Person_id'])){ $in_Person_id = $_POST['in_Person_id']
?>

<html>
<head>
<title>Person_Select.php</title>
</head>
<body>

<h1>Guest System</h1>

 <?php

@ $db = new mysqli('localhost', 'hrbailey' , 'hb1628', 'hrbailey');

if (mysqli_connect_errno())
{
 echo 'Error: Could not connect to database. Please try again later.';
 exit;
}

$query = "select * from person
      where person_id = '$in_Person_id' ";
  echo $query;

$result= @mysqli_query ($db, $query);

$num_results = mysqli_num_rows($result);

if ( $num_results == 0)
{
echo '<font color=red>';
echo 'No Person data found. <br />';
echo '</font>';
 echo '<p><a href="Person_Menu.html">Return to Menu</a> </p>';
 }
 else
{
echo ' <br> <br> ';
echo 'Your search found ';
echo $num_results;
echo ' matches';
echo ' <br> <br> <br>';

//Build Table Header
echo'<table align="center" cellspacing="3" cellpadding="3" width="75%">
<tr>
<td align="left"> <b> Person_id    </b> </td>
<td align="left"> <b> Last name    </b> </td>
<td align="left"> <b> First Name    </b> </td>
<td align="left"> <b> Street Address    </b> </td>
<td align="left"> <b> City   </b> </td>
<td align="left"> <b> State   </b> </td>
<td align="left"> <b> Zip   </b> </td>
<td align="left"> <b> RSVP   </b> </td>
<td align="left"> <b> Hotel   </b> </td>
<td align="left"> <b> Household   </b> </td>
<td align="left"> <b> Gift   </b> </td>
</tr>';

while ($row = mysqli_fetch_array($result, MYSQLI_ASSOC))
{
echo '<tr>
   <td align="left">'  .$row['person_id'].   '</td>
   <td align="left">'  .$row['Last Name'].   '</td>
   <td align="left">'  .$row['description'].   '</td>
   <td align="left">'  .$row['First Name'].   '</td>
   <td align="left">'  .$row['Street Address'].   '</td>
<td align="left">'  .$row['City'].   '</td>
<td align="left">'  .$row['State'].   '</td>
<td align="left">'  .$row['Zip'].   '</td>
<td align="left">'  .$row['RSVP'].   '</td>
<td align="left">'  .$row['Hotel'].   '</td>
<td align="left">'  .$row['Household'].   '</td>
<td align="left">'  .$row['Gift'].   '</td>

   <td align="left"> <a href=Person_RSVP.php?person='   
.$row['person_id'].
'&prod='  .$row['person_id'].   '>View Guests </a> </td>
</tr>';
}
echo '</table>';

  echo '<br />';
  echo '<p><a href="Person_Menu.html">Return to Menu</a> </p>';

}
$result->free();
$db->close();

?>
</body>
</html>

Update: I am getting these notices and warnings:

Notice: Undefined index: in_Person_id in C:\student\hrbailey\Person_Select.php on line 3

Guest System

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\student\hrbailey\Person_Select.php on line 30

No Person data found. Return to Menu

Fatal error: Call to a member function free() on a non-object in C:\student\hrbailey\Person_Select.php on line 90

halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    **What** is not working? Are you getting error messages? When you try it what happens and what did you expect to happen instead? – JJJ Apr 18 '15 at 15:24
  • It says it is undefined: – Heather Bailey Apr 18 '15 at 15:25
  • Notice: Undefined index: in_Person_id in C:\student\hrbailey\Person_Select.php on line 3 Guest System Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\student\hrbailey\Person_Select.php on line 30 No Person data found. Return to Menu Fatal error: Call to a member function free() on a non-object in C:\student\hrbailey\Person_Select.php on line 90 – Heather Bailey Apr 18 '15 at 15:25
  • Most likely with your POST array and in your form. Fix that and the rest of the errors will magically disappear. – Funk Forty Niner Apr 18 '15 at 15:26
  • yea i don't understand how its used, i just copied it from the example and changed the name of the field for the rest of the code, but how do i define it? – Heather Bailey Apr 18 '15 at 15:28
  • You have a few answers below including one from me also explaining the possible reasons, if you haven't seen them yet. @HeatherBailey – Funk Forty Niner Apr 18 '15 at 15:39
  • Where are we at with the question? You haven't responded to any of the answers given below. If none of the answers given solved it for you, then post your HTML form in your question as an additional edit. – Funk Forty Niner Apr 18 '15 at 16:09
  • So I tried adding if(isset($_POST['in_Person_id'])){ $in_Person_id = $_POST['in_Person_id'] else {echo "Error getting value";} it gave me unexpected else so i took else then got unexpected echo. So i just took the if and else part out now it says Parse error: syntax error, unexpected end of file in C:\student\hrbailey\Person_Select.php on line 95 – Heather Bailey Apr 18 '15 at 16:10
  • This tells me ^ that you're not showing us your full code, as posted in your original question. Please update it including the HTML form you are using for it. You obviously have a missing or one brace too many, or are using short tag syntax without having short open tag enabled. – Funk Forty Niner Apr 18 '15 at 16:14
  • `if(isset($_POST['in_Person_id'])){ $in_Person_id = $_POST['in_Person_id'] else {echo "Error getting value";}` that's wrong. – Funk Forty Niner Apr 18 '15 at 16:16
  • Reload my answer http://stackoverflow.com/a/29719312/ and look near the bottom under **Edit**. Also go over my answer in its entirety. – Funk Forty Niner Apr 18 '15 at 16:18
  • i just updated the code again in my original post, i am trying! – Heather Bailey Apr 18 '15 at 16:20

3 Answers3

2

Notice taken from comments:

Notice: Undefined index: in_Person_id in C:\student\hrbailey\Person_Select.php on line 3

This is in regards to this line:

$in_Person_id = $_POST['in_Person_id'];

Your HTML form's element may not hold the name attribute for it, or it contains a typo.

It should look something like this:

<form method="post" action="handler.php">
Person ID:
<input type="text" name="in_Person_id">
<input type="submit" name="submit" value="Submit">
</form>

Or if the HTML form's element is a hidden type from a fetched query inside it:

<input type="hidden" name="in_Person_id">

You did not share your HTML form, therefore that is what I have submitted as an answer.

Sidenote: in_Person_id is not the same as in_person_id should that be the case.

  • Those are case-sensitive and are two different animals altogether.

Using empty() / isset() with a conditional statement should be used.

I.e.:

if(!empty($_POST['in_Person_id'])){...}

or using both functions:

if(isset($_POST['in_Person_id']) && !empty($_POST['in_Person_id']) ){...}

Differences between isset() and empty():

It depends what you are looking for, if you are just looking to see if it is empty just use empty as it checks whether it is set as well, if you want to know whether something is set or not use isset.

Empty() checks if the variable is set and if it is it checks it for null, "", 0, etc

Isset() just checks if is it set, it could be anything not null


In your particular case: if ($var).

You need to use isset if you don't know whether the variable exists or not. Since you declared it on the very first line though, you know it exists, hence you don't need to, nay, should not use isset.

The same goes for empty, only that empty also combines a check for the truthiness of the value. empty is equivalent to !isset($var) || !$var and !empty is equivalent to isset($var) && $var, or isset($var) && $var == true.

If you only want to test a variable that should exist for truthiness, if ($var) is perfectly adequate and to the point.


  • Plus, if your HTML form is part of your PHP/MySQL which is unknown, then you should use a conditional statement as such which may explain the notice for it.

Edit:

Taken from comments:

if(isset($_POST['in_Person_id'])){ $in_Person_id = $_POST['in_Person_id'] else {echo "Error getting value";}

should be (missing a brace and semi-colon)

Place it before your query:

if(isset($_POST['in_Person_id'])){

 $in_Person_id = $_POST['in_Person_id']; 
 } 
 else {
 echo "Error getting value";
 }

$query = "select * from person
      where person_id = '$in_Person_id' ";
  echo $query;

 // rest of your code
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • ok i updated that and now i get: Error getting value Guest System Notice: Undefined variable: in_Person_id in C:\student\hrbailey\Person_Select.php on line 31 select * from person where person_id = '' Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\student\hrbailey\Person_Select.php on line 36 No Person data found. Return to Menu Fatal error: Call to a member function free() on a non-object in C:\student\hrbailey\Person_Select.php on line 96 – Heather Bailey Apr 18 '15 at 16:23
  • @HeatherBailey depends on where you placed it in your code. Put that before your query `$query = "select * from person...` as outlined in another edit. – Funk Forty Niner Apr 18 '15 at 16:27
  • @HeatherBailey where is your HTML form also? I need to know where that is and if it's in the same file as the posted code. – Funk Forty Niner Apr 18 '15 at 16:30
  • i don't even know where i have the form declared? – Heather Bailey Apr 18 '15 at 16:31
  • @HeatherBailey that's the **root** of the problem then. You need an HTML form when you use `$_POST['in_Person_id']`. Why are you using that if you don't have a form? That explains everything. – Funk Forty Niner Apr 18 '15 at 16:32
  • i think this it? Person_Select.php
    Attribute Value
    Person Code
    – Heather Bailey Apr 18 '15 at 16:37
  • @HeatherBailey Yes. You need to use that ^ **before** you can load the Person_Select.php file and filling in the field for `Person Code`. – Funk Forty Niner Apr 18 '15 at 16:39
  • ok that worked, so now i have my html pointing to my html (i had it pointing to php first) and now when i enter in a person code i am still getting an error (i have person codes from 1 - 25 in the database) select * from person where person_id = '1' Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\student\hrbailey\Person_Select.php on line 32 No Person data found. Return to Menu Fatal error: Call to a member function free() on a non-object in C:\student\hrbailey\Person_Select.php on line 92 – Heather Bailey Apr 18 '15 at 16:45
  • ok i got some data back this time but it is telling me that i have undefined indexes in my databses now... thanks for helping it got me much further than i was a few hours ago, appreciate it! i would give you a check but i don't have enough points yet – Heather Bailey Apr 18 '15 at 17:05
  • @HeatherBailey you have spaces in some of your column names, one being `Last Name` and others so that could be an additional factor. It's not good practice using spaces, where you should be using underscores for column names. – Funk Forty Niner Apr 18 '15 at 17:13
  • oh ok thanks! i took the spaces out in the tables and the code, for some reason im still getting an undefined for on of the PERSON_id tags – Heather Bailey Apr 18 '15 at 17:23
2

When using POST/GET , you should always check if the passed index exists , to do so , just use function isset();

Your code should look like:

<?php
if(isset($_POST['in_Person_id'])){ $in_Person_id = $_POST['in_Person_id'] ... and other functions here} else {echo "Error getting value";}
0

Try removing the '@' from @$db as the @ sign suppresses errors. Removing the @ should give you errors you can work with.

Besides the lacking of an error message, some (off-topic) tips to improve/refactor your code:

  • Separate your PHP and HTML, you don't want ending up with large pieces of PHP in your HTML. Makes it hard to debug and edit later on.
  • Pay attention to where you are starting <?php, in the current piece of code, make sure to close it properly. (I can tell from your code you are not closing ?> before the HTML element.
  • For later research (you don't want to rewrite this as of now) you could use PDO, more advanced and general than MySQLi, but valuable to grasp later on.

EDIT You can ignore the first paragraph of this answer, as I have interpreted your question differently.

jbehrens94
  • 2,356
  • 6
  • 31
  • 59
  • Downvoter: care to explain your down vote? I have edited my question accordingly to the right perception of the TS. – jbehrens94 Apr 18 '15 at 15:38