在新页面上显示单个条目,发送ID为row [英] show single entry on a new page with sending id of row
问题描述
我是php和mysqli的新手,并找到了不错的教程,但需要一些帮助.
i am very novice to php and mysqli and found a great tutorial but am needing some help.
我希望一行可链接,并将其发送到另一个名为single.php?id = ROWID的页面,这样它将显示单个条目
i am wanting a row to be linkable and send it to another page named single.php?id=ROWID so it will show the single entry
这是我到目前为止所得到的.
this is what i got so far.
<html>
<head>
<title>MySQLi Tutorial</title>
</head>
<body>
<?php
//include database connection
include 'db_connect.php';
$action = isset($_GET['action']) ? $_GET['action'] : "";
if($action=='delete'){ //if the user clicked ok, run our delete query
$query = "DELETE FROM users WHERE id = ".$mysqli->real_escape_string($_GET['id'])."";
if( $mysqli->query($query) ){
echo "User was deleted.";
}else{
echo "Database Error: Unable to delete record.";
}
}
$query = "select * from users";
$result = $mysqli->query( $query );
$num_results = $result->num_rows;
echo "<div><a href='add.php'>Create New Record</a></div>";
if( $num_results ){
echo "<table border='1'>";//start table
//creating our table heading
echo "<tr>";
echo "<th><a href=\"single.php?id={$id}\">Firstname</></th>";
echo "<th>Lastname</th>";
echo "<th>Username</th>";
echo "<th>Action</th>";
echo "</tr>";
//loop to show each records
while( $row = $result->fetch_assoc() ){
//extract row
//this will make $row['firstname'] to
//just $firstname only
extract($row);
//creating new table row per record
echo "<tr>";
echo "<td>{$firstname}</td>";
echo "<td>{$lastname}</td>";
echo "<td>{$username}</td>";
echo "<td>";
echo "<a href='edit.php?id={$id}'>Edit</a>";
echo " / ";
echo "<a href='#' onclick='delete_user( {$id} );'>Delete</a>";
echo "</td>";
echo "</tr>";
}
echo "</table>";//end table
}else{
//if table is empty
echo "No records found.";
}
//disconnect from database
$result->free();
$mysqli->close();
?>
<script type='text/javascript'>
function delete_user( id ){
//this script helps us to
var answer = confirm('Are you sure?');
if ( answer ){ //if user clicked ok
//redirect to url with action as delete and id to the record to be deleted
window.location = 'index.php?action=delete&id=' + id;
}
}
</script>
</body>
</html>
我认为我将在url中发送行ID是正确的吗?
i am right in thinking i would be sending the rows id in the url ?
echo "<th><a href=\"single.php?id={$id}\">Firstname</></th>";
但是我在single.php中遇到问题,我必须输入什么代码才能显示单个条目?
but i am having issues with single.php what code would i have to put to show the single entry?
我已经有一段时间了,没有人在附近,所以我删除了代码,吞下我的骄傲寻求帮助:/
i have been on this a while and got no were near so i deleted the code and swallowed my pride to seek some help :/
预先感谢
推荐答案
感谢您提出有趣的问题.
首先,让我通知您,尽管您使用的是看起来比较现代的数据库访问库,但使用它的方式就像猛as的化石一样古老.
Thank you for the interesting question.
First, let me inform you that, although you are using a moder-looking database access library, the way you are using it is as ancient as a mammoth fossil.
要考虑的几件事
- 从不按原样使用mysqli,但仅以某些更高级别的抽象库的形式使用.
- 从不在应用程序代码中使用real_escape_string,但仅使用准备好的语句.
- 从不将您的数据库代码与HTML输出混合在一起.首先获取数据,然后开始输出.
- 从不使用GET方法修改数据.
- Never use mysqli as is, but only in the form of some higher level abstraction library.
- Never use real_escape_string in the application code but use prepared statements only.
- Never mix your database code with HTML output. Get your data first, then start for output.
- Never use GET method to modify the data.
这里是基于上述原理的示例.它执行所有基本的CRUD操作:
Here goes the example based on the above principles. It does ALL basic CRUD operations:
<?
include 'safemysql.class.php'; // a library
$db = new SafeMysql();
$table = "test";
if($_SERVER['REQUEST_METHOD']=='POST') {
if (isset($_POST['delete'])) {
$db->query("DELETE FROM ?n WHERE id=?i",$table,$_POST['delete']);
} elseif ($_POST['id']) {
$db->query("UPDATE ?n SET name=?s WHERE id=?i",$table,$_POST['name'],$_POST['id']);
} else {
$db->query("INSERT INTO ?n SET name=?s",$table,$_POST['name']);
}
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
exit;
}
if (!isset($_GET['id'])) {
$LIST = $db->getAll("SELECT * FROM ?n",$table);
include 'list.php';
} else {
if ($_GET['id']) {
$row = $db->getRow("SELECT * FROM ?n WHERE id=?i", $table, $_GET['id']);
foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v);
} else {
$row['name']='';
$row['id']=0;
}
include 'form.php';
}
它正在使用模板来显示数据:
It is using templates to display the data:
list.php
<a href="?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>
和form.php
<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
<a href="?">Return to the list</a>
</form>
<? if ($row['id']):?>
<div align=right>
<form method="POST">
<input type="hidden" name="delete" value="<?=$row['id']?>">
<input type="submit" value="Удалить"><br>
</form>
</div>
<?endif?>
这是要显示的部分.
if ($_GET['id']) {
$row = $db->getRow("SELECT * FROM ?n WHERE id=?i", $table, $_GET['id']);
foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v);
} else {
$row['name']='';
$row['id']=0;
}
include 'form.php';
如果您不想显示表单,请使用所需的标记创建另一个名为single.php
的模板
if you don't want to show the form - create another template called single.php
with whatever markup you wish
这篇关于在新页面上显示单个条目,发送ID为row的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!