Jump to content

[SOLVED] Undefined Index?


twilitegxa

Recommended Posts

How do I define the index for the variable $year on the third page of my form? On the second page, year is the value of a function:

 

$year =  yearOfBirth($_POST['birth_date'], $_POST['birth_month'], $_POST['age']);

 

and the values come from the previous page.

 

How do I define the index in order insert it into my table in my database? Here is the insert page:

 

<?php

session_start();

$page='scout.php';
$_SESSION['referer']=$page;

//Access Tracking Snippet

//set up static variables
$page_title = "scout.php";
$user_agent = getenv("HTTP_USER_AGENT");
$date_accessed = date("Y-m-d");

//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
$db = mysql_select_db("smrpg", $conn) or die(mysql_error());

//create and issue query
$sql = "insert into access_tracker values
('', '$page_title', '$user_agent', '$date_accessed')";
mysql_query($sql,$conn);

//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
mysql_select_db("smrpg", $conn) or die(mysql_error());

//create and issue first query; insert values into scouts table
$add_scout = "insert into scouts values ('', now(), '$_SESSION[userName]', '$_SESSION[identity]', '$_SESSION[name]', '$_SESSION[element_of_influence]', '$_SESSION[age]', '$_SESSION[birth_month]', '$_SESSION[birth_date]', '$year', '$_SESSION[blood_type]', '$_SESSION[hobbies]', '$_SESSION[favorite_color]', '$_SESSION[favorite_gemstone]', '$_SESSION[favorite_food]', '$_SESSION[least_favorite_food]', '$_SESSION[favorite_school_subject]', '$_SESSION[least_favorite_school_subject]', '$_SESSION[strengths]', '$_SESSION[weaknesses]', '$_SESSION[mission]', '$_SESSION[goal]', '$_SESSION[biography]', '$_SESSION[height_feet]', '$_SESSION[height_inches]')";
mysql_query($add_scout, $conn) or die(mysql_error());

//get identity from last query
$identity = $_SESSION['identity'];

//create and issue second query; insert values into stats table
$add_stats = "insert into stats values ('', '$identity', '$_POST[bodyv]', '$_POST[mind]', '$_POST[soul]')";
mysql_query($add_stats, $conn) or die(mysql_error());

//create and issue third query; insert values into derived_values table
$add_derived_values = "insert into derived_values values ('', '$identity', '$_POST[health]', '$_POST[energy]', '$_POST[acv1]', '', '$_POST[dcv1]', '', '$_POST[total_cp]')";
mysql_query($add_derived_values, $conn) or die(mysql_error());

//create message for user
$msg = "<p>Stats were added.</p>";
?>
<html>
<head>
<title>New Character Created</title>
<style type="text/css" media="screen">
/*<![CDATA[*/
@import url(global.css); 
/*]]>*/
</style>
</head>
<body>
<!-- HEADER -->
<h1 class="logo">Sailor Moon RPG</h1>
<!-- /HEADER -->
<?php include("topnav.php"); ?>
<div id="main">
<?php include("includes/log.php"); ?>
<?php include("mainnav.php"); ?>
<h1>New Character Created</h1>
<?php print $msg; ?>
</div>
<?php include("bottomnav.php"); ?><!-- FOOTER -->
<!-- FOOTER -->
<div id="footer_wrapper">
<div id="footer">
<p>Sailor Moon and all characters
are<br /> 
trademarks of Naoko Takeuchi.</p>
<p>Copyright © 2009 Liz Kula. All rights reserved.<br />
A product of <a href="#" target="_blank">Web Designs By Liz</a> systems.</p>
<div id="foot-nav">
<ul>
<li><a href="http://validator.w3.org/check?uri=http://webdesignsbyliz.com/digital/index.php" target="_blank"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></li>
<li><a href="http://jigsaw.w3.org/css-validator/validator?uri=http://webdesignsbyliz.com/digital/global.css" target="_blank"><img class="c2" src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" /></a></li>
</ul>
</div>
</div>
</div>
<!-- /FOOTER -->
</body>
</html>

 

Here is the previous page where the $year gets named:

 

<?php

session_start();
if(!isset($_SESSION['loggedIn'])) {
header("Location: login.php");
}

function yearOfBirth ($day, $month, $age)
{
    $now = strtotime('1992-03-23');

    $yob = date('Y', $now) - $age;
    
    if (date('md', $now) < sprintf('%02d%02d', $month, $day)) $yob--;
    
    return $yob;
}

$year =  yearOfBirth($_POST['birth_date'], $_POST['birth_month'], $_POST['age']);

//echo $year;

//register our session variables
session_register('identity');
session_register('name');
session_register('element_of_influence');
session_register('age');
session_register('birth_month');
session_register('birth_date');
session_register ('birth_year');
session_register('blood_type');
session_register('hobbies');
session_register('favorite_color');
session_register('favorite_gemstone');
session_register('favorite_food');
session_register('least_favorite_food');
session_register('favorite_school_subject');
session_register('least_favorite_school_subject');
session_register('strengths');
session_register('weaknesses');
session_register('goal');
session_register('mission');
session_register('biography');
session_register('height_feet');
session_register('height_inches');

