简体   繁体   English

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

[英]php update mysql table via form, reload information on page immediately

I'm creating a page in which room reservations are displayed in a table, with the possibilty to update or delete them. 我正在创建一个页面,其中房间预订显示在表格中,并且可以更新或删除它们。

The reservations come from a MySQL-database, table reservations. 保留来自MySQL数据库的表保留。

It works, but I would like that the information from the database is updated on the page immediately after pressing the buttons. 它可以工作,但是我希望在按下按钮后立即在页面上更新数据库中的信息。

For instance, if now I set the username from 'person' to 'another person', the field gets updated correctly in the database, but I need to refresch the page to see the update in my table. 例如,如果现在我将用户名从“ person”设置为“ another person”,则该字段将在数据库中正确更新,但是我需要刷新页面以查看表中的更新。

How can I do this? 我怎样才能做到这一点?

<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();
?>

The working code is: 工作代码为:

<?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>

Move the logic that does the updating and deleting above the logic that does the rendering: 将进行更新和删除的逻辑移到进行渲染的逻辑上方:

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

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

You'll also need to adjust your logic a bit. 您还需要稍微调整一下逻辑。 You're using the $reservationid from the SELECT to do the deleting and updating. 您正在使用SELECT$reservationid进行删除和更新。 This doesn't work, because the execution context for the PHP is refreshed with each page load. 这是行不通的,因为每次页面加载都会刷新PHP的执行上下文。 What you need is to store the reservation id in each form (maybe in a hidden field), and then to retrieve that from $_POST . 您需要以每种形式(可能在隐藏字段中)存储预订ID,然后从$_POST检索该ID。

Incidentally, your code is very vulnerable to SQL injection. 顺便说一句,您的代码容易受到SQL注入的攻击。 Also, you should look at using mysqli or PDO ; 另外,您应该看看使用mysqliPDO mysql_connect is deprecated in the current version of PHP. 当前版本的PHP中不推荐使用mysql_connect

You could use jQuery for this. 您可以为此使用jQuery。 You have to make an $.ajax ( http://api.jquery.com/jquery.ajax/ ) call. 您必须进行$.ajaxhttp://api.jquery.com/jquery.ajax/ )调用。 From the callback you can fill/set the fields you want to. 在回调中,您可以填充/设置所需的字段。 You'll need $('#idofelement').html() ( http://api.jquery.com/html/ ) for this. $('#idofelement').html()您需要$('#idofelement').html()http://api.jquery.com/html/ )。 If you have got any questions don't be affraid to ask ;) Good luck! 如果您有任何疑问,请不要害怕提出;)祝您好运!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM