使用mysqli更新记录的问题

时间:2017-07-26 03:32:57

标签: php mysqli

我想更新/编辑我的用户表单,但是当我点击“编辑”按钮时,我没有得到预期的结果。它应该显示新数据,但它会显示旧数据。

编码:

<?php 

include"errorReporting.php";
 include "conn.php";
 $conn = connect();
 $conndb = connectdb();

$wardID = $_REQUEST["wardID"];
$RequestName = $_REQUEST["RequestName"];
$Department = $_REQUEST["Department"];
$Position = $_REQUEST["Position"];
$Date= $_REQUEST["Date"];
$TypeOfRequest = $_REQUEST["TypeOfRequest"];
$PleaseSpecify = $_REQUEST["PleaseSpecify"];
$DateRequire = $_REQUEST["DateRequire"];
$DateReturn = $_REQUEST["DateReturn"];

mysqli_select_db($conn,"misadmin") or die ($conn->error ."\n");
$query = "select * from requestform";
$result2= $conn->query($query) or die ($conn->error ."\n");
$row_result =mysqli_fetch_assoc($result2);

mysqli_select_db($conn,"misadmin") or die ($conn->error ."\n");
$conn ->query("UPDATE requestform SET RequestName='$RequestName',Department='$Department',Position='$Position',Date='$Date',TypeOfRequest='$TypeOfRequest',PleaseSpecify='$PleaseSpecify',DateRequire='$DateRequire',DateReturn='$DateReturn' where wardID='$wardID'",$conn->affected_rows);

$result_update=mysqli_fetch_assoc($result);



header("Location:requestform3.php");
?>

输出: this is edit page

update to new record

did not change anything

5 个答案:

答案 0 :(得分:1)

where wardID='???'中的

必须在数据发生变化之前检索数据。我是说&#34; 4f&#34;不是&#34; med&#34;。

您可以在帖子表单中添加一个textbok:

<input type="hidden" id="original_wardID" value="<?php echo $wardID?>" />

在你的php代码中添加:

$ori_wardID=$_REQUEST['original_wardID'];

然后改变你的sql:

UPDATE requestform SET wardID='$wardID',RequestName='$RequestName',Department='$Department',Position='$Position',Date='$Date',TypeOfRequest='$TypeOfRequest',PleaseSpecify='$PleaseSpecify',DateRequire='$DateRequire',DateReturn='$DateReturn' where wardID='$ori_wardID'

答案 1 :(得分:1)

试一试:

$conn ->query("UPDATE requestform SET RequestName='$RequestName',Department='$Department',Position='$Position',Date='$Date',TypeOfRequest='$TypeOfRequest',PleaseSpecify='$PleaseSpecify',DateRequire='$DateRequire',DateReturn='$DateReturn' where wardID=$wardID ");

答案 2 :(得分:0)

"UPDATE requestform SET RequestName='$RequestName',Department='$Department',Position='$Position',Date='$Date',TypeOfRequest='$TypeOfRequest',PleaseSpecify='$PleaseSpecify',DateRequire='$DateRequire',DateReturn='$DateReturn' where wardID='$wardID'"

更改为

"UPDATE requestform SET RequestName='".$RequestName."',Department='".$Department."',Position='".$Position."',Date='".$Date."',TypeOfRequest='".$TypeOfRequest."',PleaseSpecify='".$PleaseSpecify."',DateRequire='".$DateRequire."',DateReturn='".$DateReturn."' where wardID=".$wardID

或者喜欢这个演示代码

$db_sql="select id ,uid,regdate from `newtable` where id=?";
$stmt=$mysqli->prepare($db_sql);//
$stmt->bind_param("i",$id);// i int d double s string b blob
$result = $stmt->execute();

但我想使用pdo会更好

答案 3 :(得分:0)

您没有使用if ($_SERVER['REQUEST_METHOD'] == 'POST'){//save database}(isset $_POST['edit']){//save database}作为编辑按钮,以便在点击编辑按钮时保存数据。

另外,您在UPDATE查询中使用日期(日期=)。 DATE也是MYSQL中使用的本机函数,您需要更改它。如果您将其更改为DateChanged使用预准备语句,它可以帮助您的代码更具可读性。

if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
{
    $stmt = $conn->prepare("UPDATE requestform  SET RequestName=?, 
    Department=?, Position=?, DateChanged=?, TypeOfRequest=?, 
    PleaseSpecify=?, DateRequire=?, DateReturn=? WHERE wardID=?");

    $stmt->bind_param("sssssssss", $RequestName, $Department, 
    $Position, $DateChanged, $TypeOfRequest, $PleaseSpecify, 
    $DateRequire, $wardID);
    $stmt->execute();
    $stmt->close();
    $conn->close();

}

答案 4 :(得分:0)

我找到了答案。在我使用旧模板重做我的编码之后

以下是代码。

{{1}}

感谢那些提出建议的人。我从你们那里学到了很多东西。