//store our posted values in the session variables
$_SESSION['identity'] = $_POST['identity'];
$_SESSION['name'] = $_POST['name'];
$_SESSION['element_of_influence'] = $_POST['element_of_influence'];
$_SESSION['age'] = $_POST['age'];
$_SESSION['birth_month'] = $_POST['birth_month'];
$_SESSION['birth_date'] = $_POST['birth_date'];
$_SESSION['birth_year'] = $year;
$_SESSION['blood_type'] = $_POST['blood_type'];
$_SESSION['hobbies'] = $_POST['hobbies'];
$_SESSION['favorite_color'] = $_POST['favorite_color'];
$_SESSION['favorite_gemstone'] = $_POST['favorite_gemstone'];
$_SESSION['favorite_food'] = $_POST['favorite_food'];
$_SESSION['least_favorite_food'] = $_POST['least_favorite_food'];
$_SESSION['favorite_school_subject'] = $_POST['favorite_school_subject'];
$_SESSION['least_favorite_school_subject'] = $_POST['least_favorite_school_subject'];
$_SESSION['strengths'] = $_POST['strengths'];
$_SESSION['weaknesses'] = $_POST['weaknesses'];
$_SESSION['goal'] = $_POST['goal'];
$_SESSION['mission'] = $_POST['mission'];
$_SESSION['biography'] = $_POST['biography'];
$_SESSION['height_feet'] = $_POST['height_feet'];
$_SESSION['height_inches'] = $_POST['height_inches'];

//Access Tracking Snippet

//set up static variables
$page_title = "stats.php";
$user_agent = getenv("HTTP_USER_AGENT");
$date_accessed = date("Y-m-d");

//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
$db = mysql_select_db("smrpg", $conn) or die(mysql_error());

//create and issue query
$sql = "insert into access_tracker values
('', '$page_title', '$user_agent', '$date_accessed')";
mysql_query($sql,$conn);

?>

<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Sailor Moon RPG - Character Creation Form | Stats</title>
<style type="text/css" media="screen">
/*<![CDATA[*/
@import url(global.css); 
/*]]>*/
</style>
<!-- Script for calculating Derived Values -->
<script language="javascript">

function healthv()
{
var first,sec,third,res1;
//Take the value of first textbox and convert it to float
first=parseFloat(document.forms[0].bodyv.value);
//Take the value of second textbox and convert it to float
sec=parseFloat(document.forms[0].mind.value);
//third
third=parseFloat(document.forms[0].soul.value);
res1=(first+third)*5;
//show the result in the result textbox
document.forms[0].health.value=res1;
}

function energyv()
{
var sec,third,res2;
//Take the value of first textbox and convert it to float
first=parseFloat(document.forms[0].bodyv.value);
//Take the value of second textbox and convert it to float
sec=parseFloat(document.forms[0].mind.value);
//third
third=parseFloat(document.forms[0].soul.value);
res2=(sec+third)*5;
//show the result in the result textbox
document.forms[0].energy.value=res2;
}

function acv()
{
var first,sec,third,res3;
//Take the value of first textbox and convert it to float
first=parseFloat(document.forms[0].bodyv.value);
//Take the value of second textbox and convert it to float
sec=parseFloat(document.forms[0].mind.value);
//third
third=parseFloat(document.forms[0].soul.value);
res3=Math.floor((first+sec+third)/3);
//show the result in the result textbox
document.forms[0].acv1.value=res3;
}

