beennn Posted October 12, 2011 Share Posted October 12, 2011 Previously iv asked how to update values in a database which was very specific to only one value. As there will be many values shown, and these values can be created by the users, how can i accomplish something which would have the same effect as: echo "<a href=\"#\" onclick=\"UPDATE groups SET [\'number_of_members\'] = [\'number_of_members\'] + 1\">Join</a>"; (just to show what I mean) any help is greatly appreciated Link to comment Share on other sites More sharing options...
justsomeguy Posted October 12, 2011 Share Posted October 12, 2011 I'm not sure if you're asking about SQL code, but you may want to use ajax to send a request to update the database if that suits your needs. Link to comment Share on other sites More sharing options...
beennn Posted October 12, 2011 Author Share Posted October 12, 2011 sorry, i just mean how would i go about doing that without having predetermined forms set up. a user can create an unlimited amount of groups; these groups appear in a table with the amount of members next to it and a 'join' link next to them which i want people to be able to click and it increase the number of members. I know how i could increase the number if i knew what the groups would be called, but as they can be called anything, how can i create a form which detects the name of the group? sorry if this still doesnt make sense, finding this hard to explain Link to comment Share on other sites More sharing options...
justsomeguy Posted October 12, 2011 Share Posted October 12, 2011 The group should have a numeric ID in the database, and you pass that ID to PHP to tell it which group they are joining. So, when you write out the links that they click on then you need to include the ID. If it's just a regular click, you can put the ID in the URL. If you want to use ajax then you would pass the ID to the click handler. Link to comment Share on other sites More sharing options...
beennn Posted October 12, 2011 Author Share Posted October 12, 2011 thank you, ill give it a go. do you know any docs/tuts or examples on how to do this? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 12, 2011 Share Posted October 12, 2011 http://www.w3schools.com/php/php_get.asp There's also a section about ajax. Link to comment Share on other sites More sharing options...
beennn Posted October 12, 2011 Author Share Posted October 12, 2011 thank you very much Link to comment Share on other sites More sharing options...
beennn Posted October 12, 2011 Author Share Posted October 12, 2011 Is it possible to use get outside of a form? table displays like this, not a form, surrounded the 'join' with a form and only realised after i did so that it only 'gets' values within the form, how would i get the group value thats outside the form?____________________________|group | number of members | Join|____________________________| AL | 0 | Join|____________________________|GS | 0 | Join|____________________________ and heres how im displaying it: echo "<table width=\"100%\" align=\"center\" border='0'>"; echo "<tr> <th>Group</th> <th>Number of Members</th> <th>Join</th></tr>"; while($groups = mysql_fetch_array( $findGroups )) { echo "<tr><td align=\"center\">"; echo $groups['group_name']; echo "</td><td align=\"center\">"; echo $groups['number_of_members']; echo "</td><td align=\"center\">"; echo "join button here"; echo "</td></tr>"; } echo "</table>"; Link to comment Share on other sites More sharing options...
justsomeguy Posted October 12, 2011 Share Posted October 12, 2011 You can make a link and just put the variables in the URL like you see on the tutorial page, they will be accessible using $_GET. Link to comment Share on other sites More sharing options...
beennn Posted October 12, 2011 Author Share Posted October 12, 2011 ooooh, lol thank you, doing it like this, i can just use the name instead of an ID? iv tried: "<a href = \"thank_you.php?group=&groups['group_name']\">Join</a>" how do i seperate the &groups['group_name'] so it finds this value in the database? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 12, 2011 Share Posted October 12, 2011 You need to get the value from the database first, and include it in the link when you write the URL with PHP. A numeric ID is preferable, you would only want to use something like a name if you can guarantee it is unique in the database and if you escape it with rawurlencode when you put it in the URL. http://www.php.net/manual/en/function.rawurlencode.php Link to comment Share on other sites More sharing options...
beennn Posted October 12, 2011 Author Share Posted October 12, 2011 ok, i understand the principle of this, im just having trouble understanding how to implement it, every way i have tried using rawurlencode doesnt reveal the actual value just the code which is supposed to get the value Link to comment Share on other sites More sharing options...
justsomeguy Posted October 12, 2011 Share Posted October 12, 2011 echo '<a href="thank-you.php?group=' . rawurlencode($group_name) . '">Join</a>'; Link to comment Share on other sites More sharing options...
beennn Posted October 12, 2011 Author Share Posted October 12, 2011 thank you, Iv finaly got everything set up but it doesnt update the database, when using names (as these names will be unique) whats different when you said this u escape it with rawurlencode the groups name is: PFU's which appears in the url as: thank_you.php?group=PFU%27s ______________________and im receiving it like this:$group_name = $_GET['group_name'];$result = mysql_query("SELECT * FROM groups WHERE group_name='$group_name'");if(!$result) { echo mysql_error();}while($row = mysql_fetch_array($result)) { mysql_query("UPDATE groups SET number_of_members = number_of_members + 1"); } Link to comment Share on other sites More sharing options...
Ingolme Posted October 12, 2011 Share Posted October 12, 2011 thank you, Iv finaly got everything set up but it doesnt update the database, when using names (as these names will be unique) whats different when you said this the groups name is: PFU's which appears in the url as: thank_you.php?group=PFU%27s ______________________and im receiving it like this:$group_name = $_GET['group_name'];$result = mysql_query("SELECT * FROM groups WHERE group_name='$group_name'");if(!$result) { echo mysql_error();}while($row = mysql_fetch_array($result)) { mysql_query("UPDATE groups SET number_of_members = number_of_members + 1"); } The URL is supposed to look like that. The apostrophe is not a valid character in a URL, so it is encoded with its hexadecimal value. When it arrives on the server it will be decoded. By the way, you have to escape characters before putting them in the query to prevent people from hijacking your database:$group_name = mysql_real_escape_string($_GET['group']); Link to comment Share on other sites More sharing options...
beennn Posted October 12, 2011 Author Share Posted October 12, 2011 oooh ok thank you, makes sense >.< when the link it pressed, the value in the database does not change, can i use number_of_members = number_of_members + 1 when the default value of number_of_members is NULL? or do i have to set the default to 1? would this be the only issue stopping it from working EDIT: if so, how can i alter the default value of a collum?tried: ALTER TABLE groupsALTER COLUMN number_of_members int DEFAULT '1' Link to comment Share on other sites More sharing options...
beennn Posted October 13, 2011 Author Share Posted October 13, 2011 Ok, changed the default value and still its not working, pretty sure im doing this wrong, could you tell me where the mistake it please? <?$con = mysql_connect("localhost","username","password");if(!$con) { echo mysql_error();} mysql_select_db("db_name", $con); $group_name = mysql_real_escape_string($_GET['group_name']); $result = mysql_query("SELECT * FROM groups WHERE group_name='$group_name'");if(!$result) { echo mysql_error();} while($row = mysql_fetch_array($result)) { mysql_query("UPDATE groups SET number_of_members = number_of_members + 1"); } ?> Link to comment Share on other sites More sharing options...
Ingolme Posted October 13, 2011 Share Posted October 13, 2011 I don't know if you copied the query string presented earlier, but if you did then you'll have this problem: $group_name = mysql_real_escape_string($_GET['group_name']);thank_you.php?group=PFU%27s They should both be the same. Link to comment Share on other sites More sharing options...
beennn Posted October 13, 2011 Author Share Posted October 13, 2011 Haha, yep, didnt notice until it was to late, but even after i had changed it, still doesnt update the database Link to comment Share on other sites More sharing options...
Ingolme Posted October 13, 2011 Share Posted October 13, 2011 Are you checking for errors? If the return value of mysql_query() is false, print out mysql_error(). Also, make sure that rows are being affected. Print out mysql_affected_rows() to see if anything was modified during the query. Link to comment Share on other sites More sharing options...
beennn Posted October 13, 2011 Author Share Posted October 13, 2011 while($row = mysql_fetch_array($result)) { $check = mysql_query("UPDATE groups SET number_of_members = number_of_members + 1", mysql_affected_rows());if(!$check){echo mysql_error();} } I must be doing something wrong? >.< Link to comment Share on other sites More sharing options...
thescientist Posted October 13, 2011 Share Posted October 13, 2011 well, what's happening? are you getting any errors? have you tried echoing mysql_affected rows? the problem could be query. The comma and then mysql_affected_rows()? Link to comment Share on other sites More sharing options...
beennn Posted October 13, 2011 Author Share Posted October 13, 2011 tried echo'ing and still no errors, not returning anything Link to comment Share on other sites More sharing options...
thescientist Posted October 13, 2011 Share Posted October 13, 2011 what about the query. i mentioned the syntax looked off. what are you trying to accomplish with mysql_affected_rows() there? If you're trying add it into the query, then you would need to use ., not a comma. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 13, 2011 Share Posted October 13, 2011 You use mysql_affected_rows after running the query. Look at the manual page: http://www.php.net/manual/en/function.mysql-affected-rows.php Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.