YAY, I had a go myself and it works, but could do with a pointer (PHP)

Permabanned
Joined
22 Apr 2007
Posts
1,805
Ok gents, I've started getting to grips with this now.

I want to display two fields from a MySQL table on to the webpage, so I've done this

Code:
<?php

$address = "localhost";
$username = "root";
$password = "";
$database = "mark1e_dmd";

mysql_connect($address,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
    
$query = "SELECT p_title, p_summary FROM news";
$result = mysql_query($query)
or die("Query failed : " . mysql_error());

    
    print "<table border=1>\n";
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        print "\t<tr>\n";
        foreach ($line as $col_value) {
            print "\t\t<td>$col_value</td>\n";
        }
        print "\t</tr>\n";
    }
    print "</table>\n";
    mysql_free_result($result);

    
mysql_close();
?>

That works great.

But what I want to do is provide a tick box (I guess) in the displayed table which if user clicks 'OK', 'Update' or whatever, drops that row from the table.

I'm not sure how to do this bit.

Thanks
 
you need a way to uniquely identify each row in the database table independent of time and changes to other rows in the table, so you can put that identifier inside a html input name/value inside your html table to pass to the database for a update/delete.

best way, in my opinion, is to add an extra 'id' column to your table, of type unsigned integer, unique and automatically incrementing (auto_increment in mysql), so each row gets a unique id by default. select that id along with the columns in the query you're doing at the moment, and reference that id inside an html input control to do the modification on each row, ie:

Code:
print "\t\t<td>$col_value</td><td><input type=\"button\" name=\"remove_$id\" /></td>\n";

then, at the start of your script, you can check $_REQUEST for the presence of any keys of the format "remove_<id>", get the id using a string function or regular expression, and construct an sql query to delete the row corresponding to that id from the table.

similarly for updating/editing.

if for some reasons you can't modify the structure of the database, you could instead compute a hash of the data you're returning and use that as a id - ie:

SELECT md5(concat(p_title, p_summary)) as id, p_title, p_summary FROM news";

- of course the performance of that method doesn't scale nearly as well as using a seperate id column.

hope this helps
 
Hah! Cool. I already have an id field in the table so every news article has an ID associated with it

I dont fully understand your $REQUEST bit though

Thanks so far
 
well, $_REQUEST is an array of values passed to the PHP script, taken from the request URL passed to the webserver (form items usually).

You can use any of PHP's array functions to search/extract them.

Printing them out at the top of your script with something like :

Code:
echo "<pre>"; print_r($_REQUEST); echo "</pre>\n";

- might be useful for you to see what is happening with the form data (the html input elements will contribute to the form data)
 
$_REQUEST contains $_POST, $_GET and $_SESSION variables. It might contain more, but that's all I ever use it for
 
Basically what matia is saying is that you need to have some way of getting your users selection into your PHP script. This is usually done using html forms, of the type you see everywhere on the web (including the reply box on this forum for example).

Your generated table will probably be something like
Code:
<table>
  <tr>
    <td>Title1</td><td>Summary1</td>
  </tr>
  <tr>
    <td>Title2</td><td>Summary2</td>
  </tr>
</table>

You'll need to make it look more like this:

Code:
<form name="newsEdit" action="newsDelete.php" method="post">
<table>
  <tr>
    <td>Title1</td><td>Summary1</td><td><input type="submit" name="delete_1" value="delete"></td>
  </tr>
  <tr>
    <td>Title2</td><td>Summary2</td><td><input type="submit" name="delete_2" value="delete"></td>
  </tr>
</table>
</form>

So that each row has a delete button which is processed by the newsDelete.php script.

Using a form is a much better than using a link, because if your site ever gets indexed by a search engine, you don't want it accidentally deleting records because it followed a "delete" link :)
 
Back
Top Bottom