上嗨我很努力地看到爲什麼這不更新數據庫。它重新加載由HEADER
指向的頁面,但不更新任何字段。任何幫助將是grelliy appreaciated。爲什麼這個更新查詢不能在php
更新表格
<?php
include("../script/dbconnect.php");
include("../script/addprodcat.php");
$post = get_posts($_GET['id']);
if (isset($_POST['name'], $_POST['description'], $_POST['price'], $_POST['sale'], $_POST['picture'], $_POST['category'])) {
$errors = array();
$name = trim($_POST['name']);
$description = trim($_POST['description']);
if (empty($name)) {
$errors[] = 'You need to supply a title';
} else if (strlen($name) > 255) {
$errors[] = 'Title cannot be longer than 255 characters';
}
if (empty($description)) {
$errors[] = 'You need to supply text';
}
if (empty($price)) {
$errors[] = 'You need to supply text';
}
if (empty($sale)) {
$errors[] = 'You need to supply text';
}
if (empty($picture)) {
$errors[] = 'You need to supply text';
}
if (! category_exists('id', $_POST['category'])) {
$errors[] = 'Category does not exist';
}
if (empty($errors)) {
edit_product($_GET['id'], $name, $description, $price, $sale, $picture, $_POST['category']);
header("Location: ../admin/edit_products.php?id={$post[0]['post_id']}");
die();
}
}
?>
<div style="width:100%; height:150px; background-color:white;"><span style="font-family:saxMonoRegular; letter-spacing:2px; display:block; font-size:4.5em; text-align:center; padding-top:15px;"> Edit <?php echo $post[0]['name']; ?> </span></div>
<div class="link" style="width:100%; background-color:#ccc;">
<form action="" method="post">
<?php
if (isset($errors) && ! empty($errors)) {
echo '<ul><li>', implode('</li><li>', $errors), '</li></ul>';
}
?>
<label for="name">Title</label>
<input type="text" name="name" value="<?php echo $post[0]['name']; ?>"><br/>
<label for="price">Price</label>
<input type="text" name="price" value="<?php echo $post[0]['price']; ?>"><br/>
<label for="sale">Sale</label>
<input type="text" name="sale" value="<?php echo $post[0]['sale']; ?>"><br/>
<label for="picture">Picture</label>
<input type="text" name="picture" value="<?php echo $post[0]['picture']; ?>"><br/>
<label for="description">Description</label>
<textarea name="description" rows="15" cols="50"><?php echo $post[0]['description']; ?></textarea><br/>
<label for="prod_id">Category</label>
<select name="prod_id">
<?php
foreach (get_categories() as $category) {
$selected = ($category['name'] == $post[0]['name']) ? " selected" : '';
?>
<option value="<?php echo $category['id']; ?>" <?php echo $selected; ?>> <?php echo $category['name']; ?></option>
<?php
}
?>
</select><br/>
<input class="button-link" type="submit" value="Edit Post">
</form>
</div>
addprodcat.php
function edit_product($id, $prod_id, $prod_sub_id, $name, $description, $price, $sale, $picture, $category) {
$id = (int) $id;
$prod_id = (int) $prod_id;
$prod_sub_id = (int) $prod_sub_id;
$name = mysql_real_escape_string($name);
$description = mysql_real_escape_string($description);
$price = mysql_real_escape_string($price);
$sale = mysql_real_escape_string($sale);
$picture = mysql_real_escape_string($picture);
$category = (int) $category;
mysql_query("UPDATE `products` SET
`cat_id` = {$category},
`prod_id` = {$prod_id},
`prod_sub_id ` = '{$prod_sub_id}',
`name` = '{$name}',
`description` = '{$description}',
`price` = '{$price}',
`sale` = '{$sale}',
`picture` = '{$picture}'
WHERE `id` = {$id}");
echo mysql_error();
}
你應該'echo'生成的查詢字符串,並在此處顯示:[**,請勿在新代碼中使用'mysql_ *'函數**](http://bit.ly/phpmsql) 。他們不再被維護[並被正式棄用](https://wiki.php.net/rfc/mysql_deprecation)。看到[**紅框**](http://j.mp/Te9zIL)?學習[*準備的語句*](http://j.mp/T9hLWi),並使用[PDO](http://php.net/pdo)或[MySQLi](http://php.net/ mysqli) - [這篇文章](http://j.mp/QEx8IB)將幫助你決定哪個。如果你選擇PDO,[這裏是一個很好的教程](http://j.mp/PoWehJ)。 – DaveRandom