У меня есть рабочая версия следующего скрипта, но есть огромный блок жестко закодированных данных, чтобы он работал. В следующем коде я сократил этот код до всего лишь ассоциативного массива, однако на моей странице значение SiteTypeID всегда равно 0, когда оно должно быть числом от 3 до 43. $ _POST [‘SiteTypeID’] не кажется выбирать что-нибудь. Я не могу найти ошибку.
Вот код страницы, закомментирован раздел, с которым мне нужна помощь.
<?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);
echo '<div class="text">';
$scrubbed = array_map('spam_scrubber', $_POST);
if ($_SERVER['REQUEST_METHOD'] == 'POST'){
if (filter_var($scrubbed['url'], FILTER_VALIDATE_URL)){
$url = mysqli_real_escape_string($dbc, $scrubbed['url']);
} else {
$url = "";
echo '<p class="error">Please enter a valid url</p>';
}
if (!empty($scrubbed['SiteTypeID'])){
$sitetypeid = $scrubbed['SiteTypeID'];
} else {
$sitetypeid ="";
echo '<p class="error">Please select a category</p>';
}
if($url && $sitetypeid){
$q = "SELECT UserID, url, SiteID FROM sites WHERE url='$url' AND SiteTypeID='$sitetypeid' AND SiteTypeID!=$id";
$query = mysqli_query ($dbc, $q);
if (mysqli_num_rows($query) == 0){
$q = "UPDATE sites SET url='$url', SiteTypeID='$sitetypeid' WHERE SiteID=$id LIMIT 1";
$query = mysqli_query ($dbc, $q);
if (mysqli_affected_rows($dbc) == 1){
echo '<p><b>Your url has been successfully edited</b></p>';
} elseif (mysqli_affected_rows($dbc) == 0){
echo '<p>No new details have been inserted</p>';
} else {
echo '<p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message.
echo '<p>' . mysqli_error($dbc) . '<br />Query: ' . $q . '</p>'; // Debugging message.
}
} else {
echo '<p><b>That url has already been registered with that category, if you own this site you can change categories if you wish to. Please note that a website cannot be added multiple times in different categories.</b></p>';
}
}
}
?>
<h1>Edit Your Sites</h1><p><b>Do not add duplicate urls, they will be deleted. We only accept family-safe urls. We play fair and expect our members to do the same.</b> </p>
<?php
$query2 = "SELECT s.SiteTypeID, u.UserID, s.SiteType FROM sitetypes AS s LEFT JOIN sites AS u USING (SiteTypeID) WHERE SiteID=$id";
$mx = mysqli_query($dbc, $query2);
if (mysqli_num_rows($mx) ==1){
$row2 = mysqli_fetch_array($mx, MYSQLI_ASSOC);
echo '<p>Your site\'s current category is <b>'.$row2['SiteType'] . '</b></p>';
}
?>
// Это блок кода, в котором проблема.
<?php
$optionarray = array(3 => 'Children & Family', 4 => 'Home Business', 5 => 'Advertising', 6 => 'Affiliate Programs', 7 => 'Art & Photography', 9 => 'Beauty & Jewelry', 10 => 'Blogging', 11 => 'Books, Literature', 12 => 'Business & Finance', 13 => 'Computer Games', 14 => 'Computing', 15 => 'Dating & Relationships', 16 => 'Directories', 17 => 'Education',
18 => 'Electronics', 19 => 'Entertainment', 20 => 'Environment', 21 => 'Flowers', 22 => 'Food, Drink', 23 => 'Forums, chat rooms', 24 => 'Free Stuff', 25 => 'Gifts & Shopping',
26 => 'Health', 27 => 'Humor', 28 => 'Interior Design', 29 => 'Internet Marketing', 30 => 'Miscellaneous', 31 => 'Music', 32 => 'Pets', 33 => 'Real Estate', 34 => 'Religion & Spirituality', 35 => 'Science', 36 => 'Sports', 37 => 'Stocks & Trading', 38 => 'Travel', 39 => 'Vehicles', 40 => 'Web Design', 41 => 'Web Hosting', 42 => 'Work At Home', 43 => 'Psychology');
echo '<form action="edit_your_sites.php" method="post"><select name="SiteTypeID"><option value="">Select a Category</option>';
foreach ($optionarray as $key => $value){
echo '<option value="$key"';
if (isset($_POST['SiteTypeID']) && ($_POST['SiteTypeID'] == "$key")) echo 'selected="selected">';
echo '>' . $value . '</option>';
}
echo '</select>';
?>
// конец блока проблемного кода.
<?php$ms = "SELECT UserID, url, SiteID FROM sites WHERE SiteID=$id";
$msp = mysqli_query($dbc, $ms);
if (mysqli_num_rows($msp) > 0){
$row = mysqli_fetch_array($msp, MYSQLI_ASSOC);
echo '<p>Url:<input type="text" name="url" size="60" maxlength="80" value="' . $row['url'] .'" /><small>You can only add each url once into the database, each url must be unique.</small></p><p><input type="submit" name="submit" value="Edit Site Information!" /><input type="reset" name="reset" value="Clear Form" />
<input type="hidden" name="id" value="' . $id . '" />
</form><p><a href="delete_url.php?id=' . $row['SiteID'] . '"><b>Delete Url</b></a>';
} else {
echo '<p class="error">A system error occurred, we apologize for the inconvenience.</p>';
}
?>
<?php
} else {
$url = BASE_URL . 'index.php';
header("Location: $url");
}
echo '</div>';
include ('includes/footer.html');
?>
Задача ещё не решена.
Других решений пока нет …