function dcv()
{
var first,sec,third,res4;
//Take the value of first textbox and convert it to float
first=parseFloat(document.forms[0].bodyv.value);
//Take the value of second textbox and convert it to float
sec=parseFloat(document.forms[0].mind.value);
//third
third=parseFloat(document.forms[0].soul.value);
res4=Math.floor((first+sec+third)/3 -2);
//show the result in the result textbox
document.forms[0].dcv1.value=res4;
}
</script>
<!-- End Derived Values Script -->
</head>
<body>
<!-- HEADER -->
<h1 class="logo">Sailor Moon RPG</h1>
<!-- /HEADER -->
<?php include("topnav.php"); ?>
<div id="main">
<?php include("includes/log.php"); ?>
<?php include("mainnav.php"); ?>
<h1>Step 2: Character Outline - Creation</h1>
<h2>Stats And Derived Values</h2>
<form name="cal" method="post" action="insert_scout.php">
<table border="0" cellspacing="0" cellpadding="0">
<tr>
<td><div align="left"><strong>Body:</strong></div></td>
<td><input name="bodyv" type="text" id="bodyv" size="5" maxlength="2" onBlur="acv(), dcv(), healthv(), energyv()" /></td>
</tr>
<tr>
<td><div align="left"><strong>Mind:</strong></div></td>
<td><input name="mind" type="text" id="mind" size="5" maxlength="2" onBlur="acv(), dcv(), healthv(), energyv()" onBlur="acv(), dcv(), healthv(), energyv()" /></td>
</tr>
<tr>
<td><div align="left"><strong>Soul:</strong></div></td>
<td><input name="soul" type="text" id="soul" size="5" maxlength="2" onBlur="acv(), dcv(), healthv(), energyv()" /></td>
</tr>
<tr>
<td></td>
<td> </td>
</tr>
<tr>
<td><div align="left"><strong>Health Points:</strong></div></td>
<td><input name="health" type="text" id="health"readonly="readonly" size="5" maxlength="2" /></td>
</tr>
<tr>
<td><div align="left"><strong>Energy Points:</strong></div></td>
<td><input name="energy" type="text" id="energy" readonly="readonly" size="5" maxlength="2" /></td>
</tr>
<tr>
<td><div align="left"><strong>Attack Combat Value:</strong></div></td>
<td><input name="acv1" type="text" id="acv1" readonly="readonly" size="5" maxlength="2" /></td>
</tr>
<tr>
<td><div align="left"><strong>Defense Combat Value:</strong></div></td>
<td><input name="dcv1" type="text" id="dcv1" readonly="readonly" size="5" maxlength="2" /></td>
</tr>
<tr>
<td> </td>
<td>
<input name="total_cp" type="hidden" id="total_cp" value="10" /></td>
</tr>
<tr>
<td colspan="2"><div align="center"><input name="calcstats" type="submit" id="submit" value="Next" >
<input type="reset" value="Reset" /></div></td>
</tr>
</table>
</form>
</div>
<?php include("bottomnav.php"); ?>
<!-- FOOTER -->
<div id="footer_wrapper">
<div id="footer">
<p>Sailor Moon and all characters are<br>
trademarks of Naoko Takeuchi.</p>
<p>Copyright © 2009 Liz Kula. All rights reserved.<br>
A product of <a href="#" target="_blank">Web Designs By Liz</a> systems.</p>
<div id="foot-nav"><!-- <ul>
<li><a href="http://validator.w3.org/check?uri=http://webdesignsbyliz.com/digital/index.php" target="_blank"><img src="http://www.w3.org/Icons/valid-xhtml10-blue" alt="Valid XHTML 1.0 Transitional" height="31" width="88" /></a></li>
<li><a href="http://jigsaw.w3.org/css-validator/validator?uri=http://webdesignsbyliz.com/digital/global.css" target="_blank"><img class="c2" src="http://jigsaw.w3.org/css-validator/images/vcss-blue" alt="Valid CSS!" /></a></li>
</ul> --></div>
</div>
</div>
<!-- /FOOTER -->
</body>
</html>

 

Can anyone help?

Link to comment
Share on other sites

The two pages you've showed us are separate HTTP requests, and the scripts are executed independly. The second one does not know about the first one, does not know its variables, opened resources etc.

 

The only way to communicate are sessions, so why you don't send the value of $year in it?

 

BTW. When it comes to sessions, you should not use the session_register() function. It is deprecated since PHP 5.3.0 and will be removed in the future releases. Saving a value to $_SESSION array is enough to create a session variable.

Link to comment
Share on other sites

That is what I did, but ti didn't work. Don't I put it on the page that is after the variable is set? Like in this case, the third page that inserts everything?

 

//create and issue query
$sql = "insert into access_tracker values
('', '$page_title', '$user_agent', '$date_accessed')";
mysql_query($sql,$conn);

$_SESSION['year'] = $year;

//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
mysql_select_db("smrpg", $conn) or die(mysql_error());

//create and issue first query; insert values into scouts table
$add_scout = "insert into scouts values ('', now(), '$_SESSION[userName]', '$_SESSION[identity]', '$_SESSION[name]', '$_SESSION[element_of_influence]', '$_SESSION[age]', '$_SESSION[birth_month]', '$_SESSION[birth_date]', '$_SESSION[year]', '$_SESSION[blood_type]', '$_SESSION[hobbies]', '$_SESSION[favorite_color]', '$_SESSION[favorite_gemstone]', '$_SESSION[favorite_food]', '$_SESSION[least_favorite_food]', 

 

This is the part where I saved it in the session on the third page. Did I do something wrong?

Link to comment
Share on other sites

Once again I remind: your third script does not see any variables from the previous requests - they are gone.

 

//create and issue query
$sql = "insert into access_tracker values
('', '$page_title', '$user_agent', '$date_accessed')";
mysql_query($sql,$conn);

$_SESSION['year'] = $year;

 

Why are you trying to initialize this variable in the second page, instead of first? I told you that $year does not exist here, has no value, so you must not use it. Forget about this variable, just focus on the sessions and set the year in the first page, and use it in the second. Simple, I think :).

Link to comment
Share on other sites

On the first page I use the three posts of age, birth_month, and birth_day to get the value for the variable $year. Since it is post data, can I even set this information on the first page? I have it figuring out the value of $year on the second page using a function because the post data doesn't get posted until the first page it submitted. Is there a way to figure this value on the first page??

Link to comment
Share on other sites

Okay, I figured out what I was doing wrong. I have saved the $year variable into a session already on the second page, so I just sent the session into the insert command. I just had it inserting with the wrong name. Thanks for the help everyone!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.