NameSilo

Question about setting a variable in Php

SpaceshipSpaceship
Watch

TwiztedFake

Established Member
Impact
1
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.
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
PHP:
$cib = 'b'.$id;
 
0
•••
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:
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.';
	}
 
0
•••
In your first mysql_query, change it to this:

PHP:
$result = mysql_query($sql)or die('SQL Error:<ul><li>'.$sql.'</li><li>'.mysql_error().'</li></ul>');

That should show the error message.
 
0
•••
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
 
0
•••
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
 
0
•••
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:
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.';
	}
 
0
•••
Appraise.net

We're social

Spaceship
Domain Recover
CatchDoms
DomainEasy — Live Options
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back