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:


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

$username = $_SESSION['username'];


$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'];

    $statusnew = "yes";
    $statusnew = "no";

$editnewsite = $_POST['editnewsite'];

$message = "";
$message = "messagesuccess";
// $siteid will Echo up until here. After this point it stops working.
            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;
              //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'";

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

        } // here
    else {
        $error = "Invalid URL";
        $message = "messageerror";
                $error = "Type in the Maximum Views you<br />want your site to get daily!";
                $message = "messageerror";
                $error = "Type in the Url of your website!";
                $message = "messageerror";
                $error = "Type in a Title for your site!";
                $message = "messageerror";
$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">
<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";
function undoBlankpass() {
  if(document.login.password.value == ""){
    document.login.password.value ="Username";
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;
<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 class="loginboxdiv" id="password">
            <input class="loginbox" type="text" name="password" type="text" value="Password" onFocus="make_blank1();" onBlur="undoBlankpass();">
            <div id="login">
            <?php echo "<input type='image' src='img/$loginstatus.png' alt='".ucfirst($loginstatus)."'>";?>
            <div id="register">
            <a href="register.php"><img src="img/register.png"></a>
            <div id="forgotpassword">
            <a href="resetpassword.php" class="forgot">Forgot Password?</a>

<!-- Content Top -->
<div id="contenttop">
    <div id="links">
    <table cols="7">
        <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>
<!-- 92x30 -->

<!-- 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>



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



Recommended Answers

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 …

Jump to Post

All 4 Replies

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"...>

Or make it a hidden variable in the form (<input type=hidden...)

thanks very much! All fixed

Glad to help!

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.