pjc2003 Posted January 5, 2007 Share Posted January 5, 2007 Hi ive created this page stock_control but i cant seem to get the SQL to pick up the value of a variable part_number that I have used the GET function to bring in from previous page.. heres my code.<?phprequire('./header.php');require('./dbconn.php');$action = 'show_form';if(!empty($_POST)) { $action= 'process_data'; $part_number = $_GET['part_number']; }if($action== 'process_data') {$location_name = trim($_POST['location']);$valid = intval(trim($_POST['valid']));$sql= "UPDATE stockdata ( location , valid) " ."VALUES ('$location_name', '$valid' )WHERE part_number=$part_number ";$ok= mysql_query($sql); if($ok){ echo "<p class='ErrorText'>Update OK!</p>"; } else { echo "<p class ='ErrorText'>Update Failed!</p>"; } } else { $action = 'show_form'; } echo $sql; //end running database queryif($action == 'show_form') {?><form method="post" action="stock_control.php"> <p class ='MainText'> Change Location - <select name="location"> <option =1 <?php if ($location_name ="A") echo ' selected '?> >A</option> <option =2 <?php if ($location_name ="B") echo ' selected '?> >B</option> <option =3 <?php if ($location_name ="C") echo ' selected '?> >C</option> <option =4 <?php if ($location_name ="D") echo ' selected '?> >D</option> <option =5 <?php if ($location_name ="E") echo ' selected '?> >E</option> <option =6 <?php if ($location_name ="F") echo ' selected '?> >F</option> <option =7 <?php if ($location_name ="G") echo ' selected '?> >G</option> <option =8 <?php if ($location_name ="H") echo ' selected '?> >H</option> <option =9 <?php if ($location_name ="I") echo ' selected '?> >I</option> <option =10 <?php if ($location_name ="J") echo ' selected '?> >J</option> <option =11 <?php if ($location_name ="K") echo ' selected '?> >K</option> <option =12 <?php if ($location_name ="L") echo ' selected '?> >L</option> <option =13 <?php if ($location_name ="M") echo ' selected '?> >M</option> <option =14 <?php if ($location_name ="N") echo ' selected '?> >N</option> <option =15 <?php if ($location_name ="O") echo ' selected '?> >O</option> <option =16 <?php if ($location_name ="P") echo ' selected '?> >P</option> <option =17 <?php if ($location_name ="Q") echo ' selected '?> >Q</option> <option =18 <?php if ($location_name ="R") echo ' selected '?> >R</option> <option =19 <?php if ($location_name ="S") echo ' selected '?> >S</option> <option =20 <?php if ($location_name ="T") echo ' selected '?> >T</option> <option =21 <?php if ($location_name ="U") echo ' selected '?> >U</option> <option =22 <?php if ($location_name ="V") echo ' selected '?> >V</option> <option =23 <?php if ($location_name ="W") echo ' selected '?> >W</option> <option =24 <?php if ($location_name ="X") echo ' selected '?> >X</option> <option =25 <?php if ($location_name ="Y") echo ' selected '?> >Y</option> <option =26 <?php if ($location_name ="Z") echo ' selected '?> >Z</option> </select></p> <p class ='MainText'> Change Validity- <select name="valid"> <option =1 <?php if ($valid =0) echo ' selected '?> > 0</option> <option =2 <?php if ($valid =1) echo ' selected '?> > 1</option> </select></p> <input type="submit" value="save"/> </p></form> <?php }require('./footer.php');?>everything is picked up apart from the part_number value..cheers,pete. Quote Link to comment https://forums.phpfreaks.com/topic/33012-using-a-get-variable-not-working/ Share on other sites More sharing options...
fert Posted January 5, 2007 Share Posted January 5, 2007 try changing[code]$ok= mysql_query($sql);[/code]to[code]$ok= mysql_query($sql) or die(mysql_error());[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33012-using-a-get-variable-not-working/#findComment-153713 Share on other sites More sharing options...
Carterhost Posted January 5, 2007 Share Posted January 5, 2007 There's nowhere I can see that you passed the part_number variable across. Does it come from apage previous to this? if it does, you should put the [code] $part_number = $_GET['part_number'];[/code] before [code]$action = 'show_form';[/code]and include [code]<input type="hidden" name="part_number" value="<?=$part_number?>" />[/code] in your formand [code]$part_number=$_POST['part_number'][/code] after [code]if($action== 'process_data') {[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33012-using-a-get-variable-not-working/#findComment-153718 Share on other sites More sharing options...
pjc2003 Posted January 5, 2007 Author Share Posted January 5, 2007 hi there,thanks for your replys..I have an error now that says: 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 '( location , valid) VALUES ('E', '1' ) WHERE part_number=00000my code now looks like this:<?phprequire('./header.php');require('./dbconn.php');$part_number = $_GET['part_number'];$action = 'show_form';if(!empty($_POST)) { $action= 'process_data'; }if($action== 'process_data') {$part_number=$_POST['part_number'];$location_name = trim($_POST['location']);$valid = intval(trim($_POST['valid']));$sql= "UPDATE stockdata ( location , valid) " ."VALUES ('$location_name', '$valid' )WHERE part_number=$part_number ";$ok= mysql_query($sql) or die(mysql_error()); if($ok){ echo "<p class='ErrorText'>Update OK!</p>"; } else { echo "<p class ='ErrorText'>Update Failed!</p>"; } } else { $action = 'show_form'; } echo $sql; //end running database queryif($action == 'show_form') {?><form method="post" action="stock_control.php"><input type="hidden" name="part_number" value="<?=$part_number?>" /> <p class ='MainText'> Change Location - <select name="location"> <option =1 <?php if ($location_name ="A") echo ' selected '?> >A</option> <option =2 <?php if ($location_name ="B") echo ' selected '?> >B</option> <option =3 <?php if ($location_name ="C") echo ' selected '?> >C</option> <option =4 <?php if ($location_name ="D") echo ' selected '?> >D</option> <option =5 <?php if ($location_name ="E") echo ' selected '?> >E</option> <option =6 <?php if ($location_name ="F") echo ' selected '?> >F</option> <option =7 <?php if ($location_name ="G") echo ' selected '?> >G</option> <option =8 <?php if ($location_name ="H") echo ' selected '?> >H</option> <option =9 <?php if ($location_name ="I") echo ' selected '?> >I</option> <option =10 <?php if ($location_name ="J") echo ' selected '?> >J</option> <option =11 <?php if ($location_name ="K") echo ' selected '?> >K</option> <option =12 <?php if ($location_name ="L") echo ' selected '?> >L</option> <option =13 <?php if ($location_name ="M") echo ' selected '?> >M</option> <option =14 <?php if ($location_name ="N") echo ' selected '?> >N</option> <option =15 <?php if ($location_name ="O") echo ' selected '?> >O</option> <option =16 <?php if ($location_name ="P") echo ' selected '?> >P</option> <option =17 <?php if ($location_name ="Q") echo ' selected '?> >Q</option> <option =18 <?php if ($location_name ="R") echo ' selected '?> >R</option> <option =19 <?php if ($location_name ="S") echo ' selected '?> >S</option> <option =20 <?php if ($location_name ="T") echo ' selected '?> >T</option> <option =21 <?php if ($location_name ="U") echo ' selected '?> >U</option> <option =22 <?php if ($location_name ="V") echo ' selected '?> >V</option> <option =23 <?php if ($location_name ="W") echo ' selected '?> >W</option> <option =24 <?php if ($location_name ="X") echo ' selected '?> >X</option> <option =25 <?php if ($location_name ="Y") echo ' selected '?> >Y</option> <option =26 <?php if ($location_name ="Z") echo ' selected '?> >Z</option> </select></p> <p class ='MainText'> Change Validity- <select name="valid"> <option =1 <?php if ($valid =0) echo ' selected '?> > 0</option> <option =2 <?php if ($valid =1) echo ' selected '?> > 1</option> </select></p> <input type="submit" value="save"/> </p></form> <?php }require('./footer.php');?>many thanks,pete. Quote Link to comment https://forums.phpfreaks.com/topic/33012-using-a-get-variable-not-working/#findComment-153721 Share on other sites More sharing options...
ikmyer Posted January 5, 2007 Share Posted January 5, 2007 do you see http://yoursitename/codefile.php?part_number=x in the address bar ? Quote Link to comment https://forums.phpfreaks.com/topic/33012-using-a-get-variable-not-working/#findComment-153723 Share on other sites More sharing options...
ikmyer Posted January 5, 2007 Share Posted January 5, 2007 the $_GET['part_number'] gets the value from the referer link ( link you click on to get to page or link a form was sent to )the $_POST['part_number'] gets the value from the actual form input. Quote Link to comment https://forums.phpfreaks.com/topic/33012-using-a-get-variable-not-working/#findComment-153726 Share on other sites More sharing options...
fert Posted January 5, 2007 Share Posted January 5, 2007 change[code]$sql= "UPDATE stockdata ( location , valid) " ."VALUES ('$location_name', '$valid' )WHERE part_number=$part_number ";[/code]to[code]$sql= "UPDATE stockdata ( location , valid) " ."VALUES ('$location_name', '$valid' )WHERE part_number='$part_number' ";[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33012-using-a-get-variable-not-working/#findComment-153732 Share on other sites More sharing options...
pjc2003 Posted January 5, 2007 Author Share Posted January 5, 2007 still getting the error :(the error that comes up is: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 '( location , valid) VALUES ('H', '0' ) WHERE part_number='0000the way I have brought the variable in from other page is:<a class='LinkText' href='stock_control.php?part_number=<?php echo $part_number?>'>Edit Item</a>that works ok because i can see in the address bar that the part number is added on fine...check it out on my site..www.longnines.net/mobile/most strange..... Quote Link to comment https://forums.phpfreaks.com/topic/33012-using-a-get-variable-not-working/#findComment-153746 Share on other sites More sharing options...
Carterhost Posted January 5, 2007 Share Posted January 5, 2007 Do you have a part_number field in your DB that has the value 0000 ?If not, maybe you should enter one manually? Quote Link to comment https://forums.phpfreaks.com/topic/33012-using-a-get-variable-not-working/#findComment-153753 Share on other sites More sharing options...
kenrbnsn Posted January 5, 2007 Share Posted January 5, 2007 The works "location" and/or "valid" are probably reserved words in MySQL so you need to surround them with backticks:[code]<?php$sql= "UPDATE stockdata ( `location` , `valid`) VALUES ('$location_name', '$valid' ) WHERE part_number=$part_number ";?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/33012-using-a-get-variable-not-working/#findComment-153761 Share on other sites More sharing options...
pjc2003 Posted January 5, 2007 Author Share Posted January 5, 2007 ive tried what you suggested ken but still getting the same error accept that this time is doesnt give any value for part_number: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 '( `location` , `valid`) VALUES ('Z', '1' ) WHERE part_number=' Quote Link to comment https://forums.phpfreaks.com/topic/33012-using-a-get-variable-not-working/#findComment-153770 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.