954,180 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Variable Mysteriously Stops Echoing

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:

<?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."".$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 youwant 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>


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

 </div>
</div>

</div>

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

</div>

</body>
</html>
Smudly
Light Poster
41 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

It sounds like you are INITIALLY arriving at the page via editsites.php?siteid=95 , in which case line 11 works as expected. BUT your FORM does not "save" that siteid, so when you submit/POST the form, it is going to editsites.php, NOT to editsites.php?siteid=95.

So add the site id to your FORM's action:

<form action="editsites.php?siteid=<?php echo intval($_GET['siteid']);?>" method="post"...>
hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 243
 

Or make it a hidden variable in the form (

chrishea
Nearly a Posting Virtuoso
1,427 posts since Sep 2008
Reputation Points: 210
Solved Threads: 230
 

thanks very much! All fixed

Smudly
Light Poster
41 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
 

Glad to help!

PS: Don't forget to mark the thread as solved!

hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 243
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You