Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Passing Variables through the URL

Status
Not open for further replies.

JamesCliff

Programmer
Feb 16, 2005
106
GB
Hi all,

I need abit of help on a couple of things:

1. Ive created two pages edit.php and delete.php. For now we will concentrate on delete.php. I have a list.php which sends the specific SaleID variable to delete.php for a selected record. Below is a portion of code that exists in list.php:-

Code:
	while($result = mysql_fetch_array($rslt))
	{
	
	 echo "<table>";
	 
     echo "<tr>";
	 
	 echo "<td width=\"50\"><a href=\"photos/" . $result['Image_Ref'] . "\" target=\"_blank\">View Picture</a></td>"; 
     echo '<td width="150">' . $result['Title'] . '</td>
	       <td width="260">' . $result['Description'] . '</td>
		   <td width="120">' . $result['Price'] . '</td>
		   <td width="110">' . $result['Date'] . '</td>

		   </tr>';
		   
		   echo "<tr>";
	 
           echo '<td colspan="5">&nbsp;</td>
		   
	             </tr>';

		   
	 echo "<tr>";
	 echo '<td><a href="admin/sales/edit.php?SaleID=' . $result['SaleID'] .'">edit</a> | <a href="admin/sales/delete.php?SaleID='. $result['SaleID'] .'">delete</a></td>
		   
		   </tr>';

As you can see above when examining the code, for each record there is an edit and delete link, these links are generated by php, which specifies the SaleID for each record. The variables are passed through the URL to the specific page. So for example a record with the SaleID of 3, is then specified to be deleted by clicking the delete link, the php passes the url of: delete.php?SaleID=1
This giving the SaleID to delete.php, so the passed SaleID is deleted. The query code for the delete.php is:

Code:
$query = "DELETE FROM sales WHERE SaleID = '$SaleID'";
$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());

// close database connection
mysql_close($connection);

// print result
echo "<font size=-1>Sale item has been deleted from the database. <a href=[URL unfurl="true"]http://www.briskfire.com/gb/index.php?page=admin/sales/admin>Go[/URL] back to the
admin menu</a>.</font>";

However when i click the delete link for any record on the list.php the link works and the delete success message is displayed. However the record is not deleted from the database, this leads me to believe that there is a problem with the query.

You can view and test the above at:




2. Is is possible to pass multiple variables to multiple pages within a url?

For example my site is structered using php and the include function. There is an index.php which is coded to include pages passed within the url. So to view the sales page the URL would be: index.php?page=sales

However the script im working on above also requires variables to be passed within the URL eg.
delete.php?SaleID=19

The list.php and delete.php would be displayed within the index.php as you can tell when you view the site on the link posted above.

So im confused at whether i can pass both of these variables in the URL at the same time, and if possible how would i do this? For example would it be something along these lines:

index.php?page=admin/sales/delete?SaleID=19

I am probably completly wrong, and would be greatful if someone could shed some light on this for me.


I hope it is possible to address both of my issues.

Thanks alot

Jim
 
try this:
Code:
$query = "DELETE FROM sales WHERE SaleID = '".$_GET['SaleID']."';
$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());

or set $saleID = $_GET['SaleID'];

then:
Code:
$query = "DELETE FROM sales WHERE SaleID = '$saleID';
$result = mysql_query($query) or die ("Error in query: $query. " .
mysql_error());

Regards,

Martin

Gaming Help And Info:
 
Thanks alot for that code m8

Works a treat

I am now able to delete records from the database.

However im still stuck on problem 2. As the variable is passed through the URL to the delete.php i need the delete.php to be included within the index.php. But im not sure howto do this or even if it can be done.

As i said above my site is based upon the index.php which includes other pages using the following:
<b>index.php?page=sales</b>

And the list.php is included in the index.php as the following:
<b>index.php?page=admin/sales/list</b>

This includes 2 links which are edit and delete. These links pass variables of there own eg. the delete link passes the variable to delete.php?SaleID=4, this link is found within the list.php which is included within the index.php of my site.

I want the delete.php to be included as well however i dont know if it is possible and if it is howto pass multiple variables to multiple pages within the URL
eg. index.php?page=admin/sales/delete?SaleID=4 (that link is just an example and wont work i very much doubt)

What im after is the correct way to do what im trying to do above, if it is actually possible. How would i pass multiple variables to multiple pages basiclly, once i know this i can add the index.php?page=admin/sales/delete...... to the link.

Thanks

Jim
 
When adding multiple variables to the URL address, you delimit any subsequent to the first with an ampersand not the question mark. Think of it as if the question mark tells you there are variables there and ampersand is used to delimit them try to print:
Code:
[URL unfurl="true"]http://www.briskfire.com/gb/index.php?page=admin/sales/delete[/URL][b]&amp;[/b]SaleID=14
 
Jim

why do you need to point the link at delete.php? instead consider pointing the delete link at index.php and test the condition of whatever variable at the start of the index file and include the scripts as you wish.

eg.
Code:
if (isset($_REQUEST['actiontype']))
{
  switch ($_REQUEST['actiontype'])
  {
     case "delete":
      include "./admin/sales/delete.php"; //delete script will run here.  if the script is actually a function then ...
     // delete($_GET['SalesID']);
      break;
     //etc
  }
}

for a fuller example of an auto-processing page (which this would be), have a look at the code i posted on this thread: thread434-998288

one other comment: always escape your GPC variables before using them in a sql query. thus don't use
Code:
$sql = "Delete * from table where `id` = '" . $_GET['SalesID'] ."'";
instead use
Code:
$id = mysql_escape_string($_GET['SalesID']);
$sql = "Delete * from table where `id` = '$id';
 
Ok thanks for the feedback lads,

The above worked :)

Im looking to slightly modify my script in the sense that it deletes the actual specific image file as well from the photos directory.

The delete script already deletes a full table row based on the SaleID passed to it through the URL. What i would like is for the script to delete the named image in the photos directory, as well as erasing its reference from the database.

I realise this is alittle more advanced, and could be hard to do, but any help would be greatful.

Ive started learning howto intergrate linux bash scripts into php, however i do not have the knowledge to pull something like this off.

So any help would be appriciated.

Thanks alot

Jim
 
if you know the filename of the image just use
Code:
unlink($path.$filename); //this deletes the file
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top