Jump to content
Larry Ullman's Book Forums

How To Use A Number Sent In Url From Previous Page.


Recommended Posts

I am trying to learn how to use a number sent in a URL from a page on my site.

 

I run a script that allows me go to "register_attendance.php?hh_id=".    "hh_id" is the primary key for a record in a database table named household.  Then I want to insert a new record in a table named attendance that would contain this number (hh_id) in a column named fkhh_id.

 

But I obviously don't know how to use the INSERT INTO query so that new record will appear in the attendance table with the number (hh_id) in the column named fkhh_id.

If anyone can help me understand how to insert the number sent in the URL into a newrecord in another table, WOW!  I will appreciate it.

I trust you can understand my question, but it is hard to write down in words.
Thanks to all
Wes Smith

Link to comment
Share on other sites

Thanks for the recommendation.

I tried the following:

 

    // Make the query:
    $q = "INSERT INTO Attendance (dateofattendance,fkhh_id,attendees,event,note) VALUES ('$da','$_GET['hh_id']', '$a','$e','$n')";
    $r = @mysqli_query ($dbc, $q); // Run the query.
    if ($r) { // if it ran OK.
 
      //Print a message:
      echo '<h2>Attendance is recorded.</h2>
 
When I do this all the columns in Attendance are written to a new record in the table, but fkhh_id always received a zero.
 
I don't know what I am doing wrong.
Thanks.
Wes
Link to comment
Share on other sites

I have confirmed that the correct id is sent to register_attendance.php.

But still the INSERT query does not put this number in the attendance table, but does put everthing else.

I am including the code here so maybe you can determine what I am doing wrong.

-----------------------

<?php # Script 9.5 - register_attendance.php #2

// Check for a valid user ID, through GET:

if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // From view_households.php

                $id = $_GET['id'];

                echo '<p>The household id is <b>' . $id . '</b></p>';

}

echo '<p>The household id is <b>' . $id . '</b></p>';

 

$page_title = 'Register Attendance';

include ('includes/header.html');

 

// Check for form submission:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 

  $errors = array(); // Initialize an error array.

 

  $da = $_POST['dateofattendance'];

 

  // Check for attendees:

  if (empty($_POST['attendees'])) {

    $errors[] = 'You forgot to enter the attendees.';

  } else {

    $a = trim($_POST['attendees']);

  }

 

  // check for event:

  if (empty($_POST['event'])) {

    $errors[] = 'You forgot to enter the event.';

  } else {

    $e = trim($_POST['event']);

  }

 

  // check for note:

  if (empty($_POST['note'])) {

    $errors[] = 'You forgot to enter a note.';

  } else {

    $n = trim($_POST['note']);

  }

 

  if (empty($errors)) { // If everything is OK.

    // Record the attendance in the table.

 

    require ('includes/mysqli_connect.php');

 

    // Make the query:

    $q = "INSERT INTO Attendance (dateofattendance,fkhh_id,attendees,event,note) VALUES ('$da','$id', '$a','$e','$n')";

    $r = @mysqli_query ($dbc, $q); // Run the query.

    if ($r) { // if it ran OK.

 

      //Print a message:

      echo '<h2>Attendance is recorded.</h2>

    <p><br /></p>';

      echo "hh_id=" . $_SESSION['household'];

 

    } else { // If it did not run OK.

 

      //Public message:

      echo '<h2>Attendance was not recorded.</h2>

      <p><br /></p>';

      // Debugging message:

      echo '<p>' . mysqli_error($dbc) . '<br /><br />Query: ' . $q . '</p>';

    } // End of if ($r) IF.

 

    mysqli_close($dbc); // Close the database connection.

    // Include the footer and quit the script:

    include ('includes/footer.html');

    exit();

 

    } else { // Report the errors.

 

    echo '<h2>Error!</h2>

    <p class="error">The following error(s) occurred:<br />';

    foreach ($errors as $msg) { // Print each error.

       echo " - $msg<br />";

    }

    echo '</p><p>Please try again.</p><p><br /></p>';

  }// End of if (empty($errors)) IF.

  } // End of the main Submit conditional.

?>

<h1>Register Attendance</h1>

<form action="register_attendance.php" method="post">

                <p>Date Of Attendance: <input type="date" name="dateofattendance" size="15" maxlength="20" value="<?php if (isset($_POST['dateofattendance'])) echo $_POST['dateofattendance']; ?>" /></p>

        <p>Attendees: <input type="text" name="attendees" size="50" maxlength="75" value="<?php if (isset($_POST['attendees'])) echo $_POST['attendees']; ?>" /></p>

                <p>Event: <input type="text" name="event" size="25" maxlength="25" value="<?php if (isset($_POST['event'])) echo $_POST['event']; ?>" /></p>

                <p>Note: <input type="text" name="note" size="25" maxlength="25" value="<?php if (isset($_POST['note'])) echo $_POST['note']; ?>" /></p>

                <p><input type="submit" name="submit" value="register_attendance" /></p>

</form>

<?php include ('includes/footer.html'); ?>

-----------------------------------------------------------

 

I am making an error in handling the information in the code!

But I am at a lost on how to proceed.

If you can identify my error, I will be eternally grateful!

Wes

Link to comment
Share on other sites

The id you want to store in the db is not being submitted with your form. You need to include a hidden form field within your form, with the id value:

 



<form ... >

<input type="hidden" name="id" value="<? echo $id; ?>">

</form>


 


You'll then need to assign the $_POST['id'] value to $id within your if ($_SERVER['REQUEST_METHOD'] == 'POST') { ... } conditional and before the db insert query.

 



if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
  $id = $_POST['id'];
  # db query
}


 


Plus, what margaux said ;)

Link to comment
Share on other sites

Margaux:

I tried the INSERT INT0 query both ways:

$q = "INSERT INTO Attendance (dateofattendance,fkhh_id,attendees,event,note) VALUES ('$da','$id', '$a','$e','$n')";

$q = "INSERT INTO Attendance (dateofattendance,fkhh_id,attendees,event,note) VALUES ('$da',$id, '$a','$e','$n')";

and also as:

$q = "INSERT INTO Attendance (dateofattendance,fkhh_id,attendees,event,note) VALUES ('$da',id, '$a','$e','$n')";

 

All three assign the number zero to the record row from the Household table.  This is the problem.

 

To check on that, I added a few lines of code very early in "register_attendance.php" as follows:

// Check for a valid user ID, through GET:

if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // From view_households.php

                $id = $_GET['id'];

                echo '<p>The household id is <b>' . $id . '</b></p>';

}

echo '<p>The household id is <b>' . $id . '</b></p>';

 

These echo statements do display $id or $_GET['id'] correctly.

I have checked this since I do know the number sent from view_households.php.

 

BUT THE INSERT INTO query always inserts a zero for that column.

 

AGH!   I have been searching for a solution now for 4 days! 

It is important that the user NOW PROVIDE the household table id, but will select the household id when they click the "register_attendance.php?id"......

 

Please know how much I appreciate and value your input!

 

God Bless!

Wes

Link to comment
Share on other sites

wes, my best advice is to walk through your code one line at a time, and as you do, echo out some sort of meaningful value that will allow you to confirm your assumptions. Through that, you will very likely be able to find the problem and resolve it.

Also, you may want to try executing the query directly on the DB through phpMyAdmin without any variables, and see what you get.

Link to comment
Share on other sites

 Share

×
×
  • Create New...