Jump to content

[SOLVED] Updating Database Only When Submit?


wwfc_barmy_army

Recommended Posts

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.

Link to comment
Share on other sites

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
}
?>

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.