Jump to content


Photo

editing data (probably simple, but I'm new)


  • Please log in to reply
1 reply to this topic

#1 smoreno

smoreno
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 28 July 2006 - 02:07 PM

Ok basically I know little to nothing about php, but I am helping make this website. I've read a couple of tutorials but I just can’t seem to get it for some reason.

Basically the site needs to have an announcements page. People are able to post announcements and then they show up, I got this part figured out. Now though, I need a page where I can see the announcements data in a form that it can be edited by a user on the website.


Page where data is displayed:
<?php
   $link = mysql_connect("mysql", "name", "pwrd");
   mysql_select_db("announcements");
   
   /* Performing SQL query */
    $query = "SELECT * FROM announce";
    $result = mysql_query($query) or die("Query failed");
	 
    /* Printing results in HTML */
    print "<table>\n";
    while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
        print "\t<tr>\n";
        foreach ($line as $col_value) {
            print "\t\t<td>$col_value</td>\n";
        }
        print "\t</tr>\n";
    }
    print "</table>\n";

    /* Free resultset */
    mysql_free_result($result);

    /* Closing connection */
    mysql_close($link);


  
?>
Page where data is submitted:
<form name="form1" method="post" action="insert_it.php">
   <table width="100%" border="0" cellpadding="5" cellspacing="0">
  <tr>
    	<td valign="top"> Announcement</td>
    
			<td>		
			<textarea name="announce" cols="40" rows="10"></textarea>
			</td></tr>
<tr>
<table width="50%" border="0" cellpadding="5" cellspacing="0">
<tr>
<td width="25%">Show Annoucement?</td>
<td width="25%"> 		
			<input type="radio" name="display" value="Y">Yes
			</td>
<td>		
			<input type="radio" name="display" value="N">No
			</td>
</tr>
</table>

	</tr>
	<tr><td><input type="submit" name="Submit" value="Submit"></td></tr>
	</table>
	
</form>

Page you are brought to after you hit submit:
<?php
 $link = mysql_connect("mysql", "user", "pwrd");
   mysql_select_db("announcements");
   $query = "INSERT INTO announce (text, display) VALUES ('$announce', '$display')";
   


$results = mysql_query($query) or die(mysql_error());
echo $result;
echo $query;

mysql_close();

print "<html><body><center>";
print "<p>You have just entered this record<p>";
print "Name : $announce<br>";
print "</body></html>";

?>


I just need a page where the data that has been entered can be edited.
If anyone could help it would be greatly appreciated.

Thanks!

#2 ober

ober
  • Staff Alumni
  • Advanced Member
  • 5,337 posts
  • LocationEast Coast, USA

Posted 28 July 2006 - 02:23 PM

I'm going to give you a high level overview since you have the tools to do the work.

You basically take the form that you have to create a new annoucement and fill it with values from the database.  I'm not sure how you provide the edit feature (button on the annoucement based on who is logged in?) but you'd end up going to (I'm going to assume some things) newannounce.php?postid=1234 from the link on the post to edit the post.

Once you get to that page, you run a SQL query with that ID in a WHERE clause.  You grab all the data in the record (mysql_fetch_assoc or mysql_fetch_array), and then in your form you have something like this:
<td>
<?php
    if(isset($row['announce']))
    {	
        echo '<textarea name="announce" cols="40" rows="10">' . $row['announce'] . '</textarea>';
    }
    else
    {	
        echo '<textarea name="announce" cols="40" rows="10"></textarea>';
    }
?>
</td></tr>

And you do that for all the elements in the form that have a value in the database.

Hope that helps.

EDIT: And don't forget to use the ID value in the UPDATE statement!

Info: PHP Manual





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users