twilitegxa Posted July 21, 2009 Share Posted July 21, 2009 I have an insert statement that is inserting two blank records into my table each time I try to run it. Here is the 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="insert.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="100"></td> </tr> <tr> <td>Character Name:</td> <td><input type="text" name="name" size="30" maxlength="100"></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"></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="100"> </td> </tr> <tr> <td>Favorite Color:</td> <td><input type="text" name="favorite_color" size="10" maxlength="30"> </td> </tr> <tr> <td>Favorite Gemstone:</td> <td><input type="text" name="favorite_gemstone" size="10" maxlength="10"> </td> </tr> <tr> <td>Favorite Food:</td> <td><input type="text" name="favorite_food" size="10" maxlength="30"> </td> </tr> <tr> <td>Least Favorite Food:</td> <td><input type="text" name="least_favorite_food" size="10" maxlength="30"> </td> </tr> <tr> <td>Favorite School Subject:</td> <td><input type="text" name="favorite_school_subject" size="10" maxlength="10"> </td> </tr> <tr> <td>Least Favorite School Subject:</td> <td><input type="text" name="least_favorite_school_subject" size="10" maxlength="10"> </td> </tr> <tr> <td>Strengths:</td> <td><input type="text" name="strengths" size="30" maxlength="100"> </td> </tr> <tr> <td>Weaknesses:</td> <td><input type="text" name="weaknesses" size="30" maxlength="100"> </td> </tr> <tr> <td>Goal:</td> <td><input type="text" name="goal" size="30" maxlength="100"> </td> </tr> <tr> <td>Mission:</td> <td><input type="text" name="mission" size="30" maxlength="100"> </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> Here is the second page (has input saved as sessions): <?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 | Scout</title> <style type="text/css" media="screen"> /*<![CDATA[*/ @import url(global.css); /*]]>*/ </style> <!-- to get derived values --> <script type="text/javascript"> function handle(a,b,c) { var e = document.getElementsByTagName('input'); for (i in e) { if (e[i].name == a) a = e[i]; if (e[i].name == b) b = e[i]; if (e[i].name == c) c = e[i]; } c.value = (Number(a.value) + Number(b.value)) * 5; } </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 action="insert.php" method="post"> <table border="0"> <tr> <td>Body:</td> <td><input type="text" onKeyUp="handle('body','soul','health')" name="body" size="10" maxlength="2"></td> </tr> <tr> <td>Mind:</td> <td><input type="text" onKeyUp="handle('mind','soul','energy')" name="mind" size="10" maxlength="2"></td> </tr> <tr> <td>Soul:</td> <td><input type="text" onKeyUp="handle('body','soul','health')" name="soul" size="10" maxlength="2"></td> </tr> <tr> <td></td> <td> </td> </tr> <tr> <td>Health Points:</td> <td><input type="text" onKeyUp="handle('body','soul')" name="health" size="10" maxlength="3" disabled="true" /></td> </tr> <tr> <td>Energy Points:</td> <td><input type="text" onKeyUp="handle('mind','soul')" name="energy" size="10" maxlength="3" disabled="true" /></td> </tr> <tr> <td>Attack Combat Value:</td> <td><input type="text" name="acv1" size="10" maxlength="3" disabled="true" /> </td> </tr> <tr> <td>(Special Attack)</td> <td><input type="text" name="acv2" size="10" maxlength="3" disabled="true" /> </td> </tr> <tr> <td>Defense Combat Value:</td> <td><input type="text" name="dcv1" size="10" maxlength="30" disabled="true" /> </td> </tr> <tr> <td>(Special Defense)</td> <td><input type="text" name="dcv2" size="10" maxlength="3" disabled="true" /> </td> </tr> <tr> <td>Total Character Points:</td> <td><input type="text" name="total_cp" size="10" maxlength="3" value="10" disabled="true" /> </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 --> <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> And here is the insert page: <?php session_start(); ?> <?php //Access Tracking Snippet //set up static variables $page_title = "insert.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); ?> <?php $identity = $_SESSION['identity']; $con = mysql_connect("localhost","root",""); if (!$con) { die('Could not connect: ' . mysql_error()); }mysql_select_db("smrpg", $con); $sql="INSERT INTO scouts (id, create_time, username, identity, name, element_of_influence, age, birth_month, birth_date, birth_year, height_feet, height_inches, blood_type, hobbies, favorite_color, favorite_gemstone, favorite_food, least_favorite_food, favorite_school_subject, least_favorite_school_subject, strengths, weaknesses, goal, mission, biography) VALUES ('', now(), '$_SESSION[userName]', '$_SESSION[identity]','$_SESSION[name]','$_SESSION[element_of_influence]','$_SESSION[age]','$_SESSION[birth_month]','$_SESSION[birth_date]','$_SESSION[birth_year]','$_SESSION[height_feet]','$_SESSION[height_inches]','$_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[goal]','$_SESSION[mission]','$_SESSION[biography]')"; if (!mysql_query($sql,$con)) { die('Error: ' . mysql_error()); } $display_block = "Your character has been successfully created! Redirecting to Your Account Page..."; mysql_close($con) ?> <html> <head> <title>Sailor Moon RPG - Character <?php echo $identity; ?> Created Successfully</title> <!-- <meta HTTP-EQUIV="REFRESH" content="2; url=http://localhost/account.php"> --> <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>Character <?php echo $identity; ?> Created Successfully</h1> <?php echo $display_block; ?> </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? Quote Link to comment Share on other sites More sharing options...
trq Posted July 21, 2009 Share Posted July 21, 2009 Each of those pages has the same insert query in it. Mind narrowing down which one you think is the problematic one? While your at it, echo your query to see what it contains before executing it, this is always good for debuging. Complex variables (such as arrays) should be surrounded by {} when within strings. So something like.... '$_SESSION[identity]' should be.... '{$_SESSION['identity']}' Having said that however, its dangerous to place variables like that straight into queries. You've made no attempt to sanitize your data, take a look at mysql_real_escape_string. One last thing, session_register has been depricated and should no longer be used. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 21, 2009 Author Share Posted July 21, 2009 The query I'm having problems with is on the third page: $insert = "INSERT INTO scouts (id, create_time, username, identity, name, element_of_influence, age, birth_month, birth_date, birth_year, height_feet, height_inches, blood_type, hobbies, favorite_color, favorite_gemstone, favorite_food, least_favorite_food, favorite_school_subject, least_favorite_school_subject, strengths, weaknesses, goal, mission, biography) VALUES ('', now(),'$_SESSION[userName]','$_SESSION[identity]','$_SESSION[name]','$_SESSION[element_of_influence]','$_SESSION[age]','$_SESSION[birth_month]','$_SESSION[birth_date]','$_SESSION[birth_year]','$_SESSION[height_feet]','$_SESSION[height_inches]','$_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[goal]','$_SESSION[mission]','$_SESSION[biography]')"; if (!mysql_query($insert,$con)) { die('Error: ' . mysql_error()); } This statement originally said $sql, but as you pointed out, there was another query using this, so I changed it to $insert, but that didn't solve the problem, obviously. I'm not quite sure how to use the mysql_real_escape_string on my queries. I read the page from your link, but it didn't help me understand how to use it. :-( You said that the session_register is deprecated. Does that mean I shoudl just remove those lines using session_register? Will me session variables still be set as I have them written now? (Example: $_SESSION['identity'] = $_POST['identity'] You said complex variables should eb surrounded by curly brackets. Does that mean all instances of your example ('$_SESSION[identity]') should be changed to use the brackets? Sorry I ask so many questions. I am really struggling to understand. Quote Link to comment Share on other sites More sharing options...
trq Posted July 21, 2009 Share Posted July 21, 2009 This statement originally said $sql, but as you pointed out, there was another query using this, so I changed it to $insert, but that didn't solve the problem, obviously. Obviously that would have no effect. I'm not quite sure how to use the mysql_real_escape_string on my queries. I read the page from your link, but it didn't help me understand how to use it. :-( $foo = mysql_real_escape_string($foo); $foo is now safe (reasonably) to use within a database query. You said complex variables should eb surrounded by curly brackets. Does that mean all instances of your example ('$_SESSION[identity]') should be changed to use the brackets? Yes. Why stop at one? Now, have you tried echo'ing your query ($insert) as I suggested? What does it output? Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 21, 2009 Author Share Posted July 21, 2009 I don't know how to do that. Quote Link to comment Share on other sites More sharing options...
trq Posted July 21, 2009 Share Posted July 21, 2009 echo $insert Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted July 21, 2009 Author Share Posted July 21, 2009 Oh, well, then I do know how to do that, but it is just returning the actual insert statement: INSERT INTO scouts (id, create_time, username, identity, name, element_of_influence, age, birth_month, birth_date, birth_year, height_feet, height_inches, blood_type, hobbies, favorite_color, favorite_gemstone, favorite_food, least_favorite_food, favorite_school_subject, least_favorite_school_subject, strengths, weaknesses, goal, mission, biography) VALUES ('', now(),'twilitegxa','','','','','','','1992','','','','','','','','','','','','','','','') Quote Link to comment Share on other sites More sharing options...
trq Posted July 21, 2009 Share Posted July 21, 2009 Thats what its meant to return. Now you can see your sql statement as your database would receive it. Notice all the empty values? Theres your problem. Have you removed those calls to session_register yet? It appears your $_SESSION array is empty. Quote Link to comment Share on other sites More sharing options...
twilitegxa Posted August 23, 2009 Author Share Posted August 23, 2009 For some reason Firefox seems to have been the problem. Apparently an add-on was causing the problem and when I removed it, the problem stopped. Thanks for all the help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.