Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > PHP

Reply
 
LinkBack Thread Tools Display Modes
Old 03-09-2003, 12:02 PM   #1 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Return an array with register_globals = off

I've got a script that works perfectly with register_globals turned on, and breaks with them off. It's pretty obvious that the array just isn't available with globals turned off, and I've run out of ideas on how to make this work. A mock-up of my script is shown below. What it does is grab the values from an SQL query, put those values into an array that are passed with POST to the script. Remember, this works as is.

PHP Code:
<?php
// include the link
require_once("../link.php");
// establish vars
$PHP_SELF $_SERVER['PHP_SELF'];
$row_id $_POST['row_id'];
$db mysql_select_db("test");
$db_table "test_table";

if (
$_POST['submit'] == "Delete") {
foreach (
$del_row as $row_id) {
            
$result mysql_query("DELETE FROM $db_table WHERE id = $row_id");
            
$i++;
        }
        echo 
"<div align=center>\n";
        echo 
$i " deleted.\n<br><br>\n";
        echo 
"[<a href=\"$PHP_SELF\">Delete more records.</a>]\n<br>\n</div>";
} else {
echo
"<html>\n<head>\n<title>Delete Database Records</title>\n</head>\n<body>\n";
        echo 
"<div align=center>\n";
        echo 
"<strong>Delete Database Records</strong><br><br>\n";
        
        
$query "SELECT * FROM $db_table ORDER BY id";
        
$result mysql_query($query);
        
$num_rows mysql_num_rows($result);
        if (
$num_rows == "0") {
            die(
"<font color=red>No records found.</font>\n</div>\n</body>\n</html>");
        } else {
            echo 
$num_rows " record(s) found.<br>\n";
            echo 
"<form method=post action=\"$PHP_SELF\">";
            echo 
"<table border=1 cellpadding=1 cellspacing=1 width=\"50%\">";
            echo 
"<tr>\n";
            echo 
"<td align=center>Name</td>\n";
            echo 
"<td align=center>Serial #</td>\n";
            echo 
"<td align=center>Delete?\n";
            echo 
"</tr>\n";
            
            while (
$row mysql_fetch_array($resultMYSQL_ASSOC)) {
                
// define the row id
                
$row_id $row['id'];

                echo 
"<tr>\n";
                echo 
"<td align=center valign=center>" $row['name'] . "</td>\n";
                echo 
"<td align=center valign=center>" $row['serial_number'] . "</td>\n";
                echo 
"<td align=center valign=center>\n";
                echo 
"<input type=checkbox name=\"del_row[]\" value=\"$row_id\"></input>\n";
                echo 
"</td></tr>\n";
                }
        }

        echo 
"<tr>\n";
        echo 
"<td align=left valign=center>\n</tr>\n";
        echo 
"<tr>\n<td>\n";
        echo 
"<input type=submit name=submit value=\"Delete\"></input>\n";
        echo 
"<input type=reset value=\"Reset\"></input>\n";
        echo 
"</td>\n</tr>\n</table>\n</form>\n<br><br>\n";
}
?>
Now, how would I go about returning the value of del_row[] without globals on? Ideas?
bdl is offline   Reply With Quote
Old 03-09-2003, 01:57 PM   #2 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
return it where? you don't have a function? you can't "return" anything without a function...


also, naming a html object with square brackets is considered bad form...

Quote:
echo "<input type=checkbox name=\"del_row[]\" value=\"$row_id\"></input>\n";
and naming a bunch of html objects with the same name is also not a great idea.. hmm.


what are you trying to achieve?
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 03-09-2003, 02:19 PM   #3 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Quote:
Originally posted by abc123
return it where? you don't have a function? you can't "return" anything without a function...
You're right, I simply mean that it returns the value to the script from POST. When you hit 'submit', the script calls on itself to validate the form input and then take those values and do something with them. In this case, it loops the array through MySQL's DELETE command to delete each row you've selected, with the value of $row_id.

Quote:

