[英]select record from mysql_fetch_assoc after putting into table
not sure how feasable this is, but I have just rolled my own user search form, which simply queries my database and returns all the results with any given username, or similar using the LIKE 'some_username%' statement. 不确定这是否可行,但是我刚刚滚动了自己的用户搜索表单,该表单仅查询我的数据库并使用给定的用户名或使用LIKE'some_username%'语句返回的所有结果返回所有结果。
My search works great, and im really chuffed with myself as I am a php and mysql novice. 我的搜索效果很好,由于我是php和mysql新手,因此我真的很高兴。
I used a mysql_fetch_assoc($result) statement, and then used a while loop to echo out each row from the database into an html table. 我使用mysql_fetch_assoc($ result)语句,然后使用while循环将数据库中的每一行回显到html表中。
What I would then like to be able to do, is select a record from the table, and open a new page, which is populated with all the fields for that record, which I can then use to edit and update the user settings. 然后,我想做的就是从表中选择一条记录,然后打开一个新页面,该页面中填充了该记录的所有字段,然后可以用来编辑和更新用户设置。
I thought perhaps one way to do it, is to perhaps echo out a form instead? 我想也许是这样做的一种方法,或者是回显某种形式? that way I can have a button next to each row, to post the fields into some php code on my new page? 这样,我可以在每行旁边放置一个按钮,以将字段发布到新页面上的一些php代码中? I thought this may be a bit clunky though, and not sure how I would go about echoeing out a different form for each row. 我认为这可能有些笨拙,并且不确定如何对每一行回显不同的形式。
Don;t know if anyone had any ideas on the best way to do this? 不知道是否有人对实现此目标的最佳方法有任何想法? If you need any code examples of what im working with, I can post them here. 如果您需要任何与我一起工作的代码示例,我可以在此处发布。
Thanks very much!! 非常感谢!! Eds 埃兹
not a form but a hyperlink. 不是表格而是超链接。
I wonder why you aren't familiar with this way of opening new pages as it is used everywhere. 我想知道为什么您不熟悉这种打开新页面的方式,因为这种方式到处都是。
just create a hyperlink 只需创建一个超链接
<a href="edit.php?id=1">name</a>
here is a sketch example of such an application, editing only one field, but you can add any number as well: 这是此类应用程序的草图示例,仅编辑一个字段,但您也可以添加任何数字:
a main script: 主要脚本:
<?
mysql_connect();
mysql_select_db("new");
$table = "test";
if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part:
$name = mysql_real_escape_string($_POST['name']);
if ($id = intval($_POST['id'])) {
$query="UPDATE $table SET name='$name' WHERE id=$id";
} else {
$query="INSERT INTO $table SET name='$name'";
}
mysql_query($query) or trigger_error(mysql_error()." in ".$query);
header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);
exit;
}
if (!isset($_GET['id'])) { //listing part:
$LIST=array();
$query="SELECT * FROM $table";
$res=mysql_query($query);
while($row=mysql_fetch_assoc($res)) $LIST[]=$row;
include 'list.php';
} else { // form displaying part:
if ($id=intval($_GET['id'])) {
$query="SELECT * FROM $table WHERE id=$id";
$res=mysql_query($query);
$row=mysql_fetch_assoc($res);
foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v);
} else {
$row['name']='';
$row['id']=0;
}
include 'form.php';
}
?>
and two simple templates responsible for output, 和两个简单的模板负责输出,
one for the displaying the form, form.php 一个用于显示表单,form.php
<? include TPL_TOP ?>
<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>
<? include TPL_BOTTOM ?>
and one to display the list, list.php 还有一个显示列表,list.php
<? include TPL_TOP ?>
<a href="?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>
<? include TPL_BOTTOM ?>
always start php like this <?php, usually php manual configuration do not support short tag like this <?. 总是像这样启动PHP <?php,通常php手动配置不支持像这样的短标签<?。 for hyperlink just use view record It is just query string and get id on next page like this $id = $_GET['id']; 对于超链接,只需使用视图记录即可。它只是查询字符串,并在下一页上获得ID,例如$ id = $ _GET ['id']; Hope u will understand.. 希望你能明白..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.