So if name doesn't exist create new row with 4 columns and input data, if exists then add to existing data, that is what I am looking for thanks
Databases
Started by uainfantry, May 21 2006 01:20 AM
2 replies to this topic
#1
Posted 21 May 2006 - 01:20 AM
I have my website where I have stats for a game and for now I have everything manual, but I want to make it so that the stats are written into a database and when same player name shows up instead of updating to the new score I want it to add to existing score because more people show up and it becomes a hassle adding everything manually. If it is hard to understand what I am trying to do, here is the site which should give you a clue: http://dfshack.fdns....d=scores5.20.06
So if name doesn't exist create new row with 4 columns and input data, if exists then add to existing data, that is what I am looking for thanks
for the help if any
.
So if name doesn't exist create new row with 4 columns and input data, if exists then add to existing data, that is what I am looking for thanks
#2
Posted 21 May 2006 - 04:47 AM
I'll give you this as a starting block:
Obviously you must change it - add a form - alter fields etc but you get the idea with the update? it checks for a record, then if there is one it updates it else it inserts a new one.
<?php
/* I apologize for any bad coding, this was done at...10:46am
@ Still shattered from last night!
*/
$config = array(
'user' => 'db_username', // Change
'pass' => 'db_password', // Change
'DB' => 'database_name', // Change
'host' => 'localhost'
);
@mysql_connect($config['host'], $config['user'], $config['pass']) or die(mysql_error());
@mysql_select_db($config['DB']) or die(mysql_error());
$Check = @mysql_query("SELECT count(id) FROM `table` WHERE id='$playerid'") or die(mysql_error())
$Check1 = ($Check < 1) ? 'n' : 'y'; // Not needed - but its one of my quirks.
if($Check1 == 'n')
{
// If it doesnt exisit write a new entry.
@mysql_query("INSERT INTO `table` (id, playername, score) VALUES ('', '$playername', '$score')")
or die('Data not inserted: '.mysq_error());
echo "Data inserted!";
}
else
{
// If it exists simply update it.
@mysql_query("UPDATE `table` SET score='$score' WHERE id='$playerid'") or die('Not updated: '.mysql_error());
echo "Data updated!";
}
$Query = mysql_query("SELECT* FROM `table` ORDER by score ASC") or die('Cannot fetch data: '.mysql_error());
$nor = mysql_count($Query);
if($nor < 1)
{
echo "There are no records in the database!";
}
else
{
echo "<table>";
while($row == @mysql_fetch_array($Query) )
{
echo '<tr>
<td>'.$row['id'].'</td>
<td>'.$row['playername'].'</td>
<td>'.$row['score'].'</td>
</tr>';
}
echo "</table>";
}
?>
Obviously you must change it - add a form - alter fields etc but you get the idea with the update? it checks for a record, then if there is one it updates it else it inserts a new one.
Edited by .Matt, 21 May 2006 - 04:49 AM.
#3
Posted 27 May 2006 - 03:21 AM
For the benefit of uainfantry, I've fixed .Matt's code. Not bad for 10:46am. (@Matt: Sorry babes.)
Obviously you must change it - add a form - alter fields etc but you get the idea with the update? it checks for a record, then if there is one it updates it else it inserts a new one.
[/quote]
Obviously you must change it - add a form - alter fields etc but you get the idea with the update? it checks for a record, then if there is one it updates it else it inserts a new one.
[/quote]
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users
