2012-04-20 82 views
0

我似乎在編輯用戶/成員信息時遇到了問題。我已經提供了下面的表格。任何解決方案都非常感謝。我已經拿出驗證檢查來縮短腳本。使用php/mysql編輯頁面

頁面呈現時沒有錯誤。並顯示成功消息。但是,數據庫中的信息未被更改/編輯。

此外,數據庫中的值(對應於正在運行的第二個數據庫查詢)正顯示在窗體的字段中。但是,當我POST更改數據庫中沒有進行更改。

**PHP scipt** 

<?php 
session_start(); 
if (isset($_SESSION['id'])) { 
$id = $_SESSION['id']; 
$username = $_SESSION['username']; 
} 

else { 
echo "You have not signed in"; 
} 

if (isset ($_POST['submit'])){ 
$title = $_POST['title']; 
$content = $_POST['content']; 
$make= $_POST['make']; 
$model = $_POST['model']; 
$price = $_POST['price']; 
$location = $_POST['location']; 

include_once "scripts/connect_to_mysql.php";  

$title = mysql_real_escape_string($title); 
$content = mysql_real_escape_string($content); 
$make = mysql_real_escape_string($make); 
$model = mysql_real_escape_string($model); 
$price = mysql_real_escape_string($price); 
$location = mysql_real_escape_string($location); 

$title = eregi_replace("`", "", $title); 
$content = eregi_replace("`", "", $content); 
$make = eregi_replace("`", "", $make); 
$model = eregi_replace("`", "", $model); 
$price = eregi_replace("`", "", $price); 
$location = eregi_replace("`", "", $location); 

$sql = mysql_query ("UPDATE `advertisements` SET `title`='$title',   
`content`='$content', `make`='$make', `model`= '$model', `price`='$price', 
`location`='$location', `id`='$id' WHERE `advertisements` . `ads_id`='$ads_id'") 
or die (mysql_error()); 

$success = "You have successfuly edited your ad"; 
} 

else { 

    if (isset($_GET['ads_id'])) { 
    $ads_id = $_GET['ads_id']; 
    } 

    else { 
    echo "URL not found"; 
    } 

    include_once "scripts/connect_to_mysql.php"; 
    $query = mysql_query("SELECT * FROM advertisements WHERE ads_id='$ads_id'"); 

    while($row = mysql_fetch_assoc($query)) 
    { 
    $title = $row["title"]; 
$content = $row["content"]; 
$make = $row["make"]; 
$model = $row["model"]; 
$price = $row["price"]; 
$location = $row["location"]; 
$ads_id = $row ["ads_id"]; 
    } 



    } 

    ?> 


    **form** 

    <h1>Edit Advertisement</h1> 

    <?php echo "$success";?> 

<form action="edit.php" method="POST" enctype="multipart/form-data"> 

Title: <input name="title" type="text" value="<?php print "$title"; ?>"/><br/> 
Content: <input name="content" type="text" value="<?php print "$content"; 
    ?>"/><br/> 
Make: <select name="make"> 
<option value="<?php echo "$make"; ?>"><?php echo "$make"; ?></option> 
     <option value="1">1</option> 
     <option value="2">2</option> 
     <option value="3">3</option> 
     <option value="4">4</option> 
</select> <br/> 

Model: <select name="model"> 
    <option value="<?php echo "$model"; ?>"><?php echo "$model"; ?></option> 
     <option value="1">1</option> 
     <option value="2">2</option> 
     <option value="3">3</option> 
     <option value="4">4</option> 
</select> <br/> 
Price: <input name="price" type="text" value="<?php print "$price"; ?>"> <br/> 

Location: <select name="location"> <br/> 
     <option value="<?php echo "$location"; ?>"><?php echo "$location"; 
        ?></option> 
     <option value="Leicester">Leicester</option> 
     <option value="Loughborough">Loughborough</option> 
     <option value="Nottingham">Nottingham</option> 
     <option value="Derby">Derby</option> 
</select> <br/> <br/> 

    <input name="submit" type="submit" value="Edit ad"/> 

</form> 
+0

不要檢查表單域以確定您是否在帖子中。使用'$ _SERVER ['REQUEST_METHOD'] =='POST'。它是100%可靠的。 – 2012-04-21 00:32:40

+0

嘗試添加回聲語句以檢查您是否獲得了正確的值。 (echo $ title;在mysql_query之前等) – 2012-04-21 01:26:53

+0

我很感激這個幫助。我已經嘗試了兩個東西,你們建議,但它仍然不會更新我的數據庫,當我運行UPDATE查詢後運行echo語句時,我得到更新的值。你們有沒有其他的解決方案,或者如果可能的話,你可以編輯我的腳本到你認爲可以工作的地方。 – sam 2012-04-22 11:49:27

回答

0

嘗試移動

if (isset($_GET['ads_id'])) { 
    $ads_id = $_GET['ads_id']; 
    } 

    else { 
    echo "URL not found"; 
    } 

到右上角的

if (isset ($_POST['submit'])){ 

後,這可能會導致問題

和表單行動,我想一定有這樣的事

<form action ="edit.php?ads_id=the id for the page" > 
+0

謝謝你的寫作。 – sam 2012-04-26 18:11:03