Jump to content

Php Form, Update Database


beennn

Recommended Posts

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Archived

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

×
×
  • Create New...