undefined变量$ url,不确定为什么我收到此错误

时间:2014-04-06 13:35:09

标签: php mysql mysqli

任何人都可以告诉我为什么我收到此脚本的以下未定义变量($ url)错误。 错误是我的if语句if ($url)。我是新手。感谢

  

第28行的脚本'testnew / edit_your_sites.php'发生错误:   未定义的变量: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");
    }
    ?>

此脚本中可能还有一些错误 如果你看到任何它,如果你将不胜感激 让我知道他们。 感谢您的帮助,你们真的是一个很好的帮助

3 个答案:

答案 0 :(得分:0)

这是因为您在$url块中定义了变量else 。但是,如果empty($scrubbed['url'])true,则永远不会定义变量,因为永远不会执行else块。

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

要避免此错误,请在上述代码段之前定义$url

$url = null;

答案 1 :(得分:0)

变量仅在if语句中定义,您需要在此之前定义它...尝试在脚本开头用空值定义它。

$page_title = 'Edit Your Account';
$url="";
include ('includes/header.html');

答案 2 :(得分:0)

因为你说过了

if($url)

但这没有定义。

if(isset($url))