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
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;
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');
It is because you have said
if($url)
But this is not defined.
try
if(isset($url))