Jump to content

I need help with some PHP that is acting very funny.


alphasynaptic

Recommended Posts

Ok I have written a php script for profiles on my site. The problem is that [code]$pic[/code] is working correctly when used as the source for an image but it does not work when I try to use it as the default value for a form field. [code]$pic[/code], when echoed anywhere except in the clause where it is used as an image source returns absolutely nothing. Would somebody please tell me what is wrong with my code? I would highly appreciate it if anyone desiring to say that my code is messy or to suggest alternate ways to do things keep their opinions to themselves. I have tried to ask for help with php on other boards before and have gotten answers completely irrelevant to the problem I was asking assistance for. All of the $_GET, $_POST, and $_SESSION variables are set before the script is accessed.
[code]
<?php
$id = $_GET['id'];
$user = mysql_query("SELECT * FROM users WHERE id='$id'");
$character = mysql_query("SELECT * FROM characters WHERE id='$id'");
list($id, $username, $password, $email, $pic) = mysql_fetch_row($user);
list($id, $name, $class, $level, $experience, $money) = mysql_fetch_row($character);
switch ($class)
{
case 1:
$class = $c1;
break;
case 2:
$class = $c2;
break;
case 3:
$class = $c3;
break;
case 4:
$class = $c4;
break;
}
if($_GET['edit']=="true")
{echo"$pic";
if (isset($_POST['change']))
{
$username = $_SESSION['username'];
$newemail = $_POST['email'];
$newpic = strip_tags($_POST['pic']);
$newpass = strip_tags($_POST['newpassword']);
$newpass2 = strip_tags($_POST['newpassword2']);
$oldpass = md5(strip_tags($_POST['oldpassword']));
$oldpass2 = md5(strip_tags($_POST['oldpassword2']));
$userinfo = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$oldpass'");
list($id, $username, $password, $email, $pic) = mysql_fetch_row($userinfo);
if ($oldpass == $oldpass2)
{
if ($oldpass == $password)
{
if ($newpass!="")
{
if ($newpass == $newpass2)
{
$newpass = md5($newpass);
mysql_query("UPDATE users SET users.password='$newpass' WHERE users.username='$username'");
echo "<center>Password updated.</center>";
}
else
{
echo "<center>New passwords don't match. <a href='index.php?act=profile&edit=true'>Try Again?</a></center>";
}
}
if ($newemail!="")
{
if (preg_match("/.*@.*..*/", $newemail) | preg_match("/(<|>)/", $newemail))
{
mysql_query("UPDATE users SET users.email='$newemail' WHERE users.username='$username'");
echo "<center>Email updated.</center>";
}
else
{
echo "<center>Not a vail E-mail address. <a href='index.php?act=profile&edit=true'>Try Again?</a></center>";
}
}
if (ereg("image", $newpic))
{
mysql_query("UPDATE users SET users.pic='$newpic' WHERE users.username='$username'");
echo "<center>Profile picture updated.</center>";
}
else
{
echo "<center>Not a vail picture file. <a href='index.php?act=profile&edit=true'>Try Again?</a></center>";
}
}
else
{
echo "<center>Wrong password! <a href='index.php?act=profile&edit=true'>Try Again?</a></center>";
}
}
else
{
echo "<center>Your old passwords did not match. <a href='index.php?act=profile&edit=true'>Try Again?</a></center>";
}
}
else
{
echo "<table cellspacing='1' cellpadding='0' border='1' width='500' bordercolor='black' align='center'>
<tr><td><center>Edit</center></td></tr>
<tr><td><form action='index.php?act=profile&edit=true' method='POST'>
Profile Pic: <input type='text' name='pic' size='25' maxlength='100' value='$pic'><br>
New E-mail Address:  <input type='text' name='email' size='25' maxlength='50'><br>
New Password: <input type='password' name='newpassword' size='10' maxlength='10'><br>
Repeat New Password: <input type='password' name='newpassword2' size='10' maxlength='10'><br>
Old Password: <input type='password' name='oldpassword' size='10' maxlength='10'><br>
Repeat Old Password: <input type='password' name='oldpassword2' size='10' maxlength='10'>
<input type='submit' name='change' value='Change Info'>
</form></td></tr></table>";
}
}
else
{
echo "<table cellspacing='1' cellpadding='0' border='1' width='500' bordercolor='black' align='center'>
<tr>
<td colspan='3'><center>$name ";
if ($_SESSION['id']==$id)
{
echo '<a href="index.php?act=profile&edit=true">Edit</a>';
}
echo "</center></td>
</tr>
<tr>
<td width='50%' rowspan='4'><img src='$pic'></img></td>
<td>E-mail</td>
<td>$email</td>
</tr>
<tr>
<td>Class</td>
<td>$class</td>
</tr>
<tr>
<td>Level</td>
<td>$level</td>
</tr>
<tr>
<td width='5%'>Money</td>
<td width='45%'>$money</td>
</tr></table>";
}
?>
[/code]
People who are adverse to receiving constructive criticism are either insecure, arrogant or both. IMHO, these forums are about the exchange of ideas and information. get over it.

Anyway, I'm pretty sure I see the problem. You have two different places that $pic can be defined. Both following different database queries. The $pic you use for the img tag is defined using the query at the top of the page. the $pic you use for the input field is defined using the query within the [b]if($_GET['edit']=="true")[/b] section. I bet that the 2nd query is not returning the values you expect.

For debugging purposes, run these line right after the userinfo query instead of the list function and you will see if what is returned in the query is what you expect.
[code]<?php
if (mysql_num_rows($userinfo)) {
  echo "there were no results.";
} else {
    $row = mysql_fetch_row($userinfo);
    foreach ($row as $key => $value) {
        echo $key . ": " . $value . "<br>";
    }
}
?>[/code]
I only stated that those having unrelated comments should keep them to themselves for the fact of what they are: unrelated to my question. The last time I used a forum for help everyone was telling me different ways I could do the same things within my code. Nobody was answering my question. Therefore they were "spamming" my topic. I appreciate your response but I would hope that someone with so many posts knows the difference between spam and general input. That's beside the topic though. If I hear one more person talking about how I should accept unrelated comments they will be reported for spamming. I do appreciate relevant comments though. Your code echoed neither "there were no results." nor the "key:value".
There was a mistake in that code The if statement should have an exclamation point:
[code]if (!mysql_num_rows($userinfo)) {[/code]

Because of that mistake and because you did not get any output, it means your query, as I suspected, is returning no records.

And yes, I do know the difference between spam and general input, but apparently you don't. Go ahead and report me for spamming, but offering constructive criticism for someones code is not spamming. This isn't Burger King, you don't "get it your way". It's a forum: An online discussion group, where participants with common interests can exchange open messages. Open being the operative word.
Close this topic. I have solved my problem. When $_GET['edit'] = 'true' , the $_GET['id'] was no longer set so the queries at the top of the page returned nothing. I see constructive suggestions as being constructive ONLY when they are desired. Let's quit being immature about this as another response from you will cause me to report you as spamming as this topic is no longer valid.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.