ppwalks Posted February 9, 2011 Share Posted February 9, 2011 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 When you do an update, you can do UPDATE table(col1, col2, col5) VALUES(val1, val2, val5) WHERE [...] to skip some columns. Post your code! Quote Link to comment Share on other sites More sharing options...
ppwalks Posted February 9, 2011 Author Share Posted February 9, 2011 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 Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 I only see you doing an Insert, no updates. Quote Link to comment Share on other sites More sharing options...
ppwalks Posted February 9, 2011 Author Share Posted February 9, 2011 so how would i do that if you don't mind me asking? Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 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. Quote Link to comment Share on other sites More sharing options...
ppwalks Posted February 9, 2011 Author Share Posted February 9, 2011 I have actually tried the update command and still gave me the same problem Quote Link to comment Share on other sites More sharing options...
ppwalks Posted February 9, 2011 Author Share Posted February 9, 2011 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! Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 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. Quote Link to comment Share on other sites More sharing options...
ppwalks Posted February 9, 2011 Author Share Posted February 9, 2011 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) ) Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 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? Quote Link to comment Share on other sites More sharing options...
ppwalks Posted February 9, 2011 Author Share Posted February 9, 2011 Not made the field yet i was just experimenting with the first 2, it is for a static page to literally input content from a user via a form and have it display on the page but got stuck at the multiple submission, how will the id help please? Quote Link to comment Share on other sites More sharing options...
ppwalks Posted February 9, 2011 Author Share Posted February 9, 2011 And appologies for the confusion, this is the code from my pc it does correlate correctly on the server.. And may i say thankyou so much for you help so far you have been fantastic.... Quote Link to comment Share on other sites More sharing options...
ppwalks Posted February 9, 2011 Author Share Posted February 9, 2011 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) Quote Link to comment Share on other sites More sharing options...
Jessica Posted February 9, 2011 Share Posted February 9, 2011 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. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.