robcrozier Posted March 13, 2006 Share Posted March 13, 2006 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!!!! Quote Link to comment https://forums.phpfreaks.com/topic/4841-passing-a-variable-through-the-url/ Share on other sites More sharing options...
trq Posted March 13, 2006 Share Posted March 13, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/4841-passing-a-variable-through-the-url/#findComment-17026 Share on other sites More sharing options...
robcrozier Posted March 13, 2006 Author Share Posted March 13, 2006 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??? Quote Link to comment https://forums.phpfreaks.com/topic/4841-passing-a-variable-through-the-url/#findComment-17037 Share on other sites More sharing options...
kenrbnsn Posted March 13, 2006 Share Posted March 13, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/4841-passing-a-variable-through-the-url/#findComment-17046 Share on other sites More sharing options...
complex05 Posted March 13, 2006 Share Posted March 13, 2006 don't use the * in your queryjust mysql_query("DELETE FROM dvd WHERE title='$title'"); Quote Link to comment https://forums.phpfreaks.com/topic/4841-passing-a-variable-through-the-url/#findComment-17048 Share on other sites More sharing options...
robcrozier Posted March 13, 2006 Author Share Posted March 13, 2006 Cheers everyone that what i was looking for and its working perfectly now!!! Quote Link to comment https://forums.phpfreaks.com/topic/4841-passing-a-variable-through-the-url/#findComment-17065 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.