Jump to content


Photo

passing a variable through the URL


  • Please log in to reply
5 replies to this topic

#1 robcrozier

robcrozier
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 13 March 2006 - 03:45 PM

How do you pass a variable from one page through the URL to another page using a link to do so?

in other words - i have a variable on one page and i want to click a link to redirect to another page, taking the variable with me so that i can use it on the next page too!

please help!!!!

#2 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 13 March 2006 - 03:53 PM

p1
<?php

  $var = 'foo';
  echo "<a href='page2.php?var=$var'>click</a>";

?>
p2
<?php

  if isset($_GET['var']) {
    echo $_GET['var'];
  }

?>


#3 robcrozier

robcrozier
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 13 March 2006 - 04:32 PM

hi again! Right the variable seems to be getting passed through (i can see this by echoing it in the new page) however why wont it let me use it in a mysql query?

heres where i pass the variable in through a link whilst printing out some rows from a database table at the same time:

while($nt=mysql_fetch_array($r))
  {
    echo "<tr>
    <td>$nt[title]</td>
    <td>$nt[description]</td>
    <td>$nt[age_rating]</td>
    <td>$nt[cast]</td> 
    <td width = 11%>$nt[date_added]</td>
    <td><a href='delete_entry.php?title=$nt[title]'>Delete</a></td>
    </tr>";
  }

And here is where it is being passed into the next page:


<?php

  
     $title = $_GET['title'];
     //echo $title;    
  

require_once ('mysql_connect.php'); // Connect to the database.

$query = "delete * from dvds where title = $title";
$result = mysql_query($query); 

if($result){
echo 'done';
mysql_close();
}
else{
echo mysql_error();
}

And here is the error message that i keep getting:

You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near '* from dvds where title = Matrix' at line 1 

Why the hell is it doing this???

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 13 March 2006 - 04:47 PM

You need to put single quotes around strings in the query:
<?php
$query = "delete * from dvds where `title` = '$title'";
?>
I also put back-ticks around your field name "title". These make sure it is not mistaken for a reserved word.

Ken

#5 complex05

complex05
  • Members
  • PipPipPip
  • Advanced Member
  • 124 posts

Posted 13 March 2006 - 04:57 PM

don't use the * in your query

just mysql_query("DELETE FROM dvd WHERE title='$title'");



#6 robcrozier

robcrozier
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 13 March 2006 - 05:50 PM

Cheers everyone that what i was looking for and its working perfectly now!!!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users