bpgilly Posted May 19, 2012 Share Posted May 19, 2012 Hi i'm making a new website based on PHP. I bumped on a problem now... The problem is that I have a loop where i get all records out of my sql database and get them into a form. Every record has a differend form with a button to. Every button then has a unique ID. Now i tried everyting i could think of to get the value out of a button when a button is clicked. But nothing worked... here is the part of the code where everything is for the form and the POST. while($record = mysql_fetch_object($result)){ $id = $record->id; echo"<form action='home.php' method='post'>"; echo"<b>name:</b>".$record->name." <b>Price: </b>".$record->price.""; echo'<input type="hidden" name="id" value='.$id.'>'; echo"<input type='submit' value='Buy' /><br />"; echo"</form>"; } if($_POST) { $ID = intval($_POST['id']); $query1 = "SELECT price FROM items WHERE id = ".$ID.""; $result1 = mysql_query($query1); list($cost) = mysql_fetch_row($result1); $gold = getStat('gc',$userID); if($gold > $cost) { setStat('gc',$userID,($gold - $cost)); $queryw = "INSERT INTO users_inventory(item_id,user_id, quantity) VALUES ('".$ID."','".$userID."','1')"; $resultw = mysql_query($queryw); } else { echo"You can't afford this weapons."; } } The only thing that doesn't seem to work is this part $ID = intval($_POST['id']); is always gives me this error: Notice: Undefined index: id in shop.php on line 26 Please help me out here i have no idea what to do from here on... Quote Link to comment https://forums.phpfreaks.com/topic/262771-buttons-with-unique-id/ Share on other sites More sharing options...
noXstyle Posted May 19, 2012 Share Posted May 19, 2012 Hmm... About the HTML markup: echo'<input type="hidden" name="id" value='.$id.'>'; Should be echo'<input type="hidden" name="id" value="'.$id.'">'; Also the last part is unnecessary on line: echo"<b>name:</b>".$record->name." <b>Price: </b>".$record->price.""; Should be: echo"<b>name:</b>".$record->name." <b>Price: </b>".$record->price; Make sure the form targets are correct. You show a form with action to home.php and give us error from shop.php. Which form targets to shop.php? Undefined index notice will get risen when you are trying to access array key that is not specified. In this instance you are posting data and trying to access id key, which was not present on the form you submitted. Just make sure you are submitting the right form to the right place and you should be golden. Quote Link to comment https://forums.phpfreaks.com/topic/262771-buttons-with-unique-id/#findComment-1346797 Share on other sites More sharing options...
cpd Posted May 19, 2012 Share Posted May 19, 2012 Firstly if you want to access a specific records ID you need to do something along the lines of (assumes 'id' is a field your retrieving from the database) echo'<input type="hidden" name="id" value='.$record->id.'>' This is because your while loop is assigning the next row in your result set to the $record variable and parsing it as an object. To access the ID of the record you need to use the same principle used to access the price and name. As for your error its not entirely clear as to why its occurring as you have a hidden input tag with the name 'id'. Start debugging by typing: var_dump($_POST); Inside your if($_POST) at the top. This will show all the POST variables sent to the header via the POST method once the form is submitted. Quote Link to comment https://forums.phpfreaks.com/topic/262771-buttons-with-unique-id/#findComment-1346799 Share on other sites More sharing options...
bpgilly Posted May 19, 2012 Author Share Posted May 19, 2012 I fixed the problem. I had to make an action to a different page. I was trying to get my whole website into home.php and just work with include and require_once to get everyting one home.php. I just made a new page called store.php with my layout copied and copied my code there, then i did everyting you told me and it works fine now. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/262771-buttons-with-unique-id/#findComment-1346812 Share on other sites More sharing options...
cpd Posted May 19, 2012 Share Posted May 19, 2012 Looking at your other posts you may want to reconsider your software architure. Copying and pasting entire documents like you seem to be doing suggests your not programming in an OOP fashion as described in other topics of yours. You should do some.research on.software architectures to gain an understanding of how to build your applications. Quote Link to comment https://forums.phpfreaks.com/topic/262771-buttons-with-unique-id/#findComment-1346830 Share on other sites More sharing options...
bpgilly Posted May 20, 2012 Author Share Posted May 20, 2012 In my other topic... Seems i only have 2 posts on this forum... And i'm not really copy pasting i'm looking around reading code and trying to understand it, then i use it in my website and try to get it right, where i bumped into this problem and another one wich i'm trying to find myself to... Quote Link to comment https://forums.phpfreaks.com/topic/262771-buttons-with-unique-id/#findComment-1347016 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.