We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

php - How to add fields to database sent to a php page from another php page

Medium Priority
481 Views
Last Modified: 2012-05-11
I am using Dreamweaver CS5 to create 2 related php pages. On the first page, I collect data on a person’s email, password, and last name. This is sent to the second page, which does the following:
1.      It uses the email, password, and last name to go into one table of a database and finds other information, such as full name and address, and prints that out on the page.
2.      Within a form, it provides a textarea so that the user can send a message to us.
3.      Within the same form, it provides a drop-down list of titles that the user can pick from.
4.      Then the submit button for the form sends the information from the textarea and drop-down list to another table in the database.

My problem is that I also want to send the email, password, and last name along with the textarea and drop-down list, but when I look at the code generated by Dreamweaver to Insert Record, I can’t figure out how to change it to include those fields. I’ve tried some things and at best, I can get it to set the values equal to the defaults of -1.

The code for the email, password, and last name is:
$xvar1_rsNameInfo = "-1";
if (isset($_POST['email'])) {
  $xvar1_rsNameInfo = $_POST['email'];
}
$xvar2_rsNameInfo = "-1";
if (isset($_POST['password'])) {
  $xvar2_rsNameInfo = $_POST['password'];
}
$xvar3_rsNameInfo = "-1";
if (isset($_POST['lastname'])) {
  $xvar3_rsNameInfo = $_POST['lastname'];
}
mysql_select_db($database_connTalkingpages, $connTalkingpages);
$query_rsNameInfo = sprintf("SELECT firstname,lastname,streetaddress,city,state,zipcode FROM Results WHERE email = %s AND password = %s AND lastname = %s", GetSQLValueString($xvar1_rsNameInfo, "text"),GetSQLValueString($xvar2_rsNameInfo, "text"),GetSQLValueString($xvar3_rsNameInfo, "text"));
$rsNameInfo = mysql_query($query_rsNameInfo, $connTalkingpages) or die(mysql_error());
$row_rsNameInfo = mysql_fetch_assoc($rsNameInfo);
$totalRows_rsNameInfo = mysql_num_rows($rsNameInfo);

