answer me these questions.... ok just 1


#1

Okie dokie.
So im pretty new to the whole database thing, but I’m a fairly seasoned designer. so I knows my structural and presentational markup.
Regardless of that, here’s my dilemma.

I have a pretty good little app brewing but I’ve run into a couple of snags.

– I have a list that it actually table cells being populated from my database.

This goes like this :

[code]$query = “SELECT id, title from $tableName ORDER BY title”;
$result = mysql_db_query ($dbName, $query, $link);

while ($row = mysql_fetch_array($result)) {
echo ("

n");
echo ("$row[title] n");
echo ("");
echo (" n");
}[/code]

So what is happening is the list is being formed with just the Title information. Then it makes a link for the Title to an edit page with the row id appended. ( for the Get). So, I’ve started putting checkboxes on each table row as you can see… and I would like to be able to delete the DB rows dependant on the checks. Im not sure if attaching the same id# is what I should be doing with the checkboxes. Im just not really sure how they are going to recieve all the data. Could someone explain this to me??


#2

It looks right to me – try this: on the page that receives the POSTed form (to delete the checked records), set up a debugging system. First step, just print out the $_POST[] array. Then write the delete SQL queries, and instead of executing them, just echo them to the page to make sure things are working right. Then you can actually run the SQL.


#3

OK i have done this before to debug…
Im mostly stuck with how the delete is going to function.
Ex.
3 of the checkboxes are checked, how is that read?
Should i set up some sort of new array for it?

Thats the part that I’m stuck on.

:o

#4

You will see how the data comes in when you print out $_POST[]. Since you named your checkboxes delete[] (rather than just delete), PHP will make an array ($_POST[delete][]) of the values checked (the browser will not send the other values; a checkbox is only sent if it is checked). You are using the row id as the value of each checkbox, which is good, so the $_POST[delete][] array should contain the row id’s you want to get rid of.

From there, you should check the data (permission to delete, if you are running an authentication/authorization system; sane row IDs; no SQL injection attacks, etc), then write a single delete query like:

DELETE FROM table WHERE row_id = ‘8’ OR row_id = ‘12’ OR …

You would probably use foreach() on the $_POST[delete][] array to add each id to the base delete query. Make sure at least one value exists; depending on how you write the code you could end up deleting everything if there is no row id.

One alternative approach I am thinking more and more is a good idea is to add a date column to the DB and simply tag items as deleted with the time somebody requests that. (Modify all your SQL to ignore items with ‘deleted’ in the past, and index that column for speed.) Then if you want, you can run a regular maintenance script that will examine the ‘deleted’ dates and issue the real SQL DELETE on items that are more than 30 days old, or something like that. This has some implications for caching queries, if you’re doing that, and it would probably work better in a db with views (faster, cleaner SQL).


#5

I see… this makes alot of sense.
The only thing I didn’t get ( please bear with me :roll:) is the $_POST.
It’s not anywhere in the book I am reading.
I echoed it on the next form processing page, but it’s giving me a parse error.

Is it to be defined someplace?
Thanks for the continued help!


#6

In all recent versions of PHP (I believe from 4.1 on), $_POST[] is a superglobal array containing all of the data sent by the browser via form POST. There is a similar $_GET[] for variables from the URL. PHP takes care of setting these up as a normal part of receiving an HTTP request, and they can be referenced anywhere.

There are more superglobal arrays like these; see http://www.php.net/variables.predefined

Can you show the code including the $_POST reference that gives you the error?


#7

[code]

<?php echo("$_POST[]"); ?> [/code]

Here is the result page.


#8

and here;s the page that POSTs.

[code]

<?php while ($row = mysql_fetch_array($result)) { echo (" n"); echo ("$row[title] n"); echo (""); echo (" n"); } mysql_close ($link); ?>[/code]

#9

to view an array use print_r rather then echo.