Jump to content

[SOLVED] I have my mySQL Result in an HTML table . . How can I make a link to edit a sele


2tonejoe

Recommended Posts

I have my mySQL Result in an HTML table . . How can I make a link to edit a selected row in the html table. . . .

 

I mean. I have a query that throws the results in a table and displays it. I add an extra cell at the end where I have an

<a href="#">edit this record</a> 

. So my question is . . how can I send something to another php page that displays the selected row so it can be UPDATE'd.....?

 

anyone have any ideas? please let me know if I am not explaining this enough.

 

Cheers Guys! GREAT FORUM!

ok you may have a code like this:

 

while($rows = mysql_fetch_array($query)){
    echo "<a href=\"edit.php?id={$rows['id']}\">Edit this row</a>" ;
}

 

In this way you redirect the user to a edit.php where the id of the row he wanna edit is in the id variable. I guess u are able to write the rest of the code by yourself now.

No the $_FILES superglobal is for file uploads, in this case u must use $_GET. Take this sample code:

 

$id = $_GET['id'];
$query = mysql_query("SELECT * FROM table WHERE id='$id'");
$values = mysql_fetch_array($query);
echo $values['name']; echo $values['age'];

 

When u open 'edit.php?id=5', the id=5 part is a variable 'id' which is assigned the value '5'. U get that id with $_GET and just make a sql query to show the results for that row. Hope u got it now.

ok. I am using the code that guilty provided and I am getting this error message:

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /location/of/my/file on line 13

 

line 13 looks like this:

$values = mysql_fetch_array($query);

 

the top half of the code looks like this:

<?php

include('./db-connnect.php');


mysql_select_db("addison") or die(mysql_error());

$id = $_GET['id'];
$query = mysql_query("SELECT * FROM users WHERE id='$id'");
$values = mysql_fetch_array($query);

 

does anyone have any ideas??

 

you were right. . . . not the table, but the field. . . my field was named user_id no id.    ::)

 

my data is not display though. . . . what is wrong with this?

 

$id = $_GET['id'];
$query = mysql_query("SELECT * FROM users WHERE user_id='$id'");
$values = mysql_fetch_array($query);

echo "<table style=\"width: 800px; height: 98px; text-align: left; margin-left: auto; margin-right: auto;\" border=\"0\" cellpadding=\"2\" cellspacing=\"2\">";
echo "<tr><td>";
echo "<fieldset><legend>Edit ".$values['user_name']."</legend>";
echo "<form enctype=\"multipart/form-data\" action=\"/scripts/add-a-user.php\" method=\"POST\">";
echo "<br>  ";
echo "<input name=\"name\" value=\"".$values['user_name']."\">  Full Name<br>";
echo "<br>  ";
echo "<select selected=\"".$values['user_facility']."name=\"facility\">";

 

that isn't all of it, but the rest is the same . . .

as always. . . . another question . .

 

so I have everything working. . . its cool.

 

all i am wondering now is if I can get the links to work with my site template. I made a site index that makes use of 'case' and switches the main site content based on the '$_REQUEST'. here is the code on the main page:

 

<?php
switch($_REQUEST['page']) {
case 'default':
case 'upload-image':
case 'upload-db':
case 'user-manager':
case 'edit-a-user':
case 'add-a-user':
case 'display':
case 'history':
case 'contact':
include($_REQUEST['page'].'.php');
break;
default:
include('default.php');
break;
}
?>

 

so my question would be is can I and how could I get the linking code:

"<a href=\"edit-a-user.php?id=".$row['user_id'].">" 

to work with my existing linking schema

<a href="index.php?page=edit-a-user">

. . .? any thoughts.

<a href="index.php?page=edit-a-user?id=".$row['user_id'].">

look at this code of yours

if you have another querystring then you have to like separate them with & not ?

so it will look like this

<a href="index.php?page=edit-a-user&id=".$row['user_id'].">

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.