Adding additional data to a mysql field

Place for questions and answers for all newcomers and new coders. This is a free for all forum, no question is too stupid and to noob.
Post Reply
Twilight
Posts: 58
Joined: Sat Mar 17, 2012 11:11 am

Adding additional data to a mysql field

Post 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
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Adding additional data to a mysql field

Post by Jackolantern »

You would want to use an UPDATE query to add or change info to an existing record :)
The indelible lord of tl;dr
Twilight
Posts: 58
Joined: Sat Mar 17, 2012 11:11 am

Re: Adding additional data to a mysql field

Post 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
User avatar
Torniquet
Posts: 869
Joined: Sun Aug 02, 2009 6:18 am

Re: Adding additional data to a mysql field

Post by Torniquet »

Code: Select all

$query = "UPDATE `table` SET `field` = CONCAT(`field`, '{$new_data}') WHERE `conditions`='met'";
 
Should do what you are asking for :)
New Site Coming Soon! Stay tuned :D
User avatar
Callan S.
Posts: 2042
Joined: Sun Jan 24, 2010 5:43 am

Re: Adding additional data to a mysql field

Post 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.
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Adding additional data to a mysql field

Post 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.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12023
Joined: Wed Apr 22, 2009 11:29 pm

Re: Adding additional data to a mysql field

Post by hallsofvallhalla »

Code: Select all

$newsp = $playerinfo3['spellpoints'] - 10;
$query = "UPDATE players SET spellpoints = '$newsp' WHERE playername = '$playername';
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: Adding additional data to a mysql field

Post 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 :)
The indelible lord of tl;dr
Twilight
Posts: 58
Joined: Sat Mar 17, 2012 11:11 am

Re: Adding additional data to a mysql field

Post 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
Twilight
Posts: 58
Joined: Sat Mar 17, 2012 11:11 am

Re: Adding additional data to a mysql field

Post 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
Post Reply

Return to “Beginner Help and Support”