-3

Can anyone tell me why I am getting the following undefined variable ($url) error for this script. The error is where my if statement if ($url). I am a newbie. thanks

An error occurred in script 'testnew/edit_your_sites.php' on line 28: Undefined variable: url

<div class="text">
    <?php
    $page_title = 'Edit Your Account';
    include ('includes/header.html');
    include ('includes/functions.php');
    include ('includes/config.inc.php');
    if (isset($_GET['id']) && is_numeric($_GET['id'])){
        $id = $_GET['id'];
    } elseif (isset($_POST['id']) && is_numeric($_POST['id'])) {
        $id = $_POST['id'];
    } else {
        echo '<p class="error">This page has been accessed in error.</p>';
        include ('includes/footer.html');
        exit();
    }
    if (isset($_SESSION['UserID'])){
    require (MYSQL);
    $scrubbed = array_map('spam_scrubber', $_POST);


    if ($_SERVER['REQUEST_METHOD'] == 'POST'){
    if (empty($scrubbed['url'])){
            echo '<p class="error">Please enter a url</p>';
        } else {
            $url = mysqli_real_escape_string($dbc, $scrubbed['url']);
        }
    }
    if ($url){
    $p = "SELECT UserID FROM sites WHERE UserID=$id";
    $q = mysqli_query($dbc, $p);
    if (mysqli_num_rows($q) == 1){

    $i = "INSERT INTO sites (UserID, url, entry) VALUES('{$_SESSION['UserID']}', '$url', NOW())";
    $r = mysqli_query($dbc, $i);
        if (mysqli_affected_rows($dbc) == 1){
        echo '<p>Your website was added succesfully.</p>';
        } else {
            echo '<p class="error">Due to a system error your website could not be added.</p>';
        }

    } else {
        echo '<p class="error">Please try again';
    }

    }
    ?>
    <form action="edit_your_sites.php" method="post">
    <?php $sql = "SELECT * FROM sitetypes";
    $f = mysqli_query($dbc, $sql) or trigger_error("Query: $sql\n<br />Mysqli Error: " . mysqli_error($dbc));
    echo '<select name="SiteType" selected="selected">';
    while($row2 = mysqli_fetch_array($f, MYSQLI_ASSOC)){
        if ($row2['SiteType'] == 'selected'){
            echo "<option Selected='selected' value=" . $row2["SiteTypeID"] . ">" . $row2['SiteType'] . "</option>";
        } else { 
            echo "<option value=" . $row2["SiteTypeID"] . ">" . $row2['SiteType'] . "</option>";
        }
    }

    echo '</select>';
    ?>
    <p>Url:<input type="text" name="url" size="30" maxlength="60" value="<?php if(isset($scrubbed['url'])) echo $scrubbed['url']; ?>" /></p>
    <?php
    echo '</fieldset>
    <p><input type="submit" name="submit" value="Edit Account!" /><input type="reset" name="reset" value="Clear Form" />
    <input type="hidden" name="id" value="' . $id . '" />
    </form>';
    } else {
        $url = BASE_URL . 'index.php';
        header("Location: $url");
    }
    ?>

there may be some more errors in this script if you see any it would be appreciated if you let me know about them. thanks for all your help, you guys are really a great help

faintsignal
  • 1,828
  • 3
  • 22
  • 30
  • possible duplicate of [Reference - What does this error mean in PHP?](http://stackoverflow.com/questions/12769982/reference-what-does-this-error-mean-in-php) – Anonymous Apr 06 '14 at 13:37
  • 2
    This has been asked many times and is obvious. Your variable `$url` is only being declared based on condition, so when that condition is not true, it will throw that error. Change `if ($url){` to `if (isset($url)){` – AyB Apr 06 '14 at 13:37
  • its a notice...just turn it off... Nothing can damage this code.. – vp_arth Apr 06 '14 at 13:39
  • What about `Use of undefined constant MYSQL` ? Or it defined? – vp_arth Apr 06 '14 at 13:41

3 Answers3

0

It's because you define the variable $url only in the else block. However, if empty($scrubbed['url']) is true, then the variable is never defined, because the else block is never executed.

if (empty($scrubbed['url'])) {
    echo '<p class="error">Please enter a url</p>';
}
else {
    $url = mysqli_real_escape_string($dbc, $scrubbed['url']);
}

To avoid this error, define $url just before the abovementioned snippet:

$url = null;
MC Emperor
  • 22,334
  • 15
  • 80
  • 130
0

The variable is only defined within an if statement, you need to define it prior to that... try defining it with a blank value at the beginning of your script.

$page_title = 'Edit Your Account';
$url="";
include ('includes/header.html');
Brad Faircloth
  • 337
  • 1
  • 7
0

It is because you have said

if($url)

But this is not defined.

try

if(isset($url))
smistry
  • 1,127
  • 1
  • 9
  • 9
  • I tried if(isset($url)) but my form tells me that $url is not set, and no matter what I try it is never set, even though I type something in the form field and press submit. – user3286022 Apr 06 '14 at 13:52