Jump to content

Please HELP! SQL Nigtmare


ppwalks

Recommended Posts

Hello,

I am new to php programming and one obstacle has got me stuck now for several days and driving me insain, please if someone could point me in the right direction i would be ever so grateful. As part of a project at college I have been given an assignment to post data to sql database and then retrieve all the fields and output them to page, all is well up to now.

 

i have made the form which successfully submits data to sql and i have been able to the echo the appropraite variables back to page. The problem I have is when I submit the form it sends all fields, if I wanted to only change 2 fields then it would overwrite the whole thing then display the 2 fields inserted only, i want it to display the data before the new row has NULL atributes in them. Please can somebody help!

 

Thankyou

 

Paul

Link to comment
Share on other sites

To easier explain I have a normal table is in MyISAM format with  4 fields, metaTag, metaDesc, pageTitle & pageContent each in there own column, i have placed a form on the back end of the website which has input fields to pass the value to each table row.

 

A form example which is in its own include;

 

 <form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"/>
		 <label>Page Title</label><input type="text" id="meta-data" name="home_title"/>
         <span class="example">Example: Company Name LTd, "main Service"..</span>
        <label>Home Page Text</label><textarea class="body-text" name="home_text"></textarea><input type="submit" value="Update" id="home-update" name="home_update"/>
        </form>

 

 

Then from the form it will send to database like so;

if (isset($_POST['home_update']))
				{ mysql_select_db("barryjam_cms");

					$sql="INSERT INTO cms (pageTitle, pageText) VALUES ('$_POST[home_title]', '$_POST[home_text]')";

							if (!mysql_query($sql))
  							{
  								$error =('Error: ' . mysql_error());
  							}
								 else
							$result_home = "updated";
							echo "<script>setTimeout(\"location.replace('control_panel.php')\",2000)</script>";
						mysql_close();

					}
					include ('inc/home_form.php');

 

As i mentioned this is only a simple bit of code, but when i submit say only page title in the form it will add to pageTitle in the database and leave the other column blank, so when the data is output to the page it will only display the most recent field. I want it so if the field is blank it will still use the previous data on the page. An easy solution would be to use multiple tables but at guess i would say that is extremely bad practice.

 

Please remember i'm a student trying to learn php & Mysql so my terminolgy and knowledge may not be adequate.

 

If you could point me in the right direction i would be ever so grateful and thankyou for your time.

 

PS. i have not took any measures as yet to stop sql injections nor formatted the output string to be html compliant, this is just the foundation to my building blocks. If i'm going in the wrong direction can you please let me know also.

 

Thankyou

 

paul

Link to comment
Share on other sites

so how would i do that if you don't mind me asking?

 

Like how I posted above.

 

I would recommend finding a good SQL tutorial if you don't yet know the difference between Insert and Update. I like TiZag's tutorials but I think phpfreaks has some good ones too still.

Link to comment
Share on other sites

i get the update part of the query, its the "WHERE" section, if they are random from say 10 fields how would i implement the "WHERE" when not knowing what the previous attribute would be?

 

Thankyou for your time, you have already been so much help!

Link to comment
Share on other sites

i get the update part of the query, its the "WHERE" section, if they are random from say 10 fields how would i implement the "WHERE" when not knowing what the previous attribute would be?

 

Thankyou for your time, you have already been so much help!

 

You'd need to give each a unique ID. You will probably need to clean up your database anyway.

Post your table structure.

Link to comment
Share on other sites

this is the table i created in notepad then executed it in phpMyAdmin

 

CREATE TABLE cms (
rowID INT NOT NULL AUTO_INCREMENT,
metaName VARCHAR(100) NOT NULL,
metaDesc VARCHAR(200) NOT NULL,
homeText VARCHAR(600) NOT NULL,
aboutText VARCHAR(1000) NOT NULL,
servicesText VARCHAR(1500) NOT NULL,
PRIMARY KEY(rowID)
)

Link to comment
Share on other sites

So your WHERE clause would need to be limited by the rowID (which should probably be pageID if these are pages.)

 

This can't be the same table though, because your previous post says

INSERT INTO cms (pageTitle, pageText)

 

where did pageTitle and pageText go?

Link to comment
Share on other sites

Thankyou "JESIROSE" its working well, thankyou for the help I used "WHERE rowID = 1" and all is well, first i emptied the database and started again with your advise and worked well, so thankyou again. (Nice dog in the picture by the way)

 

Thanks! If it's working, don't forget to mark solved at the bottom.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.