Jump to content

Find name and add all together


andylord

Recommended Posts

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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']

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

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();
}
?>

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

 

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();
}
?>

Link to comment
Share on other sites

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']).",

Link to comment
Share on other sites

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'])."";

Link to comment
Share on other sites

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();
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

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();
}
?>

Link to comment
Share on other sites

 

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'])."";

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'])."'";
?>

Link to comment
Share on other sites

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();
}
?>

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.