Page 1 of 1

Error [solved]

Posted: Wed Feb 29, 2012 11:19 pm
by SpiritWebb
Have one more issue, receiving this:

Undefined index: parse_var on line 18 and line 30. However this is not supposed to run unless something is changed. As soon as I click on the settings link, it tries to run, when it shouldn't and I don't know why. Here is code:

Code: Select all

<?php
session_start();
if(!isset($_SESSION['username'])){
   $msgToUser = "You are not logged in, please log in.<br><br><a href='login.php'>Log In Here</a>";
   include_once "msgToUser.php";
   exit();

} 

include_once 'scripts/connect.php';

$username = $_SESSION['username'];

$error_msg = "";
$success_msg = "";

///// Parsing section for changing the profile picture...will only run if they change this
if($_POST['parse_var'] == "changechar"){                                                      // line 18
	
	$profilepic = $_POST['prochar'];
	$sqlUpdate = mysql_query("UPDATE players SET profilepic='$profilepic' WHERE username='$username'");
	if($sqlUpdate){
		$success_msg = '<font color="#009900">Your profile pic has been updated.</font>';
	} else {
		$error_msg = '<font color="#FFF0000">Unable to update your profile pic at this time, please try again later.</font>';
	}
}

///// Parsing section for changing your bio...will only run if they change this
if($_POST['parse_var'] == "bio"){                                                                   // line 30
	
	$bio = $_POST['bio'];
	$bio = @eregi_replace("'","'",$bio);
	$bio = @eregi_replace("`","'",$bio);
	$bio = mysql_real_escape_string($bio);
	$bio = nl2br(htmlspecialchars($_POST['bio']));
	$sqlUpdate = mysql_query("UPDATE players SET bio='$bio' WHERE username='$username'");
	if($sqlUpdate){
		$success_msg = '<font color="#009900">Your about me has been updated.</font>';
	} else {
		$error_msg = '<font color="#FFF0000">Unable to update your about me at this time, please try again later.</font>';
	}
}

// Final default sql query that wil refresh the member data on page, and show most current
$sql = mysql_query("SELECT * FROM players WHERE username='$username'");

while($row = mysql_fetch_array($sql)){
	
	$username = $row["username"];
	$profilepic = $row["profilepic"];
	$bio = $row["bio"];
	
	
} //Close while loop
if($profilepic == ""){
	$profilepic = "No character";
}
if($profilepic == "option1"){
	$profilepic = "<img src='images/chars/thumbs/option1_sm.png'>";
}
if($profilepic == "option2"){
	$profilepic = "<img src='images/chars/thumbs/option2_sm.png'>";
}
if($profilepic == "option3"){
	$profilepic = "<img src='images/chars/thumbs/option3_sm.png'>";
}
if($profilepic == "option4"){
	$profilepic = "<img src='images/chars/thumbs/option4_sm.png'>";
}
if($profilepic == "option5"){
	$profilepic = "<img src='images/chars/thumbs/option5_sm.png'>";
}
?>
<!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=iso-8859-1" />
<meta name="Description" content="The Re-Awaken" />
<title>Edit Profile | The Re-Awaken</title>
<link href="styles/style.css" rel="stylesheet" type="text/css" />
<link rel="icon" href="#" type="image/x-icon" />
<link rel="shortcut icon" href="#" type="image/x-icon" />
</head>

<body>
<?php include_once "header.php"; ?>
<table width="950" align="center">
  <tr>
    <td width="758" valign="top"><p align="center"><h2>Player Settings - <?php echo $username; ?></h2><div align="right"><a href="game.php">Cancel</a></div><br /><br />
    <?php print "$error_msg"; ?><?php print "$success_msg"; ?>
</p>
      <fieldset>
      <table width="75%" border="0" align="center" cellpadding="7">
        <form action="settings.php" method="post" enctype="multipart/form-data" name="changecar" id="changechar">
          <tr>
            <td width="49%" valign="top"><strong>Choose your character image:</strong><br /><fieldset>Current Character:<?php echo $profilepic; ?></fieldset></td>
            <td width="51%"><center><img src="images/chars/thumbs/option1_sm.png" /><br /><input type="Radio" name="prochar" value="option1" class="formFields"/></center></td>
          </tr>
          <tr>
            <td align="right"><center><img src="images/chars/thumbs/option2_sm.png" /><br /><input type="Radio" name="prochar" value="option2" class="formFields"/></center></td>
            <td><center><img src="images/chars/thumbs/option3_sm.png" /><br /><input type="Radio" name="prochar" value="option3" class="formFields"/></center></td>
          </tr>
          <tr>
            <td><center><img src="images/chars/thumbs/option4_sm.png" /><br /><input type="Radio" name="prochar" value="option4" class="formFields"/></center></td>
            <td><center><img src="images/chars/thumbs/option5_sm.png" /><br /><input type="Radio" name="prochar" value="option5" class="formFields"/></center></td>
          </tr>
          <tr>
            <td> </td>
            <td><div align="right">
            <input name="parse_var" type="hidden" value="changechar" />
            <input type="submit" name="button" id="button" value="Change Appearance" /></div></td>
          </tr>
        </form>
      </table>
      </fieldset>
      <hr align="center" width="100%" />
       <fieldset>
      <table width="75%" border="0" align="center" cellpadding="7">
        <form action="settings.php" method="post" enctype="multipart/form-data" name="bio" id="bio">
          <tr>
            <td width="49%" valign="top"><strong>About <?php echo $username; ?></strong></td>
            <td width="51%"> </td>
          </tr>
          <tr>
            <td align="right"><strong>Character Bio:</strong></td>
            <td>
            <textarea name="bio" cols="" rows="5" class="formFields" id="bio" style="width:94%; resize:none;"><?php print $bio; ?></textarea></td>
          </tr>
          <tr>
            <td> </td>
            <td><div align="right">
            <input name="parse_var" type="hidden" value="bio" />
            <input type="submit" name="button1" id="button1" value="Add/Update Bio" /></div></td>
          </tr>
        </form>
      </table></fieldset></td>
    <td width="180" valign="top">...</td>
  </tr>
</table>

<?php include_once "footer.php"; ?>
</body>
</html>

Re: Error

Posted: Thu Mar 01, 2012 12:20 am
by MikeD
Might need to use

Code: Select all

if (isset($_POST['parse_var']) == "changechar")
Or Rather

Code: Select all

if (isset($_POST['parse_var']))
{
if ($_POST['parse_var'] == "changechar")
{

}
}

Re: Error

Posted: Thu Mar 01, 2012 2:17 am
by SpiritWebb
Thanks MikeD. The second option worked!!

Re: Error [solved]

Posted: Wed Mar 28, 2012 11:28 am
by fang
If you want to test for two different things in the same conditional and one depends on another, you can use the logical and && operator. It short-circuits, so once it hits a false condition it stops evaluating. The following shouldn't throw.

Code: Select all

if (isset($_POST['parse_var']) && $_POST['parse_var'] == "changechar") {
    // ...
}

Re: Error [solved]

Posted: Wed Mar 28, 2012 4:06 pm
by Chris
Fang's method is the proper way to do it.