Jump to content

Archived

This topic is now archived and is closed to further replies.

robcrozier

passing a variable through the URL

Recommended Posts

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!!!!

Share this post


Link to post
Share on other sites
p1
[code]
<?php

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

?>
[/code]
p2
[code]
<?php

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

?>
[/code]

Share this post


Link to post
Share on other sites
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:

[code]
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>";
  }
[/code]

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

[code]

<?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();
}
[/code]

And here is the error message that i keep getting:

[code]
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
[/code]

Why the hell is it doing this???

Share this post


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

Ken

Share this post


Link to post
Share on other sites
don't use the * in your query

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

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.