The code for the textarea and drop-down list is:
if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "checkout")) {
  $insertSQL = sprintf("INSERT INTO Checkout (textboxinfo, Title) VALUES (%s, %s)",
                       GetSQLValueString($_POST['textboxinfo'], "text"),
                       GetSQLValueString($_POST['Title'], "text")
                                 );

  mysql_select_db($database_connTalkingpages, $connTalkingpages);
  $Result1 = mysql_query($insertSQL, $connTalkingpages) or die(mysql_error());


Thanks for your help!
Comment
Watch Question

Don't talk to me.
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Hi jason1178,

The data from page 1 looks into a table that contains user information (one record per user). Page 1 is a login with email and password that gives a person access to page 2 and shows them the current address we have for them.

The main purpose of page 2 is to get further inputs from them by means of the textarea and drop-down list. That information is being sent to a different table that contains one record per submission. When I look at the results in that table, I need to know who submitted the information which is why I want to pass along the email and password that was input on page 1.

Let me know if that is still not clear. Thanks!
Jason C. LevineDon't talk to me.
CERTIFIED EXPERT

Commented:
In the code snippets you posted above...are those from page 1, page 2 or both?  

Author

Commented:
They are both from page 2. I didn't include anything from page 1 because it correctly sends the information to page 2 and prints out the data I want from the database.
Jason C. LevineDon't talk to me.
CERTIFIED EXPERT

Commented:
So to get the data in the 2nd form on page 2, why not use hidden fields and recordset variables as I outlined?  Or is the recordset failing?

Author

Commented:
The variables email and password are not currently in the recordset rsNameInfo because they are not being written out. It turns out that the variable lastname is in the recordset because it is the only variable that is both input by the user on page 1 and written back out as part of the verification on page 2. I had created the recordset rsNameInfo to include just the variables being written on page 2.

So I tried what you had outlined, using the input tag, substituting lastname. The results are that I get the textboxinfo and Title information in the database, as before (based on the INSERT command), but I don't get anything for lastname. Is there a way to add to the php code that appears at the top of the page to include the three variables I want to add?

Thanks!
Jason C. LevineDon't talk to me.
CERTIFIED EXPERT
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview

Author

Commented:
Page 1 (checkoutlogin.php) is posting the results to page 2 (checkout.php). I have the full code for both pages in the attachment - they are both based on a template that has a lot of extra comments still in it.

I am not familiar with session variables and how they are used. Generally I let Dreamweaver create the code, and then usually I can follow it to change it for adding similar items. If it makes sense to use session variables after you look at the code, can you help with what I would take out from the current code as well as adding? Thanks so much.
Page 1: 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/Template1.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Talking Pages - An Online Library for the Visually Impaired</title>
<!-- InstanceEndEditable -->
<link href="talkingpages.css" rel="stylesheet" type="text/css" /><!--[if lte IE 7]>
<style>
.content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the columns in this layout with the same corrective effect. */
ul.nav a { zoom: 1; }  /* the zoom property gives IE the hasLayout trigger it needs to correct extra whitespace between the links */
</style>
<![endif]-->
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
</head>

<body>

<div class="container">
  <div class="header"><!-- InstanceBeginEditable name="Header" -->
    <h1>Check-Out Login</h1>
  <!-- InstanceEndEditable --><!-- end .header --></div>
  <div class="subheader1"><!-- InstanceBeginEditable name="Subheader1" -->
    <h4>Talking Pages</h4>
  <!-- InstanceEndEditable --></div>
  <div class="subheader2">
    <ul class="nav">
      <li><a href="index.html">Home</a></li>
      <li><a href="procedures.html">Procedures</a></li>
      <li><a href="newlibrarycards.php">New Library Cards</a></li>
      <li><a href="onlinestatus.php">Online Status</a></li>
      <li><a href="catalog.php">Catalog</a></li>
      <br />
      <li><a href="printerfriendlycatalog.php">Printer-Friendly Catalog</a></li>     
      <li><a href="checkoutlogin.php">Check-Out Login</a></li>
      <li><a href="history.html">History</a></li>
      <li><a href="donations.php">Donations</a></li>
      <li><a href="latestnews.html">Latest News</a></li>
    </ul>
    </div>
  <div class="sidebar1">
    <p>Our next mailing date is: <strong>May 7</strong></p>
    <p> Talking Pages is now mailing out audio books every 3 to 4 weeks on the mailing date 
      listed on our web site. </p>
    <p>On that date, we mail all books 
      requested since the last mailing date (up to a total of 10 audio books 
      checked out per user).</p>
    <p> Users do not have to request books all at one time – 
      any books requested between mailings will be shipped on the next mailing 
      date.</p>
 <!-- end .sidebar1 --></div>
  <div class="content"><!-- InstanceBeginEditable name="EditRegion3" -->
    <form id="checkout" name="checkout" method="post" action="checkout.php">
    <table align="center" border=5px cellpadding=2px>
    <tr valign="baseline">
       <td>You may begin the check-out procedure by signing in with your email address, password, and the last name of the person who will be receiving the audio books:
       </td>
       <td><strong>New User? </strong><br />
         If so, please sign up for a <a href="newlibrarycards.php">new library card</a>. </td>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">email:</td>
      <td>
      <input class="tt" type="text" id="email" name="email" value="" size="30" />
      </td>
 
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Password:</td>
      <td>
        <input class="tt" type="password" id="password" name="password" value="" size="30" />
        </td>
        </tr>
  <tr valign="baseline">
      <td nowrap="nowrap" align="right">Last Name:</td>
      <td>
        <input class="tt" type="text" id="lastname" name="lastname" value="" size="30" />
        </td>
        </tr>      
    <tr valign="baseline">
      <td nowrap="nowrap" align="center">&nbsp;</td>
      <td><input class="btn" name="Submit" type="submit" id="Submit" value="Submit"  />   
        <input class="btn" type="reset" name="reset" id="reset" value="Reset" /></td>
    </tr>
    </table>
    </form>
    <h1>&nbsp;</h1>
  <!-- InstanceEndEditable --><!-- end .content --></div>
  <div class="footer">
    <ul class="nav">
      <li><a href="index.html">Home</a></li>
      <li><a href="procedures.html">Procedures</a></li>
      <li><a href="newlibrarycards.php">New Library Cards</a></li>
      <li><a href="onlinestatus.php">Online Status</a></li>
      <li><a href="catalog.php">Catalog</a></li>
      <br />
      <li><a href="printerfriendlycatalog.php">Printer-Friendly Catalog</a></li>     
      <li><a href="checkoutlogin.php">Check-Out Login</a></li>
      <li><a href="history.html">History</a></li>
      <li><a href="donations.php">Donations</a></li>
      <li><a href="latestnews.html">Latest News</a></li>
    </ul>
    <!-- end .footer --></div>
<!-- end .container --></div>
</body>
<!-- InstanceEnd --></html>


Page 2:
<?php require_once('../Connections/connTalkingpages.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

$xvar1_rsNameInfo = "-1";
if (isset($_POST['email'])) {
  $xvar1_rsNameInfo = $_POST['email'];
}
$xvar2_rsNameInfo = "-1";
if (isset($_POST['password'])) {
  $xvar2_rsNameInfo = $_POST['password'];
}
$xvar3_rsNameInfo = "-1";
if (isset($_POST['lastname'])) {
  $xvar3_rsNameInfo = $_POST['lastname'];
}
mysql_select_db($database_connTalkingpages, $connTalkingpages);
$query_rsNameInfo = sprintf("SELECT firstname,lastname,streetaddress,city,state,zipcode,email FROM Results WHERE email = %s AND password = %s AND lastname = %s", GetSQLValueString($xvar1_rsNameInfo, "text"),GetSQLValueString($xvar2_rsNameInfo, "text"),GetSQLValueString($xvar3_rsNameInfo, "text"));
$rsNameInfo = mysql_query($query_rsNameInfo, $connTalkingpages) or die(mysql_error());
$row_rsNameInfo = mysql_fetch_assoc($rsNameInfo);
$totalRows_rsNameInfo = mysql_num_rows($rsNameInfo);

mysql_select_db($database_connTalkingpages, $connTalkingpages);
$query_rsTitles = "SELECT Title FROM `Catalog` WHERE Status = 'Available' ORDER BY Title ASC";
$rsTitles = mysql_query($query_rsTitles, $connTalkingpages) or die(mysql_error());
$row_rsTitles = mysql_fetch_assoc($rsTitles);
$totalRows_rsTitles = mysql_num_rows($rsTitles);

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "checkout")) {
  $insertSQL = sprintf("INSERT INTO Checkout (textboxinfo, Title) VALUES (%s, %s)",
                       GetSQLValueString($_POST['textboxinfo'], "text"),
                       GetSQLValueString($_POST['Title'], "text")
					   );

  mysql_select_db($database_connTalkingpages, $connTalkingpages);
  $Result1 = mysql_query($insertSQL, $connTalkingpages) or die(mysql_error());

  $insertGoTo = "checkoutconfirmation.html";
  if (isset($_SERVER['QUERY_STRING'])) {
    $insertGoTo .= (strpos($insertGoTo, '?')) ? "&" : "?";
    $insertGoTo .= $_SERVER['QUERY_STRING'];
  }
  header(sprintf("Location: %s", $insertGoTo));
}


