I have a simple option tag in html which lists all users held on the database when the user clicks donate i want to take the value entered and update the dontate_Total row relvant to that user with the value entered by the user in the donate label section.
here is my html
<table>
<form>
<tr><td><label>User:</label></td>
<td>
<select>
<?php do{?>
<option> <?php echo $rsNames['first_Name'];?> <?php echo $rsNames['last_Name'];?></option>
<?php } while ( $rsNames= mysql_fetch_assoc($names_query))?>
</select>
</td>
</tr>
<tr><td><label>Donation £</label></td><td><input type="text" maxlength="9" value="0.00"/></td></tr>
<tr><td><input id="submit" type="submit" value="DONATE" /></td></tr>
</form>
</table>
the above works fine but now I want to run and update query when the user hits the DONATE input field. here is my sql statment which I believe wont work just after some advice
$donate_sql =UPDATE `donate` SET donate_Total= donate_Total + $_GET['value'] WHERE first_Name = 'first_Name' AND last_Name ='last_Name';
thanks
How are you?
Edited (more profound) answer:
In your HTML, you have to give names to your fields in order to retrieve them from your PHP script i.e. give your
<select>element a name (attribute), like this:Also, the nested
<option>elements should have a “value” attribute, this is because you almost always want the identification of a field to be a unique ID (so there’s no mistake when refering to a table row) but you want to show useful information to the user, such as a full name.If you plan on using an ID, then your code (in HTML) should be updated to:
This will be outputed as:
Then in your PHP file, it would be much easier to write a query that updates exactly the user that you want.
If you don’t plan on using an ID, you have to face the fact that if 2 users are called John Doe, you’ll be updating the “total” amount of donation for both of them, and it’s going to be a bit more complicated for you to parse the information.
Say you DON’T want to use an ID and you want to stick with your first and last name, then you would write something like:
Which would produce an output similar to:
The third
<option>tag is an example of why you can’t rely on spaces to parse first and last name; a person can have double name or double last name — or both, as it is in my case 😉The separator || is an arbitrary one, the idea is to use one or more characters that are not likely going to be used as part of the name or the last name.
This approach (which again, is a really bad one) would require you to parse the first name and last name individually afterwards, so your script would end up being:
Old answer
The quick answer is:
And then using the
mysql_query($donate_sql);function.However as @hjpotter92 pointed out, you should (amongst other things) add an ID column to that table and set it as a primary key.
Also, you should keep in mind things like escaping values that come in your requests (
$_POSTand$_GET), so no SQL injection is applied to your query.Hope this helps!
Cheers.