Page 1 of 1

Adding additional data to a mysql field

Posted: Fri Oct 19, 2012 9:11 am
by Twilight
Hi All

I am sure this has been covered but not able to find anything. I have almost completed my project for work and I have one last section to do and that is to update a record.

What I want to do is add new data to the existing data field. I have created a text box but I am not sure how to add addition text to the field.

Any ideas or youtube videos would be great

Regards
Andy

Re: Adding additional data to a mysql field

Posted: Fri Oct 19, 2012 4:35 pm
by Jackolantern
You would want to use an UPDATE query to add or change info to an existing record :)

Re: Adding additional data to a mysql field

Posted: Mon Oct 22, 2012 12:21 pm
by Twilight
Jackolantern wrote:You would want to use an UPDATE query to add or change info to an existing record :)
Hi Jackolantern

I have read up on the coding for the update function but none of the articles tell me if it over writes over the current data with the new data. What I currently have is a text field full of data and I want to be able to add to this data when any work has been carried out.

Are you able to share any code that may help me with this.

Regards
Andy

Re: Adding additional data to a mysql field

Posted: Mon Oct 22, 2012 5:04 pm
by Torniquet

Code: Select all

$query = "UPDATE `table` SET `field` = CONCAT(`field`, '{$new_data}') WHERE `conditions`='met'";
 
Should do what you are asking for :)

Re: Adding additional data to a mysql field

Posted: Mon Oct 22, 2012 10:19 pm
by Callan S.
Seems complex - I thought you'd just load the data into a$, then make a$=a$.$b; where $b is the new data you want to add on. Then write $a back to the database.

Re: Adding additional data to a mysql field

Posted: Mon Oct 22, 2012 11:24 pm
by Jackolantern
If you look at the syntax of the UPDATE function, you will see that you specify which fields you want to alter. In this way, you can update all fields for a record, only a couple, most of them, or however you want to do it.

Torn's code is better since it does it in one database query.

However, you may want to look at the design of your database if you are concatenating values into a single field, because that may be violating the best practice of atomicity. Basically, that means one data piece should be stored in each field of a record and only one. The reason this is important is because, for example, if you have to go back later and write up reporting scripts for your database, those fields are going to be clusters of data you can't easily work with. It makes the design very brittle and inflexible.

Of course, there are good reasons for concatenating data in a field, such as some kind of programmatic logic (I can't think of any good examples off-hand). But it usually isn't a good idea to pile discreet pieces of data into a single field.

Re: Adding additional data to a mysql field

Posted: Tue Oct 23, 2012 2:02 am
by hallsofvallhalla

Code: Select all

$newsp = $playerinfo3['spellpoints'] - 10;
$query = "UPDATE players SET spellpoints = '$newsp' WHERE playername = '$playername';

Re: Adding additional data to a mysql field

Posted: Tue Oct 23, 2012 5:04 am
by Jackolantern
Yeah, that is how you would want to do it. If you are continuously adding on to the end of a field, you may want to review your data storage design :)

Re: Adding additional data to a mysql field

Posted: Tue Oct 23, 2012 8:42 am
by Twilight
Thanks guys

I will try this today.

The reason I want to add additional data to a filed that has data is because it is a call logging system. The engineer will go in to the ticket and add updates to that one ticket.

This is how the big boys look to do it but not sure if this is the correct way or not.

As you all know I have just started to use PHP and have come so far with all you guys helping me.

Regards
Andy

:D :D :D

Re: Adding additional data to a mysql field

Posted: Tue Oct 23, 2012 9:56 am
by Twilight
Akmost there Guys

Again thanks for your help.

I hope this will be the last thing I need to do. Please can you let me know how I get get the following in to just one submit button

Code: Select all

<form method = "POST" action="update.php">
<br><br>Update: <br><textarea name="Update" cols="100" rows="10"></textarea><br>

<input type="submit" value="submit"><br><br>

<?php

echo "<tr><td><center><A href='Update.php?id=$Ticket' >Submit</a></center></td><tr>" ;

?>
Regards
Andy