Variable Mysteriously Stops Echoing

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Smudly
Forum Commoner
Posts: 71
Joined: Wed Jun 09, 2010 10:09 pm

Variable Mysteriously Stops Echoing

Post by Smudly »

Hi,

I'm working on a page that GETs a value that is passed in the URL. This variable echos correctly up until I try echoing it inside an if statment where it needs to be.

The variable is called $siteid. Inside my if statement I have a query that runs WHERE id='$siteid'

Inside this if statement it won't echo out. It is the weirdest thing. I tried to hardcode the siteid as 95, to test it, and the query updated my database as it should. However, leaving it as the variable it will not work. Any help solving this mystery that I've been banging my head over for hours would be greatly appreciated!

My variable echos correctly up until the line that says:
// $siteid will Echo up until here. After this point it stops working.

The query I am having issues with is after the line that says:
// Problem lies in this query.

My SQL Table:
id int(11) No
userid int(11) No
url varchar(2083) No
credits int(11) No 0
dailyviews int(11) No 0
todayviews int(11) No 0
totalviews int(11) No
active tinytext No
title varchar(50) No
confirmed tinytext No

And my code:

Code: Select all

<?php
session_start();

if (isset($_SESSION['username'])){
$loginstatus = "logout";

$username = $_SESSION['username'];

include_once('inc/connect.php');

$siteid = $_GET['siteid'];
$message = "messagebox";
$success = "&nbsp;";
$error = "&nbsp;";

$useridquery = "SELECT id FROM users WHERE username='$username'";
$useridresult = mysql_query($useridquery);
$useridrow = mysql_fetch_assoc($useridresult);

$userid = $useridrow['id'];

$result1 = mysql_query("SELECT * FROM websites WHERE `id`='$siteid' && userid='$userid'"); 
$siterow1 = mysql_fetch_assoc($result1);
$titledb = $siterow1['title'];
$urldb = $siterow1['url'];
$maxviewsdb = $siterow1['dailyviews'];
$statusdb = $siterow1['active'];

$titlenew = $_POST['title'];
$urlnew = $_POST['url'];
$maxviewsnew = $_POST['maxviews'];
$statusnew = $_POST['status'];

if($statusnew=="Active"){
    $statusnew = "yes";
}
else{
    $statusnew = "no";
}

$editnewsite = $_POST['editnewsite'];

if($success!=""){
$message = "";
}
else{
$message = "messagesuccess";
}
// $siteid will Echo up until here. After this point it stops working.
if($editnewsite){
    if($titlenew){
        if($urlnew){
            if($maxviewsnew){
            
            function valid_url($urlnew)
{
    return ( ! preg_match('/^(http|https|ftp):\/\/([A-Z0-9][A-Z0-9_-]*(?:\.[A-Z0-9][A-Z0-9_-]*)+):?(\d+)?\/?/i', $urlnew)) ? FALSE : TRUE;
}
if(valid_url($urlnew)){
            
              //Check if URL is a Duplicate for current user            
            $results = mysql_query("SELECT * FROM `websites` WHERE `userid`='$userid' AND `url`='$url'");
                        $rows = mysql_num_rows($results);


            if ($rows<=0)
            {
                                $siteidnew = $siteid;
                                // Problem lies in this query.
                                $update = "UPDATE `websites` SET `title`='$titlenew', `url`='$urlnew', `dailyviews`='$maxviewsnew', `active`='$statusnew' WHERE `id`='$siteid' && `userid`='$userid'";
                                mysql_query($update);

                                $titledb = $titlenew;
                                $urldb = $urlnew;
                                $maxviewsdb = $maxviewsnew;
                                $statusdb = $statusnew;
                                echo $titledb;
                                echo $urldb.$maxviewsdb.$statusdb."<br>".$siteid;
                                // PROBLEM HERE
                                    
                                    
                                exit();
                 
            }
            else{
                $error = "You have already submitted that site";
                $message = "messageerror";
            }

        } // here
    else {
        $error = "Invalid URL";
        $message = "messageerror";
        }
            }
            else{
                $error = "Type in the Maximum Views you<br />want your site to get daily!";
                $message = "messageerror";
            }
        }
        else{
                $error = "Type in the Url of your website!";
                $message = "messageerror";
            }
    }
    else{
                $error = "Type in a Title for your site!";
                $message = "messageerror";
            }
} 
}
else{
$loginstatus = "login";
header("Location: index.php");
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html lang="en-US" xml:lang="en-US" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta name="description" content="Free Piano Sheet Music - Sheet Music Haven" />
<meta name="keywords" content="Piano,Sheet,Music,Haven,Score,Piece,Top,Trade" />
<meta name="author" content="Sheet Music Haven" />
<meta http-equiv="Content-Type" content="text/html;charset=ISO-8859-1" />
<title>Edit Sites - Sheet Music Haven</title>
<link rel="stylesheet" type="text/css" href="styles/style.css" />
<link rel="stylesheet" type="text/css" href="styles/editsites.css" />
<script type="text/javascript">
function make_blank()
{
if(document.login.username.value =="Username"){
    document.login.username.value ="";
    document.login.username.style.color ="#000000";
}
}
function make_blank1()
{
if(document.login.password.value =="Password"){
document.login.password.value ="";
document.login.password.type ="password";
document.login.password.style.color ="#000000";
}
}
function undoBlank() {
  if(document.login.username.value == ""){
    document.login.username.value ="Username";
    document.login.username.style.color="#ccc";
  }
}
function undoBlankpass() {
  if(document.login.password.value == ""){
    document.login.password.value ="Username";
    document.login.password.style.color="#cccccc";
  }
}
function inputLimiter(e,allow) {
var AllowableCharacters = '';
if (allow == 'NumbersOnly'){AllowableCharacters='1234567890';}
var k;
k=document.all?parseInt(e.keyCode): parseInt(e.which);
if (k!=13 && k!=8 && k!=0){
if ((e.ctrlKey==false) && (e.altKey==false)) {
return (AllowableCharacters.indexOf(String.fromCharCode(k))!=-1);
} else {
return true;
}
} else {
return true;
}
}
</script>
</head>
<body bgcolor="#343331">

<!-- Header -->
<div id="header">
    <div id="headerleft"></div>
    <div id="headermiddle"><a href="index.php"><img src="img/logo.png"></a></div>
    <div id="headerright">
    

            <?php echo "<form name='login' action='inc/$loginstatus.php' method='POST'>";?>
            <div class="loginboxdiv" id="username">
            <input type="text" class="loginbox" name="username" value="Username" onFocus="make_blank();" onBlur="undoBlank();">
            </div>
            <div class="loginboxdiv" id="password">
            <input class="loginbox" type="text" name="password" type="text" value="Password" onFocus="make_blank1();" onBlur="undoBlankpass();">
            </div>
            <div id="login">
            <?php echo "<input type='image' src='img/$loginstatus.png' alt='".ucfirst($loginstatus)."'>";?>
            </div>
            </form>
            <div id="register">
            <a href="register.php"><img src="img/register.png"></a>
            </div>
            <div id="forgotpassword">
            <a href="resetpassword.php" class="forgot">Forgot Password?</a>
            </div>
    </div>
    
</div>

<!-- Content Top -->
<div id="contenttop">
    <div id="links">
    
    <table cols="7">
    <tr>
        <td align="center" valign="middle" width="100px" height="48px"><a href="index.php"><img src="img/home.png"></a></td>
        <td align="center" valign="middle" width="100px" height="48px"><a href="member.php"><img src="img/member.png"></a></td>
        <td align="center" valign="middle" width="100px" height="48px"><a href="addsheet.php"><img src="img/addsheet.png"></a></td>
        <td align="center" valign="middle" width="100px" height="48px"><a href="advertise.php"><img src="img/advertise1.png"></a></td>
        <td align="center" valign="middle" width="100px" height="48px"><a href="faq.php"><img src="img/faq.png"></a></td>
        <td align="center" valign="middle" width="100px" height="48px"><a href="terms.php"><img src="img/terms.png"></a></td>
        <td align="center" valign="middle" width="100px" height="48px"><a href="contact.php"><img src="img/contact.png"></a></td>
    </tr>
    </table>
<!-- 92x30 -->
    </div>
</div>

<!-- Content Middle -->
<div id="contentmiddle">
<div id="content">

    <div id="headeditsites"></div>

<br />
<div id="editsite">
 <form action="editsites.php" method="post" name="newsite">
    Title: <input type="text" name="title" value="<?php echo $titledb; ?>"><br />
    Url: <input type="text" name="url" value="<?php echo $urldb; ?>"><br />
    Max Views: <input type="text" maxlength="11" id="NumbersOnly" onkeypress="return inputLimiter(event,'NumbersOnly')" name="maxviews" value="<?php echo $maxviewsdb; ?>"><br />
    <select name="status"><option value='Active' name='active'>Active</option><option value='Passive' name='passive'>Passive</option></select><br /><br />
    <center><input type="submit" name="editnewsite" value="Edit Site"></center>
 </form>

 </div>
</div>

</div>

<!-- Content Bottom -->
<div id="contentbottom">

</div>

</body>
</html>
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Variable Mysteriously Stops Echoing

Post by califdon »

Your id field in the table is an INT(). You must remove the single quotes around the variable name in your SQL statement. By putting quotes around it, you are telling MySQL that it is a string, so of course that will throw an error, since 95 is totally different from '95'. Same goes for userid. siteid and any other fields that are numeric.
Post Reply