andylord Posted March 10, 2009 Share Posted March 10, 2009 http://andys-cop-help.com/Points.php That is the webpage, i have a form that inputs data but what i want to do when i input something with the same name i want the numbers to add up, so I have andy, 1,1,1,1,4 and then add andy 1,1,1,1,4 i want to get andy 2,2,2,2,8 if you understand me then i want it so the highest total on the page to come first if you could me with this much appreciated im new and learning still so if you could explain it also thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/ Share on other sites More sharing options...
lonewolf217 Posted March 10, 2009 Share Posted March 10, 2009 might help to post the relevant code of your form if I understand this correctly, you basically grab data from a database and display it in this table. Some points 1) to sort by the highest value first, put the sorting characteristic in your SQL string so the data it returns is sorted, then just display it like normal 2) on the form you use to update your data, get the current value from the database and add it to what the new value is, then resubmit the value Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781240 Share on other sites More sharing options...
andylord Posted March 10, 2009 Author Share Posted March 10, 2009 Ok i understand what you are syaing there but dont know how to do it code : <?php // Get all the data from the "example" table $result = mysql_query("SELECT * FROM Points") or die(mysql_error()); echo "<table border='2'bgcolor=#000000 bordercolor=#000099>"; echo "<tr> <th>Name</th> <th>Gold</th> <th>PvP</th> <th>Storage</th> <th>Other</th> <th>Total</th>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['Name']; echo "</td><td>"; echo $row['Gold']; echo "</td><td>"; echo $row['PvP']; echo "</td><td>"; echo $row['Storage']; echo "</td><td>"; echo $row['Other']; echo "</td><td>"; echo ($row['Gold']+$row['PvP']+$row['Storage']+$row['Other']); echo "</td><tr>"; } echo "</table>"; ?> <?php if (isset($_POST['Submit'])) {$sql = "INSERT INTO $db_table(Name,Gold,PvP,Storage,Other) values ('".mysql_real_escape_string(stripslashes($_REQUEST['Name']))."','".mysql_real_escape_string(stripslashes($_REQUEST['Gold']))."','".mysql_real_escape_string(stripslashes($_REQUEST['PvP']))."','".mysql_real_escape_string(stripslashes($_REQUEST['Storage']))."','".mysql_real_escape_string(stripslashes($_REQUEST['Other']))."')";if($result = mysql_query($sql ,$db)) { echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); }} ?> <h1> </h1> <hr> <form method="post" action=""> <p>Legend: </p> <p>50G = 1Point<br /> PvP = Winner of competitions only at the moment<br /> Storage = Giving away valuable items, Helping others etc decided at the time by an officer. 5,10,15,20-100 in this region.<br /> Other = Decided by officer same as storage. </p> <p> </p> <p>Name:<br> <textarea name="Name"></textarea> <br /> <br> Gold: <br> <input type="text" name="Gold"> <br> <br> PvP: <br> <input type="text" name="PvP"> <br> <br> Storage: <br> <input type="text" name="Storage"> <br> <br> Other: <br> <textarea name="Other"></textarea> </p> <p> <input type="submit" name="Submit" value="Submit"> </p> <p> </p> <p> </p> <p> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781247 Share on other sites More sharing options...
andylord Posted March 10, 2009 Author Share Posted March 10, 2009 can someone help me please ? Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781427 Share on other sites More sharing options...
lonewolf217 Posted March 10, 2009 Share Posted March 10, 2009 1) for your name field, you should probably use <input type="text">, textarea is harder to handle and probably much more than you need for that 2) you have zero error checking going on to make sure people are actually inputting numbers into these fields 3) your only SQL statement is INSERT .. what if the row exists already, you need to UPDATE the row rather than insert a new one this is probably not the most elegant way to do this, but it will work. if $_POST['Submit'] is selected, then first run a query to select the current values for the name being edited $gold = $row['Gold'] + $_REQUEST['gold']; etc.. then you can run your SQL statement using the $gold value instead of $_REQUEST['gold'] Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781439 Share on other sites More sharing options...
andylord Posted March 10, 2009 Author Share Posted March 10, 2009 ah i understand thankyou very much Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781444 Share on other sites More sharing options...
redarrow Posted March 10, 2009 Share Posted March 10, 2009 look at this please add one to the field names. <?php $sql = "UPDATE $db_table SET Name=Name+1,Gold=Gold+ 1,PvP=PvP+1,Storage=Storage+1,Other=Other+1 where user_id='what_ever'"; $res=mysql_query($sql)or die("update error".mysql_error()); ?> Did i write this correctly any one not sure now? Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781448 Share on other sites More sharing options...
lonewolf217 Posted March 10, 2009 Share Posted March 10, 2009 well its not always guaranteed to be value + 1, at least from my understanding. If it was, then there is no point to having the whole form to increment a static value each time. secondly, i dont think he will be incrementing the Name value Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781458 Share on other sites More sharing options...
andylord Posted March 10, 2009 Author Share Posted March 10, 2009 <?php if (isset($_POST['Submit'])) {$sql = "UPDATE INTO $db_table(Name,Gold,PvP,Storage,Other) values $gold = $row['Gold'] + $_REQUEST['gold'];$Pvp = $row['PvP'] + $_REQUEST['Pvp'];$Storage = $row['Storage'] + $_REQUEST['Storage'];$Other = $row['Other'] + $_REQUEST['Other'];";if($result = mysql_query($sql ,$db)) { echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); }} ?> ok so i tryed this but it doesnt work as you can see with all the trys i did on the form itself it just gives more andys. Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781472 Share on other sites More sharing options...
redarrow Posted March 10, 2009 Share Posted March 10, 2009 fast way then i hope looks good. what ever the name of the user's condition is. user_id=".mysql_real_esacpe_string($_POST['what_ever'] <?php if (isset($_POST['Submit'])) { $sql = "UPDATE $db_table SET Name=Name+".mysql_real_esacpe_string($_POST['Name']).", Gold=Gold+".mysql_real_esacpe_string($_POST['Gold']).", PvP=PvP+".mysql_real_esacpe_string($_POST['PvP']).", Storge=Storage+".mysql_real_esacpe_string($_POST['Storage'])." ,Other=Other+".mysql_real_esacpe_string($_POST['Other'])." WHERE user_id=".mysql_real_esacpe_string($_POST['what_ever']).""; $res=mysql_query($sql)or die("update error".mysql_error()); echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781474 Share on other sites More sharing options...
andylord Posted March 10, 2009 Author Share Posted March 10, 2009 fast way then i hope looks good. what ever the name of the user's condition is. user_id=".mysql_real_esacpe_string($_POST['what_ever'] <?php if (isset($_POST['Submit'])) { $sql = "UPDATE $db_table SET Name=Name+".mysql_real_esacpe_string($_POST['Name']).", Gold=Gold+".mysql_real_esacpe_string($_POSt['Gold']).", PvP=PvP+".mysql_real_esacpe_string($_POST['PvP']).", Storge=Storage+".mysql_real_esacpe_string($_POST['Storage'])." ,Other=Other+".mysql_real_esacpe_string($_POST['Other'])." WHERE user_id=".mysql_real_esacpe_string($_POST['what_ever']).""; $res=mysql_query($sql)or die("update error".mysql_error()); echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); } ?> echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); } ?> all that gives me is this update errorQuery was empty using this <?php if (isset($_POST['Submit'])) {$sql = "UPDATE $db_table SET Name=Name+".mysql_real_esacpe_string($_POST['Name']).", Gold=Gold+".mysql_real_esacpe_string($_POST['Gold']).", PvP=PvP+".mysql_real_esacpe_string($_POST['PvP']).", Storge=Storage+".mysql_real_esacpe_string($_POST['Storage'])." ,Other=Other+".mysql_real_esacpe_string($_POST['Other'])." WHERE user_id=".mysql_real_esacpe_string($_POST['what_ever']).""; } else { $res=mysql_query($sql)or die("update error".mysql_error()); } ?> <h1> </h1> <hr> <form method="post" action=""> <p>Legend: </p> <p>50G = 1Point<br /> PvP = Winner of competitions only at the moment<br /> Storage = Giving away valuable items, Helping others etc decided at the time by an officer. 5,10,15,20-100 in this region.<br /> Other = Decided by officer same as storage. </p> <p> </p> <p>Name:<br> <textarea name="Name"></textarea> <br /> <br> Gold: <br> <input type="text" name="Gold"> <br> <br> PvP: <br> <input type="text" name="PvP"> <br> <br> Storage: <br> <input type="text" name="Storage"> <br> <br> Other: <br> <textarea name="Other"></textarea> </p> <p> <input type="submit" name="Submit" value="Submit"> </p> <p> </p> <p> </p> <p> </p> </form> Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781479 Share on other sites More sharing options...
redarrow Posted March 10, 2009 Share Posted March 10, 2009 will update all the database fields according to the name off the user from the form. and add what ever that user selects as a point. <?php if (isset($_POST['Submit'])) { $sql = "UPDATE $db_table SET Gold=Gold+".mysql_real_esacpe_string($_POST['Gold']).", PvP=PvP+".mysql_real_esacpe_string($_POST['PvP']).", Storge=Storage+".mysql_real_esacpe_string($_POST['Storage'])." ,Other=Other+".mysql_real_esacpe_string($_POST['Other'])." WHERE Name=".mysql_real_esacpe_string($_POST['Name']).""; $res=mysql_query($sql)or die("update error".mysql_error()); echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781487 Share on other sites More sharing options...
andylord Posted March 10, 2009 Author Share Posted March 10, 2009 Fatal error: Call to undefined function mysql_real_esacpe_string() in /customers/andys-cop-help.com/andys-cop-help.com/httpd.www/Pointssubmit654677.php on line 66 cant understand that one line 66 being Gold=Gold+".mysql_real_esacpe_string($_POST['Gold']).", Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781496 Share on other sites More sharing options...
lonewolf217 Posted March 10, 2009 Share Posted March 10, 2009 escape is misspelled Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781501 Share on other sites More sharing options...
andylord Posted March 10, 2009 Author Share Posted March 10, 2009 ah something so simple lol But now i get this update errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Name=andy2' at line 6 That being this line Other=Other+".mysql_real_escape_string($_POST['Other'])." WHERE Name=".mysql_real_escape_string($_POST['Name']).""; Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781502 Share on other sites More sharing options...
redarrow Posted March 10, 2009 Share Posted March 10, 2009 no data base protection. cheek the spelling come on try help me... spell checked <?php if (isset($_POST['Submit'])) { $sql = "UPDATE $db_table SET Name=Name+{$_POST['Name']}, Gold=Gold+{$_POST['Gold']}, PvP=PvP+{$_POST['PvP']}, Storage=Storage+{$_POST['Storage']} ,Other=Other+{$_POST['Other']} WHERE user_id={$_POST['what_ever']}"; $res=mysql_query($sql)or die("update error".mysql_error()); echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); } ?> second version. with database protection. spell checked lol. <?php if (isset($_POST['Submit'])) { $sql = "UPDATE $db_table SET Gold=Gold+".mysql_real_escape_string($_POST['Gold']).", PvP=PvP+".mysql_real_escape_string($_POST['PvP']).", Storage=Storage+".mysql_real_escape_string($_POST['Storage'])." ,Other=Other+".mysql_real_escape_string($_POST['Other'])." WHERE Name=".mysql_real_escape_string($_POST['Name']).""; $res=mysql_query($sql)or die("update error".mysql_error()); echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781506 Share on other sites More sharing options...
andylord Posted March 10, 2009 Author Share Posted March 10, 2009 no data base protection. cheek the spelling come on try help me... <?php if (isset($_POST['Submit'])) { $sql = "UPDATE $db_table SET Name=Name+{$_POST['Name']}, Gold=Gold+{$_POST['Gold']}, PvP=PvP+{$_POST['PvP']}, Storge=Storage+{$_POST['Storage']} ,Other=Other+{$_POST['Other']} WHERE user_id={$_POST['what_ever']}"; $res=mysql_query($sql)or die("update error".mysql_error()); echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); } ?> second version. with database protection. spell checked lol. <?php if (isset($_POST['Submit'])) { $sql = "UPDATE $db_table SET Gold=Gold+".mysql_real_escape_string($_POST['Gold']).", PvP=PvP+".mysql_real_escape_string($_POST['PvP']).", Storage=Storage+".mysql_real_escape_string($_POST['Storage'])." ,Other=Other+".mysql_real_escape_string($_POST['Other'])." WHERE Name=".mysql_real_escape_string($_POST['Name']).""; $res=mysql_query($sql)or die("update error".mysql_error()); echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); } ?> 2nd version: update errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Other=Other+ WHERE Name=andy2' at line 5 1st version: the same Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781512 Share on other sites More sharing options...
redarrow Posted March 10, 2009 Share Posted March 10, 2009 try that then <?php if (isset($_POST['Submit'])) { $sql = "UPDATE $db_table SET Gold=Gold+".mysql_real_escape_string($_POST['Gold']).", PvP=PvP+".mysql_real_escape_string($_POST['PvP']).", Storage=Storage+".mysql_real_escape_string($_POST['Storage']).", Other=Other+".mysql_real_escape_string($_POST['Other']).", WHERE Name=".mysql_real_escape_string($_POST['Name']).""; $res=mysql_query($sql)or die("update error".mysql_error()); echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781516 Share on other sites More sharing options...
andylord Posted March 10, 2009 Author Share Posted March 10, 2009 try that then <?php if (isset($_POST['Submit'])) { $sql = "UPDATE $db_table SET Gold=Gold+".mysql_real_escape_string($_POST['Gold']).", PvP=PvP+".mysql_real_escape_string($_POST['PvP']).", Storage=Storage+".mysql_real_escape_string($_POST['Storage']).", Other=Other+".mysql_real_escape_string($_POST['Other']).", WHERE Name=".mysql_real_escape_string($_POST['Name']).""; $res=mysql_query($sql)or die("update error".mysql_error()); echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); } ?> update errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' Other=Other+, WHERE Name=Testname' at line 4 That being this same again: Other=Other+".mysql_real_escape_string($_POST['Other']).", WHERE Name=".mysql_real_escape_string($_POST['Name']).""; Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781519 Share on other sites More sharing options...
lonewolf217 Posted March 10, 2009 Share Posted March 10, 2009 sounds like you have no value in $_POST['Other'] which is causing that error 2nd version: update errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Other=Other+ WHERE Name=andy2' at line 5 try echo'ing the value to make sure something is in it before you try to insert it into the database Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781520 Share on other sites More sharing options...
andylord Posted March 10, 2009 Author Share Posted March 10, 2009 ok so i added a 0 in the post, then i get this update errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Name=testname' at line 6 Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781523 Share on other sites More sharing options...
lonewolf217 Posted March 10, 2009 Share Posted March 10, 2009 Name=testname would be comparing numerical values. to compare textual values use something like this (added single quotes around the name entry from PHP ) <?php $sql = "UPDATE $db_table SET Gold=Gold+".mysql_real_escape_string($_POST['Gold']).", PvP=PvP+".mysql_real_escape_string($_POST['PvP']).", Storage=Storage+".mysql_real_escape_string($_POST['Storage']).", Other=Other+".mysql_real_escape_string($_POST['Other']).", WHERE Name='".mysql_real_escape_string($_POST['Name'])."'"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781525 Share on other sites More sharing options...
redarrow Posted March 10, 2009 Share Posted March 10, 2009 i forgot the single quotes dam lol don't forget to set Other to a 0 or number <?php if (isset($_POST['Submit'])) { $sql = "UPDATE $db_table SET Gold=Gold+".mysql_real_escape_string($_POST['Gold']).", PvP=PvP+".mysql_real_escape_string($_POST['PvP']).", Storage=Storage+".mysql_real_escape_string($_POST['Storage']).", Other=Other+".mysql_real_escape_string($_POST['Other']).", WHERE Name='".mysql_real_escape_string($_POST['Name'])."'"; $res=mysql_query($sql)or die("update error".mysql_error()); echo '<h1>Thank you</h1>Your information has been entered into our database<br><br><img src="www.andys-cop-help.com"'; } else { echo "ERROR: ".mysql_error(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781527 Share on other sites More sharing options...
andylord Posted March 10, 2009 Author Share Posted March 10, 2009 same error appears update errorYou have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE Name='testname'' at line 6 Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781530 Share on other sites More sharing options...
sasa Posted March 10, 2009 Share Posted March 10, 2009 try $sql = "UPDATE $db_table SET Gold=Gold+".($_POST['Gold']+ 0).", PvP=PvP+".($_POST['PvP'] + 0).", Storage=Storage+".($_POST['Storage'] + 0).", Other=Other+".($_POST['Other'] + 0)." WHERE Name='".mysql_real_escape_string($_POST['Name'])."'"; remove , before WHERE Quote Link to comment https://forums.phpfreaks.com/topic/148780-find-name-and-add-all-together/#findComment-781534 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.