replacing and removing data in database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
robynprivette
Forum Commoner
Posts: 46
Joined: Sun May 02, 2010 6:22 pm

replacing and removing data in database

Post by robynprivette »

My site is a fansite for a dragon game. People enter the draogn codes into the database. I want to let them fill out a form to update there codes in the database and remove them as well.

Here is my form for adding the codes

Code: Select all

<center>
Add your dragons 4 digit code to the form<br>
<table>
<tr>
<td colspan=3>
<form method="post" action="add.php" >
Scrollname: <br> 
<input type="text" name="Scrollname" size="40">
</td>
</tr>
<tr>
<td>
Egg Code:<br> 
<input type="text" name="egg" size="4"> 

<td> 
Hatchling Code:<br> 
<input type="text" name="hatchling" size="4"> 

<td>  
ER Code:<br> 
<input type="text" name="er" size="4">
</td>
</tr>
<tr>
<td colspan=3> 
<center><input type="submit" value="Submit"> 
</center> 
</td>
</tr>
</table>
</form>
And here is my current add.php code

Code: Select all

<?php

$Scrollname = $_POST['Scrollname'];
$egg = $_POST['egg'];
$hatchling = $_POST['hatchling'];
$er = $_POST['er'];

mysql_connect("localhost","","") or die ('Error: ' . mysql_error());
mysql_select_db("thehatch_dragons");
$query="INSERT INTO test (id, Scrollname, egg, hatchling, er)VALUES ('NULL', '".$Scrollname."', '".$egg."', '".$hatchling."', '".$er."')";

mysql_query($query) or die ('Error updating Database');
// Print out the contents of the entry 
echo "<center>Dragons successfully added</center>";
?> 
Thanks in advance :D
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: replacing and removing data in database

Post by cpetercarter »

You don't say what your problem is, but I guess that you find that your code does not in fact add entries to the database. if so, the reason could be that you need a space between the closing bracket and VALUES in this line:

Code: Select all

$query="INSERT INTO test (id, Scrollname, egg, hatchling, er)VALUES ('NULL', '".$Scrollname."', '".$egg."', '".$hatchling."', '".$er."')"
Also, you must 'escape' user-generated data before putting it into the database, otherwise you are open to SQL injection attacks. Change the first lines in add.php to:

Code: Select all

$Scrollname = mysql_real_escape_string($_POST['Scrollname']);
$egg = mysql_real_escape_string($_POST['egg']);
$hatchling =mysql_real_escape_string( $_POST['hatchling']);
$er = mysql_real_escape_string($_POST['er']);
Finally, you may want to consider data validation. Are you happy for your users to enter any old rubbish in your form? Or do you want to check that eg a field which expects a numeric value in fact contains one?
User avatar
robynprivette
Forum Commoner
Posts: 46
Joined: Sun May 02, 2010 6:22 pm

Re: replacing and removing data in database

Post by robynprivette »

Sorry I wasn't a little clearer. My problem is that when they want to update what they've already put in, like add to it without removing what's already in there and being able to remove what is in there if they want to. Does that make sense?
cpetercarter
Forum Contributor
Posts: 474
Joined: Sat Jul 25, 2009 2:00 am

Re: replacing and removing data in database

Post by cpetercarter »

No, that is not entirely clear. However :

The sql INSERT command will always add a new record to the database. If you want to remove or amend an existing record, you need to use the DELETE or UPDATE commands. Unless you want to delete all the records from the table, DELETE and UPDATE need a WHERE clause which will tell mysql which record or records to change. So you need a way of distinguishing one record from another. Typically, this is done by giving each record a unique id. I see that there is an id field in your table. I am not sure however why you are placing the (non-)value NULL in it in your INSERT statement. If you have defined it as an auto incrementing field, you don't need explicitly to place a new value in it at all - mysql will do this for you. But maybe the field is intended to hold the id of the player?
User avatar
robynprivette
Forum Commoner
Posts: 46
Joined: Sun May 02, 2010 6:22 pm

Re: replacing and removing data in database

Post by robynprivette »

yes it is suppose to identify the user. so i need a code that will add or delete from their id... but i don't know how to go about it ):

like say a user enters these entries

gf6T, iuG7, 8erT

and they want to remove 8erT without removing the others.
Post Reply