Board index   FAQ   Search  
Register  Login
Board index PHP PHP Scripts

Deletion of an array

Links for php scripts

Moderators: macek, egami, gesf

Deletion of an array

Postby icu90ucme » Mon Aug 04, 2003 7:23 pm

Here's the problem I retrieve a list a users from my database.
I now want the option to delete each user from my database.
Here's the code I'm using to retrieve the users:
Code: Select all
function get_users($username)
{
  //extract from the database all the URLs this user has stored
  if (!($conn = dbconnect()))
    return false;
  $result = mysql_query( "select usrfname, usrlname
                          from usrinfo
                          where username = '$username'");
  if (!$result)
    return false;

  //create an array of the URLs
while ($row = mysql_fetch_assoc($result)) {
        $usr_array[] = $row;
}   
  return $usr_array;
}


i print them out using this code:
Code: Select all
  if (is_array($usr_array) && count($usr_array)>0)
  {
    foreach ($usr_array as $usr)
    {
      echo "<tr bgcolor=$color>
      <td>$usr[usrfname]  $usr[usrlname]</td>";
      echo "<td><input type=checkbox name=\"del_me[]\"
             value=\"$usr\"></td>";
      echo "</tr>";
    }
  }

so far so good the viewing part works now
i try to delete them using this code
Code: Select all
    if (count($del_me) >0)
    {
      foreach($del_me as $usr)
      {
        if (delete_usr($valid_user, $usr))
          echo 'Deleted';
        else
          echo 'Could not delete';
      } 
    }

using the function:
Code: Select all
function delete_usr($valid_user, $usr)
{
  // delete one user from the database
  if (!($conn = dbconnect()))
    return false;

   // delete the user
  if (!mysql_query( "delete * from userinfo
                       where username='$valid_user'
                       and usrfname='$usr[usrfname]'
                       and usrlname='$usr[usrlname]' "))
    return false;
  return true; 
}


does this code seem logical because i always get a result of "Could not delete" and i don't why it stops at that point. i know it has something to do with passing the array but i don't know how to fix it.
icu90ucme
New php-forum User
New php-forum User
 
Posts: 23
Joined: Mon Jul 07, 2003 2:29 pm

Postby sigix » Tue Aug 05, 2003 2:20 am

:!:
try echoing Message in each function
in get_users echo message Showing
in delete_users echo message Deleing user .......
do like this

dbConnect() or die("No Host/Db connection");
mysql_query(.........) or die ("Sql Problem");
will show that there is some problem in your query

:arrow:
sigix
php-forum Active User
php-forum Active User
 
Posts: 364
Joined: Mon Jul 14, 2003 9:39 pm
Location: /Earth/Xion

Re: Deletion of an array

Postby WiZARD » Tue Aug 05, 2003 6:40 am

see what you do:
Code: Select all
function get_users($username)
{
  if (!($conn = dbconnect()))
    return false;

.....

maby best if you do it:
Code: Select all
function get_users($username)
{
  if (!($conn == dbconnect()))
    return false;

.....

or
Code: Select all
function get_users($username)
{
  if (!$conn)
    return false;

.....

where $conn it's:
Code: Select all
$conn=pconnect($host, $user, $password);
User avatar
WiZARD
Moderator
Moderator
 
Posts: 1257
Joined: Thu Jun 20, 2002 10:14 pm
Location: Ukraine, Crimea, Simferopol

Postby sigix » Tue Aug 05, 2003 11:13 pm

may be he is returning value from dbconnect()
:wink: if (!($con=dbconnect()))

doing this. if ($con==dbConnect() ) will match returned value to $con :!: then execute the if statements
sigix
php-forum Active User
php-forum Active User
 
Posts: 364
Joined: Mon Jul 14, 2003 9:39 pm
Location: /Earth/Xion

Postby Joel » Wed Aug 06, 2003 2:36 am

You should use something like this for deleting

"DELETE FROM table WHERE field = $var && field2 $var"

Then to see the mysql error use the good old

[code][/code]mysql_query($sql) or die("Delete Error".mysql_error());
Joel
New php-forum User
New php-forum User
 
Posts: 193
Joined: Sat Mar 29, 2003 11:57 pm
Location: Auckland, New Zealand


Return to PHP Scripts

Who is online

Users browsing this forum: No registered users and 1 guest

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron