php通过表单更新mysql表,立即在页面上重新加载信息

时间:2014-01-14 14:42:18

标签: php mysql

我正在创建一个页面,其中房间预订显示在表格中,可能会更新或删除它们。

预订来自MySQL数据库,表预订。

它有效,但我希望在按下按钮后立即在页面上更新数据库中的信息。

例如,如果现在我将用户名从“人”设置为“另一个人”,则该字段会在数据库中正确更新,但我需要刷新页面以查看表格中的更新。

我该怎么做?

<table border="1">
<tr><td>Datum</td><td>Start</td><td>Stop</td><td>Gebruikersnaam</td></tr>

<?php
$now = date("Y-m-d");
$query = "SELECT * FROM reservations WHERE (roomid = " . 45 . " AND end > NOW() ) ORDER BY start"; 
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$reservationid=$row["reservationid"];
$username=$row["username"];
$aantal=$row["numberingroup"];
$reservationid=$row["reservationid"];
$start=$row["start"];
$end=$row["end"];
$roomid=$row["roomid"];
?>

<form action="" method="post">
<tr><td><input name="StartDate" value="<? echo $StartDate; ?>" /></td><td><input name="StartTime" value="<? echo $StartTime; ?>" /></td><td><input name="StopTime" value="<? echo $StopTime; ?>" /></td><td><input name="username" value="<? echo $username;?>" /></td><td><input type="submit" value="update" name="<?php echo 'update_' . $reservationid; ?>" /></td><td><input type="submit" value="delete" name="<?php echo 'delete_' . $reservationid; ?>" /></td><td><? echo $reservationid; ?></td></tr></form>
<?php
//DELETE

if(isset($_POST['delete_' . $reservationid]))
{
$deletequery = "DELETE FROM reservations WHERE reservationid=" . $reservationid;
if(mysql_query($deletequery)){ 
echo "<p><b>Boeking verwijderd</b></p>";}
else {
echo "<p><b>Boeking niet verwijderd</b></p>";}
}

//UPDATE

if(isset($_POST['update_' . $reservationid]))
{
$NewStartDate = explode("-",$_POST[StartDate]);
$newstartdate = $NewStartDate[2] . "-" . $NewStartDate[1] . "-" . $NewStartDate[0];
$newstarttime = $_POST[StartTime] . ":00";

$newenddate = $newstartdate;
$NewEndTime = explode(":",$_POST[StopTime]);
$newendtime = mktime($NewEndTime[0],($NewEndTime[1]-1),59);
$newendtime = date("H:i:s",$newendtime);

$UpdateStart = $newstartdate . " " . $newstarttime;
$UpdateEnd = $newenddate . " " . $newendtime;

$UpdateUsername = $_POST[username];

$updatequery = "UPDATE reservations SET start='$UpdateStart', end='$UpdateEnd', username='$UpdateUsername' WHERE reservationid=" . $reservationid;
if(mysql_query($updatequery)){
echo "<p><b>Updated " . $reservationid . " " . $UpdateStart . " " . $UpdateEnd . " " .    $UpdateUsername . "</b></p>";}
else {
echo "<p><b>FAILURE IS NOT AN OPTION. AGAIN!</b></p>";}
}
?>

<?php
}
mysql_close();
?>

工作代码是:

<?php
//DELETE

if(isset($_POST['delete_' . $_POST[updateid]]))
{
$deletequery = "DELETE FROM reservations WHERE reservationid=" . $_POST[updateid];
  if(mysql_query($deletequery)){
  echo "<p><b>Boeking verwijderd</b></p>";
  }
  else {
  echo "<p><b>FAILURE IS NOT AN OPTION. AGAIN!</b></p>";
  }
}

//UPDATE

if(isset($_POST['update_' . $_POST[updateid]]))
{
$UpdateID = $_POST[updateid];

$NewStartDate = explode("-",$_POST[StartDate]);
$newstartdate = $NewStartDate[2] . "-" . $NewStartDate[1] . "-" . $NewStartDate[0];
$newstarttime = $_POST[StartTime] . ":00";

$newenddate = $newstartdate;
$NewEndTime = explode(":",$_POST[StopTime]);
$newendtime = mktime($NewEndTime[0],($NewEndTime[1]-1),59);
$newendtime = date("H:i:s",$newendtime);

$UpdateStart = $newstartdate . " " . $newstarttime;
$UpdateEnd = $newenddate . " " . $newendtime;

$UpdateUsername = $_POST[username];

$updatequery = "UPDATE reservations SET start='$UpdateStart', end='$UpdateEnd', username='$UpdateUsername' WHERE reservationid='$UpdateID'";
  if(mysql_query($updatequery)){
  echo "<p><b>Updated " . $reservationid . " " . $UpdateStart . " " . $UpdateEnd . " " . $UpdateUsername . "</b></p>";
  }
  else {
  echo "<p><b>FAILURE IS NOT AN OPTION. AGAIN!</b></p>";
  }
//  echo "<p><b>Updated " . $reservationid . " " . $UpdateStart . " " . $UpdateEnd . " "  . $UpdateUsername . "</b></p>";
}
?>

<?php
$query = "SELECT * FROM reservations WHERE (roomid = " . 45 . " AND end > NOW() ) ORDER BY start"; 
$result = mysql_query($query) or die(mysql_error());
?>
<table border="1">
<tr><td>Datum</td><td>Start</td><td>Stop</td><td>Gebruikersnaam</td></tr>

<?php
while($row = mysql_fetch_array($result)){
$reservationid=$row["reservationid"];
$username=$row["username"];
$aantal=$row["numberingroup"];
$reservationid=$row["reservationid"];
$start=$row["start"];
$end=$row["end"];
$roomid=$row["roomid"];

$startdate = explode(" ",$start);
$startdate[0] = explode("-",$startdate[0]);
$startdate[1] = explode(":",$startdate[1]);
$StartFormat = mktime($startdate[1][0],$startdate[1][1],$startdate[1][2],$startdate[0][1],$startdate[0][2],$startdate[0][0]);
$StartDate = date("d-m-Y",$StartFormat);
$StartTime = date("H:i",$StartFormat);

$stopdate = explode(" ",$end);
$stopdate[0] = explode("-",$stopdate[0]);
$stopdate[1] = explode(":",$stopdate[1]);
$StopFormat = mktime($stopdate[1][0],$stopdate[1][1],($stopdate[1][2]+1),$stopdate[0][1],$stopdate[0][2],$stopdate[0][0]);
$StopDate = date("d-m-Y",$StopFormat);
$StopTime = date("H:i",$StopFormat);
?>
<form action="" method="post">
<tr><td><input type="hidden" name="updateid" value="<?php echo $reservationid; ?>" />    <input name="StartDate" value="<? echo $StartDate; ?>" /></td><td><input name="StartTime" value="<? echo $StartTime; ?>" /></td><td><input name="StopTime" value="<? echo $StopTime; ?>" /></td><td><input name="username" value="<? echo $username;?>" /></td><td><input type="submit" value="update" name="<?php echo 'update_' . $reservationid; ?>" /></td><td>  <input type="submit" value="delete" name="<?php echo 'delete_' . $reservationid; ?>" /></td> </tr>
</form>

<?php
}
mysql_close();
?>
</table>

2 个答案:

答案 0 :(得分:1)

将执行更新和删除的逻辑移动到执行渲染的逻辑之上:

<?php
// DELETE (your delete stuff)
// UPDATE (your update stuff)

// RETRIEVE (your SELECT query)
?>
<table> <!-- your table markup -->
<?php
// RENDER (your while loop and such)

您还需要稍微调整一下逻辑。您正在使用$reservationid中的SELECT进行删除和更新。这不起作用,因为每次页面加载都会刷新PHP的执行上下文。您需要的是将保留ID存储在每个表单中(可能在隐藏字段中),然后从$_POST中检索它。

顺便提一下,您的代码非常容易受到SQL注入攻击。另外,您应该考虑使用mysqliPDO;在当前版本的PHP中不推荐使用mysql_connect

答案 1 :(得分:0)

你可以使用jQuery。您必须拨打$.ajaxhttp://api.jquery.com/jquery.ajax/)电话。从回调中,您可以填写/设置您想要的字段。您需要$('#idofelement').html()http://api.jquery.com/html/)。如果你有任何疑问,不要害怕问;)祝你好运!