hey there,
how do i go about editing something form a db. It was added to from a form and submitted.
thanks in advanced!
Printable View
hey there,
how do i go about editing something form a db. It was added to from a form and submitted.
thanks in advanced!
all of this stuff is outlined in this post and already has all the information you've been asking for lately.PHP Code:mysql_query("UPDATE addressbook SET name='newname', address='newaddress' WHERE name='david' AND phonenumber='555-5555' LIMIT 1");
yeah, but i want to have it taken from the db, put into a form to be edited then put back into the db.
query the database before you make the form and get the fields you want and save them to a variable.. for example:
now, for each <input> or <textarea> that you have, put the corresponding field name into that objects "value." for example:PHP Code:<?php
$olddata_query = mysql_query("SELECT fieldname1, fieldname2 FROM tablename WHERE id=12313 LIMIT 1");
$olddata = mysql_fetch_array($olddata_query);
?>
<!-- print your form -->
Then, just submit it and update like I posted above.PHP Code:<!-- this is for an <INPUT> tag -->
<input type="text" name="fieldname1" value="<?php echo $olddata['fieldname1']; ?>">
<!-- this is for a <TEXTAREA> tag -->
<textarea name="fieldname2"><?php echo $olddata['fieldname2']; ?></textarea>
cool! works. now, how do i put in the UPDATE code into my exsting code, here it is:
PHP Code:<?php
$db = mysql_connect('localhost', 'homtek_dclamp', 'password') or die(mysql_error());
mysql_select_db('homtek_homtek', $db) or die(mysql_error());
$olddata_query = mysql_query("SELECT subject, news FROM news WHERE news_id='".$_GET['id']."' LIMIT 1");
$olddata = mysql_fetch_array($olddata_query);
?>
<!-- print your form -->
<form action="add_news.php" method="POST">
Subject:<br>
<input type="text" name="subject" value="<?php echo $olddata['subject']; ?>"><br><br>
News Content:<br>
<textarea columns=5 rows=10 name="content"><?php echo $olddata['news']; ?></textarea>
<input type="hidden" name="poster" value="<?PHP echo $logged_in_admin; ?>">
<input type="hidden" name="date" value="DATE_HERE">
<input type="submit" value="Submit"> <input type="reset" value="Reset Form">
</form>
you have to put the update query in your add_news script, not that one.
i actualy made a error on that. when i send it to that it just adds it as a new post. so it would have to go to another one. should i just make a new page? or is there a way that i can put it there?
well, you can always use GET requests and change the form action to:
add_new.php?edit=true
then, in the add_new.php you will just have to check the value of $_GET['edit'] and if it's true, then you know you need to edit a post instead of make a new one. this could get somewhat complex (although, if you know what you're doing it could be pretty simple too), so it would be easier to just make a separate script that edits.