?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><!-- InstanceBegin template="/Templates/Template2.dwt" codeOutsideHTMLIsLocked="false" -->
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- InstanceBeginEditable name="doctitle" -->
<title>Talking Pages - An Online Library for the Visually Impaired</title>
<!-- InstanceEndEditable -->
<link href="talkingpages.css" rel="stylesheet" type="text/css" /><!--[if lte IE 7]>
<style>
.content { margin-right: -1px; } /* this 1px negative margin can be placed on any of the columns in this layout with the same corrective effect. */
ul.nav a { zoom: 1; }  /* the zoom property gives IE the hasLayout trigger it needs to correct extra whitespace between the links */
</style>
<![endif]-->
<!-- InstanceBeginEditable name="head" -->
<!-- InstanceEndEditable -->
</head>

<body>

<div class="container">
  <div class="header"><!-- InstanceBeginEditable name="Header" -->
    <h1>Check-Out</h1>
  <!-- InstanceEndEditable --><!-- end .header --></div>
  <div class="subheader1"><!-- InstanceBeginEditable name="Subheader1" -->
    <h4>Talking Pages</h4>
  <!-- InstanceEndEditable --></div>
  <div class="subheader2">
    <ul class="nav">
      <li><a href="index.html">Home</a></li>
      <li><a href="procedures.html">Procedures</a></li>
      <li><a href="newlibrarycards.php">New Library Cards</a></li>
      <li><a href="onlinestatus.php">Online Status</a></li>
      <li><a href="catalog.php">Catalog</a></li>
      <br />
      <li><a href="printerfriendlycatalog.php">Printer-Friendly Catalog</a></li>     
      <li><a href="checkoutlogin.php">Check-Out Login</a></li>
      <li><a href="history.html">History</a></li>
      <li><a href="donations.php">Donations</a></li>
      <li><a href="latestnews.html">Latest News</a></li>
    </ul>
    </div>
  
  <div class="content2"><!-- InstanceBeginEditable name="EditRegion3" -->
    <h2>Your Account Information</h2>
    <p><strong>The name and address associated with the login information you provided is:</strong></p>
    <table align="center" border="1">
      <?php do { ?>
      <tr>
        <td><strong>First Name</strong></td>
        <td><?php echo $row_rsNameInfo['firstname']; ?></td>
      </tr>
      <tr>
        <td><strong>Last Name</strong></td>
        <td><?php echo $row_rsNameInfo['lastname']; ?></td>
      </tr>
      <tr>
        <td><strong>Street Address</strong></td>
        <td><?php echo $row_rsNameInfo['streetaddress']; ?></td>
      </tr>
      <tr>
        <td><strong>City</strong></td>
        <td><?php echo $row_rsNameInfo['city']; ?></td>
      </tr>
      <tr>
        <td><strong>State</strong></td>
        <td><?php echo $row_rsNameInfo['state']; ?></td>
       </tr>
       <tr>
        <td><strong>Zip Code</strong></td>
        <td><?php echo $row_rsNameInfo['zipcode']; ?></td>
      </tr>
        <?php } while ($row_rsNameInfo = mysql_fetch_assoc($rsNameInfo)); ?>
    </table>
<h2>Contacting Us</h2>
<p><strong>If you need to change the address listed above or provide any other instructions, please provide the information in the following textbox. Also, if you are having any problems selecting a book from the drop-down list below, you may type the title of the audio books you want to check out into the following textbox. Once you are done here and/or selecting audio books from the list below, click the Submit button at the bottom of the page.</strong></p>
    <form action="<?php echo $editFormAction; ?>" id="checkout" name="checkout" method="POST">
      <p>
        
      </p>
      <p>
        <label>Enter information here:<br />
       
          <textarea class="tt2" name="textboxinfo" id="textboxinfo" cols="45" rows="7">&nbsp;</textarea>
        </label>
      </p>
      <h2>Selecting Audio Books to Check Out</h2>
      <p><strong>Please select the audio books you would like to check out from the following list of <u>available</u> books. You may have a total of 10 audio books checked out at any one time. </strong></p>
      <p><strong>If you hold down the CTRL key (Windows) or the Command key (Mac) <img src="images/command.jpg" width="23" height="25" alt="Command key on Mac" />, you can select multiple books at one time. You can also go through the check-out procedure multiple times.</strong></p>
      <div class="content3" align="center">
      <select class="tt2" name="Title" size="15" multiple="multiple" id="Title">
          <option SELECTED value="null" <?php if (!(strcmp("null", $row_rsTitles['Title']))) {echo "selected=\"selected\"";} ?>>Please select one or more titles</option>
          <?php
do {  
?>
          <option value="<?php echo $row_rsTitles['Title']?>"<?php if (!(strcmp($row_rsTitles['Title'], $row_rsTitles['Title'])))  ?>><?php echo $row_rsTitles['Title']?></option>
          <?php
} while ($row_rsTitles = mysql_fetch_assoc($rsTitles));
  $rows = mysql_num_rows($rsTitles);
  if($rows > 0) {
      mysql_data_seek($rsTitles, 0);
	  $row_rsTitles = mysql_fetch_assoc($rsTitles);
  }
?>
        </select></div>
      <p><strong>By submitting your request to borrow audio books, you are agreeing to Talking Pages <a href="termsandconditions.html">terms and conditions</a> and declaring that you are visually impaired or acting on behalf of a person who is visually impaired.</strong></p>
      <p>&nbsp;</p>
      <p align="center"><input class="btn" name="Submit" type="submit" id="Submit" value="Submit"  />   
        <input class="btn" type="reset" name="reset" id="reset" value="Reset" /></p>
        
      <input type="hidden" name="MM_insert" value="checkout" />
    </form>
    <h1>&nbsp;</h1>
  <!-- InstanceEndEditable --><!-- end .content --></div>
  <div class="footer">
    <ul class="nav">
      <li><a href="index.html">Home</a></li>
      <li><a href="procedures.html">Procedures</a></li>
      <li><a href="newlibrarycards.php">New Library Cards</a></li>
      <li><a href="onlinestatus.php">Online Status</a></li>
      <li><a href="catalog.php">Catalog</a></li>
      <br />
      <li><a href="printerfriendlycatalog.php">Printer-Friendly Catalog</a></li>     
      <li><a href="checkoutlogin.php">Check-Out Login</a></li>
      <li><a href="history.html">History</a></li>
      <li><a href="donations.php">Donations</a></li>
      <li><a href="latestnews.html">Latest News</a></li>
    </ul>
    <!-- end .footer --></div>
<!-- end .container --></div>
</body>
<!-- InstanceEnd --></html>
<?php
mysql_free_result($rsNameInfo);

mysql_free_result($rsTitles);
?>

Open in new window

Author

Commented:
I ended up having to combine what was suggested with some changes, plus additional code. The answers here were helpful, but not complete. I ended up using the hidden variables, but they had to be read in from the database. I created the session variables, but when I tried to write them out to the DB, they appeared as "$_SESSION["whatever'}", rather than the values of the session variables.
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.