wwfc_barmy_army Posted October 25, 2007 Share Posted October 25, 2007 Hello. I'm still pretty new to php, I have the following code: <?php $result = mysql_query("SELECT textblock FROM text WHERE textid = 3") or die(mysql_error()); $row = mysql_fetch_array( $result ); $texttoedit = $row['textblock']; ?> <form action="<?php $PHP_SELF ?>" method="post" name="edittext" id="edittext"> <textarea name="edittext" cols="100" rows="40"><?php echo $texttoedit; ?></textarea> <input name="Submit" type="submit" value="Submit" /> </form> <?php $editedtext = $_POST['edittext']; echo $editedtext; $query="UPDATE text SET textblock='$editedtext' WHERE textid = 3"; mysql_query($query); echo "Record Updated"; mysql_close(); ?> In the database i manually added some text into the field, when i first load this page it comes up with the text in the textarea, but on second load it has cleared the text out of the field. So my questions are: 1. How do i make it so it only executes the update query when the submit button has been pressed? 2. Why is it currently clearing the table? Is it just because submit hasn't been pressed yet (solved by number one)? Is the code ok? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/74803-solved-updating-database-only-when-submit/ Share on other sites More sharing options...
stuffradio Posted October 25, 2007 Share Posted October 25, 2007 Copy paste this code, it should be better and working for you. <?php $result = mysql_query("SELECT textblock FROM text WHERE textid = '3"'0) or die(mysql_error()); // Unless you are still debugging you need to remove the mysql_error part... it leaves for some potential exploits $row = mysql_fetch_array( $result ); $texttoedit = $row['textblock']; ?> <form action="<?php $PHP_SELF ?>" method="post" name="edittext" id="edittext"> <textarea name="edittext" cols="100" rows="40"><?php echo $texttoedit; ?></textarea> <input name="Submit" type="submit" value="Submit" name="Yourbutton"/> </form> <?php $editedtext = $_POST['edittext']; echo $editedtext; if ($_POST['Yourbutton']) { $query="UPDATE text SET textblock='$editedtext' WHERE textid = 3"; mysql_query($query); echo "Record Updated"; mysql_close(); } else { // Do nothing or add something here } ?> Quote Link to comment https://forums.phpfreaks.com/topic/74803-solved-updating-database-only-when-submit/#findComment-378232 Share on other sites More sharing options...
wwfc_barmy_army Posted October 25, 2007 Author Share Posted October 25, 2007 Thanks! It works Quote Link to comment https://forums.phpfreaks.com/topic/74803-solved-updating-database-only-when-submit/#findComment-378237 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.