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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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??

 

Link to comment
Share on other sites

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 . . .

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<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'].">

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.