[advanced search]
 

Go Back   NamePros.com > Discussion > Web Design & Development > Programming

Programming PHP, Perl, Ruby on Rails, AJAX, HTML, XHTML, CSS, JavaScript, MySQL and any other coding topics.


Closed Thread
 
LinkBack Thread Tools
Old 10-23-2006, 05:10 PM   #1 (permalink)
NamePros Regular
 
TwiztedFake's Avatar
 
Join Date: Apr 2004
Location: IL
Posts: 339
50.00 NP$ (Donate)

TwiztedFake is on a distinguished road


question about setting a variable in php

I'm working on a comments script for my site. The script uses one table to store all the data for every comment. In that table I have a column that is named cid which tells the script what it belongs too. So here is my problem.

I start by getting the id of what the comment is left for

$id = $_GET['id']

then I want it to add another letter to it that will be used for pulling the data out when a page is loaded

$cid = b + $id

the b is a letter i'm using for a specific area of the site for example right now the b means its a blog comment. I am also going to have a p$id which will be a photo comment id. So how do I write the variable to add the corresponding letter to it based on what the form is telling it.
__________________
Four Seasons Outfitters
[ 25 $NP/Month ] [ 25 $NP/Month ] [ 25 $NP/Month ]
[ 25 $NP/Month] [ 25 $NP/Month]
TwiztedFake is offline  
Old 10-23-2006, 06:05 PM   #2 (permalink)
Barru.
 
Barrucadu's Avatar
 
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,731
78.50 NP$ (Donate)

Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold


PHP Code:
$cib = 'b'.$id;
Barrucadu is offline  
Old 10-23-2006, 06:49 PM   #3 (permalink)
NamePros Regular
 
TwiztedFake's Avatar
 
Join Date: Apr 2004
Location: IL
Posts: 339
50.00 NP$ (Donate)

TwiztedFake is on a distinguished road


Got the insert $cid problem fixed but now having an issue with mysql I've built a function for the adminCP to delete blogs and their comments. The function is giving the following error when it is ran.

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/content/t/w/i/twiztedfake/html/admincp.php on line 441

What is weird is that the function is doing everything it is suppose to be doing. Which is delete the blog from the blogs table and delete all comments from the comments table that goes with the blog. I checked the database and they have all been deleted!

The function:
PHP Code:
function delete()
    {
        
$ID = $_GET['id'];
        
$table = $_GET['table'];
        
$table = 'ct_'.$table;
    
        if (
$table == 'ct_blog')
            {
                
$type = "b";
                
$cid = $type.$ID;
                
$sql = "SELECT * FROM ct_comments WHERE cid = '$cid'  ORDER BY ID DESC";
                
$result = mysql_query($sql);
                
$num_rows = mysql_num_rows($result);
                while (
$row = mysql_fetch_array($result)) // This is line 441 in my file
                    
{
                        
$sql = "DELETE FROM ct_comments WHERE cid = '$cid'";
                        
$result = mysql_query($sql);
                            if (!
$result)
                                {
                                    
stop('Failed to delete comments for entry from database. Error returned is '.mysql_error());
                                }
                        echo
'Comments sucessfully deleted.';
                    }
            }
            
        
$sql = "DELETE FROM $table WHERE ID = '$ID' LIMIT 1";
        
$result = mysql_query($sql);
        if (!
$result)
            {
                
stop('Failed to delete entry from Database. Error returned is '.mysql_error());
            }
        echo
'Entry sucessfully deleted.';
    }
__________________
Four Seasons Outfitters
[ 25 $NP/Month ] [ 25 $NP/Month ] [ 25 $NP/Month ]
[ 25 $NP/Month] [ 25 $NP/Month]
TwiztedFake is offline  
Old 10-23-2006, 07:24 PM   #4 (permalink)
Barru.
 
Barrucadu's Avatar
 
Join Date: Aug 2005
Location: East Yorkshire, England
Posts: 2,731
78.50 NP$ (Donate)

Barrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to beholdBarrucadu is a splendid one to behold


In your first mysql_query, change it to this:

PHP Code:
$result = mysql_query($sql)or die('SQL Error:<ul><li>'.$sql.'</li><li>'.mysql_error().'</li></ul>');
That should show the error message.
Barrucadu is offline  
Old 10-23-2006, 07:52 PM   #5 (permalink)
Dan
Buy my domains.
 
Dan's Avatar
 
Join Date: Feb 2006
Posts: 2,801
56.00 NP$ (Donate)

Dan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant futureDan has a brilliant future

Autism Autism Autism Autism Autism Autism Autism
Change
Code:
                while ($row = mysql_fetch_array($result)) // This is line 441 in my file
To
Code:
                while ($row = mysql_fetch_assoc($result)) // This is line 441 in my file
Dan is offline  
Old 10-24-2006, 02:10 PM   #6 (permalink)
NamePros Regular
 
TwiztedFake's Avatar
 
Join Date: Apr 2004
Location: IL
Posts: 339
50.00 NP$ (Donate)

TwiztedFake is on a distinguished road


Mikor - I get the same error I'm already getting.

Dan - Same thing, code still works like it should be gives me that error. I'm going to try a different way of writing it later on. Right now I got some other things to work on.

Thanks for the help guys
__________________
Four Seasons Outfitters
[ 25 $NP/Month ] [ 25 $NP/Month ] [ 25 $NP/Month ]
[ 25 $NP/Month] [ 25 $NP/Month]
TwiztedFake is offline  
Old 10-24-2006, 04:43 PM   #7 (permalink)
NamePros Regular
 
TwiztedFake's Avatar
 
Join Date: Apr 2004
Location: IL
Posts: 339
50.00 NP$ (Donate)

TwiztedFake is on a distinguished road


After looking at I realised where my mistake was. Basically I over coded it. No need for the while statement since I want to delete every entry that has the same id based on what was passed on to the function. So I removed all the first query and it works fine and no errors.

I just left it alone for awhile and it came to me what I needed to do. In the original function it selected * from the table then had a while statement but in that statement it was deleting all the entries on the first round. So when it looped back it had conflicting info. One said it still had more entries when really they were already gone. Here is my edited function that now works with no errors.

PHP Code:
function delete()
    {
        
$ID = $_GET['id'];
        
$table = $_GET['table'];
        
$table = 'ct_'.$table;
    
        if (
$table == 'ct_blog')
            {
                
$type = "b";
                
$cid = $type.$ID;
                
$sql = "DELETE FROM ct_comments WHERE cid = '$cid'";
                
$result = mysql_query($sql)or die('SQL Error:<ul><li>'.$sql.'</li><li>'.mysql_error().'</li></ul>');
                    if (!
$result)
                        {
                            
stop('Failed to delete comments for entry from database. Error returned is '.mysql_error());
                        }
                    echo
'Comments sucessfully deleted.<br />';
            }
            
        
$sql = "DELETE FROM $table WHERE ID = '$ID' LIMIT 1";
        
$result = mysql_query($sql);
        if (!
$result)
            {
                
stop('Failed to delete entry from Database. Error returned is '.mysql_error());
            }
        echo
'Entry sucessfully deleted.';
    }
__________________
Four Seasons Outfitters
[ 25 $NP/Month ] [ 25 $NP/Month ] [ 25 $NP/Month ]
[ 25 $NP/Month] [ 25 $NP/Month]
TwiztedFake is offline  
Closed Thread


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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


Site Sponsors
Advertise your business at NamePros

All times are GMT -7. The time now is 07:41 AM.


Powered by: vBulletin® Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0
Template-Modifications by TMS
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85