[SOLVED]Updating table, using SESSION to choose record

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

[SOLVED]Updating table, using SESSION to choose record

Post by Sinemacula »

I'm creating a two part form that inputs the data into a mysql database table. I thought that to do this I would need to set a SESSION variable that would be the same as one of the database table fields (that would have a unique value) from the first page of the form, and then use that SESSION variable in the WHERE clause to make sure the correct record was updated (ie. had the second part of the form data added).

However, what I've done isn't working, and I'm having trouble figuring out why. Here's my code, in order:
The first page of the form:

Code: Select all

<html>
<head>
<title>Initial Questions-Part 1</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#333399">
<table width="600" border="0" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#C5C5EB" align="center">
  <tr>
    <td width="19"> </td>
    <td width="558"> 
      
    </td>
    <td width="17"> </td>
  </tr>
  <tr>
    <td width="19"> </td>
    <td width="558"> 
      <p> Following is a series of questions that will determine if you qualify 
        for the preliminary part of this study. Please answer all questions by 
        clicking the cursor in the "Yes" or "No" box adjacent to the question. 
        When you have answered all the questions click the "Continue" button at 
        the bottom of the page. 
      <p align="center"><b>Initial Qualifying Questionnaire</b> <br>
      <hr>
    </td>
    <td width="17"> </td>
  </tr>
  <tr>
    <td width="19" height="240"> </td>
    <td width="558" height="240"> 
      <form id="PreQual1" action="initial-q1.php" method="post" name="PreQual1"><p align="left"><b>
	  1.) Are you 25 years of age or older? 
	  <br>
								<input type="radio" name="age" value="1">Yes
	  <input type="radio" name="age" value="0">No
	  </b>
						<p><b>
      2.) Did your apparitional encounter experience take place 
        before the age of 13 years old? 
	  <br>
								<input type="radio" name="before13" value="1">Yes
	  <input type="radio" name="before13" value="0">No
	  </b><p><b>
      3.) Were you in a normal waking state going about your 
        daily activities when you experinced your religious apparitional encounter? 
		<br>
								<input type="radio" name="waking" value="1">Yes
	  <input type="radio" name="waking" value="0">No
		</b><p><b>4.) Were you free of mind altering substances (drugs, alcohol, marijuana, etc.) when you experinced your religious apparitional encounter?<br>
								<input type="radio" name="substancefree" value="1">Yes
	  <input type="radio" name="substancefree" value="0">No
		</b>
						<p><b>
      5.) Was your experience an actual densely present human-like 
        apparitional encounter with a historical Jewish or Christian religious 
        figure? 
		<br>
								<input type="radio" name="humanlike" value="1">Yes
	  <input type="radio" name="humanlike" value="0">No
		</b>
						<div align="center">
							<p><input type="Submit" name="Submit" value="Continue"></p>
						</div>
					</form></td>
    <td width="17" height="240"> </td>
  </tr>
  <tr>
    <td width="19"> </td>
    <td width="558"> 
      <div align="center">
        
      </div>
    </td>
    <td width="17"> </td>
  </tr>
  <tr>
    <td width="19"> </td>
    <td width="558"> </td>
    <td width="17"> </td>
  </tr>
</table>
</body>
</html>
The php script that processes that form:

Code: Select all

<?php require "db_connect.php";  //this gets the username, host, dbname, password variables to connect to the database

session_start();

//we check to make sure this page was accessed by someone hitting the submit button
if(isset($_POST['Submit'])) {

//need to reset variables to take account of globals being off

$age = $_POST['age'];
$before13 = $_POST['before13'];
$waking = $_POST['waking'];
$substancefree = $_POST['substancefree'];
$humanlike = $_POST['humanlike'];
$date = date('Y-m-d H:i:s');

//set the cookie using the datetime
$_SESSION['qualify'] = $date;

//This is where we connect to the database

$db = mysql_pconnect($db_host, $db_user, $db_pass);
	if (!$db) { 
	
//this gives a warning if we can't connect to the database, and then exits

echo( "<p>Unable to connect to the " . 
"database server at this time.</p>" ); 
exit(); 
    }
    
//this is the statement that connects to the database table and inserts the new values
//first we do the selecting of the database
//then we define the command with $sql -- telling it to INSERT data into the table fields listed
//and then telling it what data to put in those fields by calling on the POSTed variables from the form

mysql_select_db($db_name, $db);
	$sql_qualify = ("INSERT INTO qualify (
	age,
	before13,
	waking,
	substancefree,
	humanlike,
	date)
	VALUES (
	'$age',
	'$before13',
	'$waking',
	'$substancefree',
	'$humanlike',
	'$date');");
	$result_qualify=mysql_query($sql_qualify,$db) or die("<P>Query failed: ".mysql_error()); //then we tell it to perform the instructions, or give an error message

//then we make sure that all questions were answered Yes - if not then redirect to not qualified page
if($_POST['age'] != 1) {
header('Location: not-qualified.html');
exit();
    }
if($_POST['before13'] != 1) {
header('Location: not-qualified.html');
exit();
    }
if($_POST['waking'] != 1) {
header('Location: not-qualified.html');
exit();
    }
if($_POST['substancefree'] != 1) {
header('Location: not-qualified.html');
exit();
    }
if($_POST['humanlike'] != 1) {
header('Location: not-qualified.html');
exit();
    }
	
//Made it through the initial qualifying -  redirect to the second part of the prequalification questionnaire
header('Location: initial-q2.html');
exit();

//if this page was reached without hitting the submit button on the form, give an error message
} else {
echo "You did not submit the associated form... you should not be here.";
}
?>
The second form:

Code: Select all

&lt;html&gt;
&lt;head&gt;
&lt;title&gt;Initial Questions-Part 2&lt;/title&gt;
&lt;meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"&gt;
&lt;/head&gt;

&lt;body bgcolor="#333399"&gt;
&lt;table width="600" border="0" cellspacing="0" bordercolor="#FFFFFF" bgcolor="#C5C5EB" align="center"&gt;
  &lt;tr&gt;
    &lt;td width="21"&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td width="556"&gt; 
      &lt;form name="form1" &gt;
        &lt;p&gt;&amp;nbsp;&lt;/p&gt;
        &lt;/form&gt;
    &lt;/td&gt;
    &lt;td width="17"&gt;&amp;nbsp;&lt;/td&gt;
  &lt;/tr&gt;
 &lt;form id="PreQual2" action="initial-q2.php" method="post" name="PreQual2"&gt;
   &lt;tr&gt;
    &lt;td width="21"&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td width="556"&gt; 
      &lt;p&gt;&lt;b&gt;6.&lt;/b&gt;) &lt;b&gt;Are you currently under psychiatric care?&lt;/b&gt; &lt;br&gt;
        &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Yes 
        &lt;input type="radio" name="psych" value="1"&gt;
        No 
        &lt;input type="radio" name="psych" value="0"&gt;
      
      &lt;p&gt;&lt;b&gt;7.) Are you currently taking antipsychotic medication? &lt;/b&gt; &lt;br&gt;
        &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Yes 
        &lt;input type="radio" name="meds" value="1"&gt;
        No 
        &lt;input type="radio" name="meds" value="0"&gt;
      &lt;p&gt;&lt;b&gt;8.) Are you pregnant, or have you given birth in the last 6 months? 
        &lt;/b&gt; &lt;br&gt;
        &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Yes 
        &lt;input type="radio" name="pregnant" value="1"&gt;
        No 
        &lt;input type="radio" name="pregnant" value="0"&gt;
      &lt;p&gt;&lt;b&gt;9.) Have you had surgery or serious illness within the last 6 months? 
        &lt;/b&gt; &lt;br&gt;
        &amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;Yes 
        &lt;input type="radio" name="surgery" value="1"&gt;
        No 
        &lt;input type="radio" name="surgery" value="0"&gt;
      &lt;/td&gt;
    &lt;td width="17"&gt;&amp;nbsp;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td width="21"&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td width="556"&gt; 
      &lt;div align="center"&gt;
        &lt;input type="Submit" name="Submit" value="Continue"&gt;
      &lt;/div&gt;
    &lt;/td&gt;
    &lt;td width="17"&gt;&amp;nbsp;&lt;/td&gt;
  &lt;/tr&gt;&lt;/form&gt;
  &lt;tr&gt;
    &lt;td width="21"&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td width="556"&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td width="17"&gt;&amp;nbsp;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;tr&gt;
    &lt;td width="21"&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td width="556"&gt;&amp;nbsp;&lt;/td&gt;
    &lt;td width="17"&gt;&amp;nbsp;&lt;/td&gt;
  &lt;/tr&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
The php script that processes the second form, where I want the data added to the same record as the first part:

Code: Select all

<?php require "db_connect.php";  //this gets the username, host, dbname, password variables to connect to the database

//we check to make sure this page was accessed by someone hitting the submit button
if(isset($_POST['Submit'])) {

//need to reset variables to take account of globals being off

$psych = $_POST['psych'];
$meds = $_POST['meds'];
$pregnant = $_POST['pregnant'];
$surgery = $_POST['surgery'];


//This is where we connect to the database

$db = mysql_pconnect($db_host, $db_user, $db_pass);
	if (!$db) { 
	
//this gives a warning if we can't connect to the database, and then exits

echo( "<p>Unable to connect to the " . 
"database server at this time.</p>" ); 
exit(); 
    }
    
//this is the statement that connects to the database table and inserts the new values
//first we do the selecting of the database
//then we define the command with $sql -- telling it to INSERT data into the table fields listed
//and then telling it what data to put in those fields by calling on the POSTed variables from the form

mysql_select_db($db_name, $db);
	$sql_morequalify = (UPDATE `qualify` SET `psych`=$psych, `meds`=$meds, `pregnant`=$pregnant, `surgery`=$surgery WHERE `date`='".$_SESSION['qualify']."');
	$result_morequalify=mysql_query($sql_morequalify,$db) or die("<P>Query failed: ".mysql_error()); //then we tell it to perform the instructions, or give an error message

//then we make sure that all questions were answered Yes - if not then redirect to not qualified page
if($_POST['psych'] != 1) {
header('Location: not-qualified.html');
exit();
    }
if($_POST['meds'] != 1) {
header('Location: not-qualified.html');
exit();
    }
if($_POST['pregnant'] != 1) {
header('Location: not-qualified.html');
exit();
    }
if($_POST['surgery'] != 1) {
header('Location: not-qualified.html');
exit();
    }
    	
//Made it through the initial qualifying -  redirect to the second part of the prequalification questionnaire
header('Location: cont2consent1.html');
exit();

//if this page was reached without hitting the submit button on the form, give an error message
} else {
echo "You did not submit the associated form... you should not be here.";
}
?>
I'm not sure if I needed to post all of that, but since I don't know where the problem lies, I thought maybe I should.

Having said that, the error I'm getting is in the second php script, at the "$sql_morequalify =" line -- a parse error. But I don't know whether that's a problem with the sytax of that line, or the SESSION variable.

Any ideas?

Thanks,
Scott
Sinemacula
Forum Contributor
Posts: 110
Joined: Sat Feb 08, 2003 2:36 am
Location: San Jose, CA

Post by Sinemacula »

Well, after reading many more posts (I had already spent quite some time on here) I found what I was missing.

I had not added session_start(); to the top of the second php page. Adding that fixed the problem.
Post Reply