dongyuan9892 2014-04-06 13:35
浏览 71

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

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

  • 写回答

3条回答 默认 最新

  • dongsi8812 2014-04-06 13:38
    关注

    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;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?