2016-11-15 121 views
2

我遇到的问题如下:PHP和MySQL - 更新后显示结果

我可以保存并检索信息后保存,但我不知道如何自动执行一旦记录是保存/更新。

要更新我使用:

$result = mysql_query("UPDATE loan SET loana='$loann', dater='$dater', apaid='$apaid' WHERE id=$id"); 

一旦救我可以加载主页机智的结果,并单击它看起来像这样一个链接,它会显示所有信息的:

echo "<td><a href=\"full_loan_details.php?id=$res[id]\" target=\"_blank\" alt=\"Print loan details\" title=\"Print loan details\">".$res['name']."&nbsp;".$res['surname']."</a></td>"; 

...但我无法在记录保存时自动执行此操作。任何帮助是极大的赞赏。

+1

您可以使用['header'](http://php.net/manual/en/function.header.php)将用户重定向到页面。 – secelite

+0

在更新记录的同一动作中,您可以在更新后立即运行选择查询。 – Loko

+1

***请[停止使用'mysql_ *'功能](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php)。*** [这些扩展名](http://php.net/manual/en/migration70.removed-exts-sapis.php)已在PHP 7中删除。了解[prepared](http://en.wikipedia.org/wiki/Prepared_statement )[PDO]声明(http://php.net/manual/en/pdo.prepared-statements.php)和[MySQLi](http://php.net/manual/en/mysqli.quickstart.prepared- statement.php),并考虑使用PDO,[这真的很简单](http://jayblanchard.net/demystifying_php_pdo.html)。 –

回答

0

你可以使用header

header("Location: /full_loan_details.php?id=$res[id]"); 

如前所述脚本很容易受到注入攻击。您应该使用PDO's

<?php 

define("DB_DSN", "mysql:host=localhost;dbname=foo"); 
define("DB_USERNAME", "root"); 
define("DB_PASSWORD", "password"); 

// define sql 
$sSQL = "UPDATE loan SET loana=:loana, dater=:dater, apaid=:apaid WHERE id=:id"; 

// create an instance of the connection 
$conn = new PDO(DB_DSN, DB_USERNAME, DB_PASSWORD); 

// prepare 
$st = $conn->prepare($sSQL); 

// securely bind any user input in the query 
$st->bindValue(":loana", $loana, PDO::PARAM_STR); 
$st->bindValue(":dater", $dater, PDO::PARAM_STR); 
$st->bindValue(":apaid", $apaid, PDO::PARAM_STR); 
$st->bindValue(":id", $id, PDO::PARAM_INT); 

// execute the connection 
if($st->execute()){ 
    header("Location: /full_loan_details.php?id=".$id); 
}else{ 
    // didnt execute 
} 

你可以做一个SELECT确认更改和或得到一个值。与上述方法相同,但需要以下内容来读取它;

要提取单列使用If,或者如果多于1行使用while

if($row = $st->fetch()){ 
    header("Location: /full_loan_details.php?id=".$row['id']); 
} 

注意:这可能是不安全的用户重定向到基于从DB unsanatised数据的位置。即使你已经用上面的方法插入它。确保您正确地清洁所有输出。

+0

我试过完全一样的东西,但是失败了,我以为是因为id没有通过。因此我的问题。 – Cluster

+0

已更新我的答案。如果您仍有问题,请检查您是否有$ id中的值。 – atoms