also, naming a html object with square brackets is considered bad form...
Actually, I gleaned that bit from a book on PHP/MySQL that I've got, if there is another way of doing it I'd gladly learn. I'm not a professional PHP programmer.

Quote:

and naming a bunch of html objects with the same name is also not a great idea.. hmm.
?

Quote:

what are you trying to achieve?
Exactly what the script does now; gives you a dynamic table with a selection of rows from MySQL 'SELECT' and allows you to delete those rows from the database. I also have a similar script that does the same thing but gives the user the ability to actually update the database based on new data, using basically the same method.

This is all part of a website I'm putting together for coworkers to access / modify a database of products.
bdl is offline   Reply With Quote
Old 03-09-2003, 06:35 PM   #4 (permalink)
Admin
$_['Your_Mom'];
 
Admin's Avatar
 
Join Date: May 2002
Location: Santee
Posts: 627
Admin is on a distinguished road
Quote:
Originally posted by abc123
and naming a bunch of html objects with the same name is also not a great idea.. hmm.
since they are check boxes wouldnt it return an array for every checkbox checked?

then you can use that to delete the rows from the table.

i would do multiple queries i would run something like this.

delete from TABLE where id = '1' or id = '2' or id = '8'

i like to keep queries to a minimum.
__________________


Urban Clothing
Admin is offline   Reply With Quote
Old 03-10-2003, 05:12 AM   #5 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Quote:
Originally posted by Admin
since they are check boxes wouldnt it return an array for every checkbox checked?

then you can use that to delete the rows from the table.

i would do multiple queries i would run something like this.

delete from TABLE where id = '1' or id = '2' or id = '8'

i like to keep queries to a minimum.
Right, what it does is returns an array like so:

$del_row['$row_id']

and the the foreach loop steps through and deletes each entry in the array. The problem I have is, this works perfectly when register_globals is on, and it breaks if turned off. I was reading a similar problem on another site, someone was suggesting using $_SESSION to store the array and use those values, I'm going to look into that, otherwise I'm lost.
bdl is offline   Reply With Quote
Old 03-10-2003, 07:10 AM   #6 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
I guess the real title of the post should have been 'How to get an array with $_POST...', something like this:

$var = array[$key][$value];
PHP Code:
$del_row = array($_POST['del_row'][$_POST['key']][$_POST['row_id']]); 
??
bdl is offline   Reply With Quote
Old 03-10-2003, 01:28 PM   #7 (permalink)
abc123
bloomberg
 
abc123's Avatar
 
Join Date: Jun 2002
Location: bloomberg
Posts: 263
abc123 is on a distinguished road
Send a message via AIM to abc123 Send a message via Yahoo to abc123
given:

Code:
<input type="checkbox" name="test" /> a
<input type="checkbox" name="test" /> b
<input type="checkbox" name="test" /> c
<input type="checkbox" name="test" /> d
you should be able to access them as:

Code:
$POST["test"][0]  // a
$POST["test"][1]  // b
$POST["test"][2]  // c
if i remember correctly or else

$x = $POST["test"]; should return you the values in the form of "a, b, c, d"
__________________
-- bloomberg.
abc123 is offline   Reply With Quote
Old 03-10-2003, 05:37 PM   #8 (permalink)
bdl
Senior Contributor
 
Join Date: May 2002
Location: vta.ca.usa
Posts: 555
bdl is on a distinguished road
Ok, I figured it out. What I needed to do was simply use the value of del_row[] instead of getting $row_id like so:

PHP Code:
$del_row $_POST['del_row']; 
That contains the value of the array $del_row.

Thanks to everyone that looked it over for me!
bdl is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
OpenGL.dll Mr.Anderson Platform/API C++ 3 08-13-2004 10:07 AM
working with Array Goong MS Technologies ( ASP, VB, C#, .NET ) 3 07-22-2004 12:14 PM
breaking down an array from a form metazai PHP 12 07-09-2004 06:18 AM
array max num Apodysophilia Java 9 04-10-2003 06:36 AM
adding to an array in php sde PHP 1 06-12-2002 11:04 AM


All times are GMT -8. The time now is 05:55 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting