Jump to content

[SOLVED] Why Inserting A Blank Record?


twilitegxa

Recommended Posts

I tried my form a minute ago, and it submitted everything perfectly. Now I try it a second time, and it is inserting two records, one completely blank and one with the stats (from the second page of the form) but without inserting the identity field. The only thing that gets inserted in both table sis the username. Here is my script:

 

first page of form:

<?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);
?>

<!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 | Scout</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"); ?>
<form action="stats.php" method="post">
<h1>Step 2: Character Outline - Creation</h1>
<h2><u>Scout Profile</u></h2>
<table border="0">
  <tr>
    <td>Identity:</td>
    <td><input type="text" name="identity" size="30" maxlength="200"></td>
  </tr>

  <tr>
    <td>Character Name:</td>
    <td><input type="text" name="name" size="30" maxlength="200"></td>
  </tr>

  <tr>
    <td>Element Of Influence:</td>
    <td>

    <SELECT NAME="element_of_influence" SIZE="1">
    <OPTION value="Purity And The Soul">Purity/Soul</OPTION>
    <OPTION value="Water">Water</OPTION>
    <OPTION value="Fire">Fire</OPTION>
    <OPTION value="Wood/Lightning">Wood/Lightning</OPTION>
    <OPTION value="Love/Metal">Love/Metal</OPTION>

    <OPTION value="Earth">Earth</OPTION>
    <OPTION value="Time">Time</OPTION>
    <OPTION value="Death And Healing">Death/Healing</OPTION>
    <OPTION value="Light">Light</OPTION>
    </SELECT> <a href="elements.php" title="Help!"><img src="question.gif" /></a></td>
  </tr>

<tr>
    <td>Age:</td>
    <td><input type="text" name="age" size="2" maxlength="2" onChange="return checkForNumber(this.value);"></td>
  </tr>
    <tr>
    <td>Date Of Birth:</td>
    <td><SELECT NAME="birth_month" SIZE="1">
    <OPTION value="01">January</OPTION>
    <OPTION value="02">February</OPTION>
    <OPTION value="03">March</OPTION>
    <OPTION value="04">April</OPTION>
    <OPTION value="05">May</OPTION>
    <OPTION value="06">June</OPTION>
    <OPTION value="07">July</OPTION>
    <OPTION value="08">August</OPTION>
    <OPTION value="09">September</OPTION>
    <OPTION value="10">October</OPTION>
    <OPTION value="11">November</OPTION>
    <OPTION value="12">December</OPTION>
    </SELECT>

    <SELECT NAME="birth_date" SIZE="1">
    <OPTION value="01">1</OPTION>
    <OPTION value="02">2</OPTION>
    <OPTION value="03">3</OPTION>
    <OPTION value="04">4</OPTION>
    <OPTION value="05">5</OPTION>
    <OPTION value="06">6</OPTION>
    <OPTION value="07">7</OPTION>
    <OPTION value="08">8</OPTION>
    <OPTION value="09">9</OPTION>
    <OPTION value="10">10</OPTION>
    <OPTION value="11">11</OPTION>
    <OPTION value="12">12</OPTION>
    <OPTION value="13">13</OPTION>
    <OPTION value="14">14</OPTION>
    <OPTION value="15">15</OPTION>
    <OPTION value="16">16</OPTION>
    <OPTION value="17">17</OPTION>
    <OPTION value="18">18</OPTION>
    <OPTION value="19">19</OPTION>
    <OPTION value="20">20</OPTION>
    <OPTION value="21">21</OPTION>
    <OPTION value="22">22</OPTION>
    <OPTION value="23">23</OPTION>
    <OPTION value="24">24</OPTION>
    <OPTION value="25">25</OPTION>
    <OPTION value="26">26</OPTION>
    <OPTION value="27">27</OPTION>
    <OPTION value="28">28</OPTION>
    <OPTION value="29">29</OPTION>
    <OPTION value="30">30</OPTION>
    <OPTION value="31">31</OPTION>
    </SELECT> </td>
  </tr>
  <tr>
    <td>Height:</td>
    <td><input type="text" name="height_feet" size="2" maxlength="2"> feet <input type="text" name="height_inches" size="2" maxlength="2"> inches</td>

  </tr>
  <tr>
    <td>Blood Type:</td>
    <td><input type="text" name="blood_type" size="4" maxlength="4"> <a href="bloodtype.php" title="Help!"><img src="question.gif" /></a>    </td>
  </tr>
  <tr>
    <td>Hobbies:</td>

    <td><input type="text" name="hobbies" size="30" maxlength="200">
    </td>
  </tr>
  <tr>
    <td>Favorite Color:</td>
    <td><input type="text" name="favorite_color" size="10" maxlength="50">
    </td>
  </tr>

  <tr>
    <td>Favorite Gemstone:</td>
    <td><input type="text" name="favorite_gemstone" size="10" maxlength="50">
    </td>
  </tr>
  <tr>
    <td>Favorite Food:</td>
    <td><input type="text" name="favorite_food" size="10" maxlength="50">

    </td>
  </tr>
  <tr>
    <td>Least Favorite Food:</td>
    <td><input type="text" name="least_favorite_food" size="10" maxlength="50">
    </td>
  </tr>
  <tr>

    <td>Favorite School Subject:</td>
    <td><input type="text" name="favorite_school_subject" size="10" maxlength="50">
    </td>
  </tr>
  <tr>
    <td>Least Favorite School Subject:</td>
    <td><input type="text" name="least_favorite_school_subject" size="10" maxlength="50">
    </td>

  </tr>
  <tr>
    <td>Strengths:</td>
    <td><input type="text" name="strengths" size="30" maxlength="200">
    </td>
  </tr>
  <tr>
    <td>Weaknesses:</td>

    <td><input type="text" name="weaknesses" size="30" maxlength="200">
    </td>
  </tr>
  <tr>
    <td>Goal:</td>
    <td><input type="text" name="goal" size="30" maxlength="200">
    </td>
  </tr>
<tr>
    <td>Mission:</td>
    <td><input type="text" name="mission" size="30" maxlength="200">
    </td>
  </tr>
  <tr>
    <td>Character Biography:</td>
    <td><textarea name="biography" rows=7 
cols=46></textarea>
    </td>
  </tr>
</table>
<p style="text-align: center" class="submit"><input type="submit" value="Create Character" />
<input type="reset" value="Reset" /></p>

</form>

</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>

 

second page of form:

<?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']);

//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="stats" 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()" /></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>

 

submit page, inserts into table:

<?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");

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($_SESSION['birth_date'], $_SESSION['birth_month'], $_SESSION['age']);

//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);

$_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[birth_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>Character, Stats, and Derived Values 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>

 

What am I doing wrong???

Link to comment
Share on other sites

Sorry I haven't actually looked at your problem, but I noticed in your second file you have under the comment:

 

//store our posted values in the session variables

 

a whole lot of $_SESSION value declarations assigned from $_POST

Here's a hot, 2 line, tip:

 

<?php
foreach ($_POST as $key => $value)
    $_SESSION[$key] = $value;
?>

Link to comment
Share on other sites

it loops through each index of the $_POST array, getting the index name ($key) and that index's value ($value) and assigns each key name and its value to the $_SESSION array.

 

It is basically copying all of the variables in $_POST over to $_SESSION.

 

If you ever add or remove indexes from $_POST you wouldnt have to modify the

"//store our posted values in the session variables" section of your second file. And if you make new scripts that copy everything from $_POST to $_SESSION you can just copy and paste that foreach loop into the new script or even put it in a template file for your future projects.

Link to comment
Share on other sites

  • 2 weeks later...
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.