2tonejoe Posted July 27, 2007 Share Posted July 27, 2007 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! Quote Link to comment Share on other sites More sharing options...
Fadion Posted July 27, 2007 Share Posted July 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
2tonejoe Posted July 27, 2007 Author Share Posted July 27, 2007 yes, but the id=row. . . . how is the id= processed by my edit.php file . . ? is it simply $_FILES['id']; ?? thanks btw Quote Link to comment Share on other sites More sharing options...
Fadion Posted July 27, 2007 Share Posted July 27, 2007 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. Quote Link to comment Share on other sites More sharing options...
Fadion Posted July 27, 2007 Share Posted July 27, 2007 Also dont forget to clean the values u get from get or post with addslashes or mysql_real_escape_string: $id = mysql_real_escape_string($_GET['id']); Quote Link to comment Share on other sites More sharing options...
2tonejoe Posted July 27, 2007 Author Share Posted July 27, 2007 thanks a ton! i can tell this forum is going to work out well with people like you pitching in! cheers guiltyGear! Quote Link to comment Share on other sites More sharing options...
2tonejoe Posted July 28, 2007 Author Share Posted July 28, 2007 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?? Quote Link to comment Share on other sites More sharing options...
Fadion Posted July 28, 2007 Share Posted July 28, 2007 It is an error produced by the query, which in fact is written correctly. Im guessing ure using a non existing table 'users' which gets you the error. Quote Link to comment Share on other sites More sharing options...
2tonejoe Posted July 28, 2007 Author Share Posted July 28, 2007 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 . . . Quote Link to comment Share on other sites More sharing options...
2tonejoe Posted July 28, 2007 Author Share Posted July 28, 2007 nevermind . . . got it. Thanks again! Quote Link to comment Share on other sites More sharing options...
2tonejoe Posted July 28, 2007 Author Share Posted July 28, 2007 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. Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 28, 2007 Share Posted July 28, 2007 what??? Quote Link to comment Share on other sites More sharing options...
2tonejoe Posted July 28, 2007 Author Share Posted July 28, 2007 basically . .. could I send the link as <a href="index.php?page=edit-a-user?id=".$row['user_id']."> or would it fail? Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 28, 2007 Share Posted July 28, 2007 <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']."> Quote Link to comment Share on other sites More sharing options...
2tonejoe Posted July 28, 2007 Author Share Posted July 28, 2007 NICE! Thanks! Quote Link to comment Share on other sites More sharing options...
teng84 Posted July 28, 2007 Share Posted July 28, 2007 dude dont forget marking this as solved Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.