We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

PHP MYSQL site function help!

getinked
getinked asked
on
Medium Priority
261 Views
Last Modified: 2012-05-11
Hi there,

So I am rebuilding an existing site with a basic PHP MYSQL system to enable non experienced site owners to be able to edit the text themselves.

I have the system working beautifully HOWEVER I need the .php to delete or overwrite the existing text before updating.

I am using the following to place the information in a MYSQL table but can't get it to overwrite the existing information:

$sql="INSERT INTO frontpage_whatson (regular_services_text)
VALUES
('$_POST[regular_services_text]')";

Open in new window


Would love some help on this one... its starting to drive me nuts!
Thanks in advance, Luke
Comment
Watch Question

INSERT INTO does exactly what it says, it inserts new data into the table. to update existing data use the UPDATE function instead.

UPDATE table_name SET data1='somedata', data2='moredata' WHERE comparison='true'
so for example, if a user was changing their email address for an account profile, you might use something like this :

$email="email@email.com";
$user="joe_smith";
$sql="UPDATE users SET email='$email' WHERE username='$user'";

Author

Commented:
Will that work in this example though:

Existing text on site: (1)
So I am rebuilding an existing site with a basic PHP MYSQL system to enable non experienced site owners to be able to edit the text themselves.

New information from maintenance form: (2)
I have the system working beautifully HOWEVER I need the .php to delete or overwrite the existing text before updating.

Will 1 replace 2 with your code?
can you please be more clear in the question : when you say you "need the php to delete or overwrite the existing text", where is this existing text? on the screen? in a form? in the database?

the MySQL function UPDATE is to change (update) existing data in the database. so if the text that you want to delete or overwrite is in the database, this is how to do it.
i'm going to assume that the text is in the database. so when someone changes the existing text, you need a way to identify it.

what's the format of your table? what columns are there?

Author

Commented:
okay... different parts of the site have their own section in the database.
The database will have the first lot of information loaded in then when the site owner wants to update the "whats on this week" section of the site he will use a web form that sends test to the database and is then echo'ed to the site where needed.

In this case  the form has two parts:
Heading & Text, These are stored in
DB_frontpage_whatson > heading
DB_frontpage_whatson > text

I have already got "Nothing is on this week" in the database for both Heading and Text, so when the owner goes to the maintenance form he will fill in the fields as required.

I want what ever he types into  either field to take place of its predecessor.

I hope this makes it a little clearer...
If you want or need screen shots or something let me know.

Cheers,
i could still do with knowing what columns your mysql table has. presumably it at least has two text columns for "heading" and "text". is there a unique ID column as well?

when you use the UPDATE function, you need a way of telling mysql which piece of data needs updating. normally we would have a column called "id" or something similar, which is simply a number which identifies each piece of data. so the first piece of data has id="1" the next is id="2" etc. in your case i guess we only have the one piece of data, so "id" would =1 here.

then we would tell mysql to UPDATE my_table SET heading='$new_heading', txt='$new_text' WHERE id='1'

the reason we use the id to identify the data, and not any other column, is because the data in other columns can and will change, but the unique id will always stay the same. yes you could choose to identify the data with something else, like this :

UPDATE my_table SET heading='$new_heading', txt='$new_text' WHERE heading='$old_heading'

but this requires you to already know what the old heading was, and also you have to be certain that there isn't another piece of data somewhere that shares the same old heading. for example, if we had 100 users on a system, so we have 100 pieces of data that say username, password, email, etc, and some guy chooses to change his password :

UPDATE users SET password='$new_password' WHERE password='$old_password'

this would cause ALL users who have the old password to have their passwords changed. so if 3 people have the same password, they will all change. this is not what we want! far better to use

UPDATE users SET password='$new_password' WHERE id='$user_id'

where $user_id refers to the unique id of the user we are updating. then we are certain we are only changing exactly the data we want to change.

it's clear your new to the MySQL thing - the more information you can give me, the clearer the examples i can give you to help you with. it would really help if you could tell me what columns your table has.




Aaron TomoskyDirector, SD-WAN Solutions
CERTIFIED EXPERT

Commented:
There are many ways to handle update vs insert. Here is one where you define a unique index an if it already exists it will update else it inserts
http://stackoverflow.com/questions/1218905/how-do-i-update-if-exists-insert-if-not-aka-upsert-or-merge-in-mysql
There is also insert ignore, or you can just do a select and if it exists than update else insert.

Author

Commented:
ok... heres a side question....
What about if I only want one to print at a time.... ie;

Heading One
Subtitle
Main Story blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
 blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah
 blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah


Next Story >>

Is that goinmg to be hard? It may be a more suitable solution... rather than getting rid of the information...

And yes... im new at this... LOL this will be my first PHP MYSQL site :D
to only print one row, you only retrieve one row of data.
if you don't need to keep the data, it makes sense to replace it!

you still haven't told me the layout of your table yet
i suggest you work through a tutorial to familiarize yourself with some of the basic concepts

this one is good http://www.tizag.com/mysqlTutorial/mysqlwhere.php

Author

Commented:
Okay, Sorry for the delay!
Thanks you for the link Statick it look to be a great resource! - However after a little browse through it didn't seem to answer the question for me (did teach me some tricks though!)

As for the "Tables" question i do apologise i though I had covered it... here is a screen of my phpAdmin hope this has the information your looking for!

I have had a chat to client and they like the Idea of having additional/old content avail. as per my example in "1 07:33 PM, ID: 35464203"

Look forward to your thoughts...


Screen-shot-2011-04-29-at-11.16..png
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
This looks great thanks Statick,
I am off to the office for a few hours so will give it a run tonight.
Thanks again!

Author

Commented:
Sorry for the delay guys... Life has been hectic!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.