Reputation: 575
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.
My search works great, and im really chuffed with myself as I am a php and mysql novice.
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.
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? 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
Upvotes: 0
Views: 717
Reputation: 169
always start php like this <?php, usually php manual configuration do not support short tag like this <?. for hyperlink just use view record It is just query string and get id on next page like this $id = $_GET['id']; Hope u will understand..
Upvotes: 0
Reputation: 157869
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
<? 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
<? 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 ?>
Upvotes: 0