2016-03-07 42 views
-1

我不能與此代碼更新我的表中的值,如果更新是成功的頁面重定向應該在(「位置:ui.php」),如何才能實現這一目標?如何更新表中的值

<?php 
ob_start(); 
include('dbconnect.php'); 

$code=$_GET['stallcode']; 

if(isset($_POST['submit'])) 
{ 
    $pcost = $_POST['pcost']; 
    $tcost = $_POST['tcost']; 
    $cash = $_POST['cash']; 
    $change = $_POST['change']; 
    if (($cash == '0')) 
    { 
     $pstatus="0"; 
    } 
    else 
    { 
     $pstatus="1"; 
    } 

    $updated=mysql_query("UPDATE tbl_stallowner SET 
      paymentstatus='$pstatus', penaltycost='$pcost',  totalcost='$tcost', cash='$cash', change='$change' 
      WHERE stallcode='$code'")or die(); 
    if($updated) 
    { 
     $msg="Successfully Updated!!"; 
     header('Location:ui.php'); 
    } 
} //update ends here 

ob_end_flush(); 
?> 
+1

會發生什麼?錯誤訊息?您應該轉義您的發佈值並且不要使用已棄用的mysql函數。 – purpleninja

回答

0

當你通過檢查if($updated)是真正將用戶重定向,這是不行的,你應該檢查而不是使用mysql_num_rows受影響的行數。

還記得exit;header()後停止執行。

$num_rows = mysql_num_rows($updated); 
if($num_rows > 0) 
{ 
$msg="Successfully Updated!!"; 
header('Location:ui.php'); 
exit; 
} 

提示:你不應該使用MySQL因爲它已經被棄用,使用MySQLi代替。

+0

這個答案將工作,但是你不應該使用這個作爲一個修復,而是移動到庫MySQLi作爲luweiqi指出。 – iJamesPHP

+0

嗨@Enzo如果這個或任何答案已經解決您的問題,請考慮點擊複選標記接受它。這向更廣泛的社區表明,您已經找到了解決方案,併爲答覆者和您自己提供了一些聲譽。沒有義務這樣做。 – Panda

0

你想用mysqli的,不是的前身,MySQL的。 MySQL是脆弱和露天開採,這裏是你應該在每個文件寫的是什麼:

dbconnect.php

<?php 
$conn = mysqli_connect("localhost","my_user","my_password","my_db"); 

// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
?> 

updatestallowner.php(或任何你把它命名爲)

<?php 
ob_start(); 
require('dbconnect.php'); 
$code = mysqli_real_escape_string($conn, $_GET['stallcode']); 
if(isset($_POST['submit'])){ 
    $pcost = mysqli_real_escape_string($conn, $_POST['pcost']); 
    $tcost = mysqli_real_escape_string($conn, $_POST['tcost']); 
    $cash = mysqli_real_escape_string($conn, $_POST['cash']); 
    $change = mysqli_real_escape_string($conn, $_POST['change']); 
    if ($cash == '0') { 
     $pstatus="0"; 
    } else{ 
     $pstatus="1"; 
    } 

    $sql = "UPDATE tbl_stallowner SET paymentstatus='$pstatus', penaltycost='$pcost', totalcost='$tcost', cash='$cash', change='$change' WHERE stallcode='$code';"; 
    $result = mysqli_query($conn, $sql); 
    if($result) { 
     $msg="Successfully Updated!!"; 
     header('Location: ui.php'); 
     exit; 
    } else { 
     die("Error updating!"); 
    } 
} 

?> 

祝你好運!