Update a Record, Display Results. One Page.
Posted: Mon Oct 19, 2009 10:40 am
Hello everyone.
I've decided to learn PHP, and I'm in week 3 of my education. My heads hurts, no one said it would be algebra! I'm finding it very exciting. One day, I want to be able to create simple content management systems. I've just written my first piece of code, and I could do with your help and critique on it.
It displays some text saved in a database. You can adjust the text using a text box and click update, it then shows you the new updated text.
Simple really.
What I'd like to know is,
- Is there a better way of doing this?
- Are any of the functions I'm using dated or just wrong?
- Could more error messages be added to help if things go wrong, where?
Generally I'd love to discuss with you how this code can be streamlined, made more professional and efficient. It's my first php...
My own critique: I don't like that the refresh comes after 2 seconds, and whilst your waiting the text in the text box goes back to the original record entry. I also don't like that the message "Please wait, updating..." is at the bottom of the page and not at the top. I don't know how you'd move it.
Next I'm going to work on stripping the html tags but including line breaks, then securing this page using login and sessions and finally making it looks attractive in CSS.
Great to be here,
Thanks in advance for any help!
-- wobblywobbly.
I've decided to learn PHP, and I'm in week 3 of my education. My heads hurts, no one said it would be algebra! I'm finding it very exciting. One day, I want to be able to create simple content management systems. I've just written my first piece of code, and I could do with your help and critique on it.
It displays some text saved in a database. You can adjust the text using a text box and click update, it then shows you the new updated text.
Simple really.
What I'd like to know is,
- Is there a better way of doing this?
- Are any of the functions I'm using dated or just wrong?
- Could more error messages be added to help if things go wrong, where?
Generally I'd love to discuss with you how this code can be streamlined, made more professional and efficient. It's my first php...
Code: Select all
<?php
/*
Hello, you'll need an SQL database, mine's called "default"
and a table in that database called "textrecord"
and a field in that table called "blurb"
Insert some text data into your record to begin wth, this script only edits the record, it doesn't create new text
Thanks!
*/
// connecting to the database
$connect = mysql_connect ("localhost","root","root") or die ("Connection to database failed");
mysql_select_db ("default");
//extracting the record from the database and displaying it
$extract = mysql_query ("SELECT * FROM textrecord");
$numrows = mysql_num_rows ($extract);
while ($row = mysql_fetch_assoc ($extract))
{
$currentblurb = $row ['blurb'];
}
echo "Your record currently contains this text: <p>";
echo "'$currentblurb'";
//Next is the form to update the record. I want the text area to show what's currently contained in the record so it can easily be edited.
?>
<form method = 'post' action='updatingrecords.php'>
Edit the text and click update.
<p>
<textarea name="blurbarea" rows="20"><?php echo $currentblurb; ?></textarea>
<p>
<input type='submit' name = 'submit' value='Update'>
<p>
</form>
<?php
// updating the record with the new text once it's been submitted
$blurbarea = $_POST ['blurbarea'];
$submit = $_POST ['submit'];
if ($submit)
{
//update the record with the users new text
$update = mysql_query ("UPDATE textrecord SET blurb = '$blurbarea'");
//Refresh the page with the new record showing
echo "Please wait, updating... <meta http-equiv = 'refresh' content = '2'>";
}
?>
My own critique: I don't like that the refresh comes after 2 seconds, and whilst your waiting the text in the text box goes back to the original record entry. I also don't like that the message "Please wait, updating..." is at the bottom of the page and not at the top. I don't know how you'd move it.
Next I'm going to work on stripping the html tags but including line breaks, then securing this page using login and sessions and finally making it looks attractive in CSS.
Great to be here,
Thanks in advance for any help!
-- wobblywobbly.