Jump to content

useing php to edit and pull data from a database


smoreno

Recommended Posts

I am currently using yahoo as a host, and they supply a mysql server, and they support php.

I am very new to php and I am having trouble figuring out how to get data from the database and being able to edit it.

Basically, on my page, I want an announcements section. I want this data to be editable though, but only by certain people. I have made a page that is password protected, I only need the codeing that would allow them to edit it.

So, 2 pages, one that pulls the data from the database and displays it, and a page that allows that data to be edited.

Could anyone help me with this or lead me to a website that would show me how?

Thanks!
Link to comment
Share on other sites

$select = "SELECT * FROM tablename";
$query = mysql_query($select);
//2 functions mysql_num_rows and mysql_fetch_array
$num_rows = mysql_num_rows($query);
while ($rows = mysql_fetch_array($query)) {
//format the data for viewing, with tables or lists or whatever.
//for instance lists
echo "<ul type=/"disc/">";
echo "<li>" . $rows['variable'] . "</li>";
echo "<li>" . $rows['variable2'] . "</li>";
echo "<li>" . $rows['variable3'] . "</li>";
echo "</ul>";
// this displays the data as a list, then if you wanted it as tables, something like this
echo "<table>";
echo "<tr><td>" . $rows['variable'] . "</td></tr>";
echo "<tr><td>" . $rows['variable2'] . "</td></tr>";
echo "<tr><td>" . $rows['variable3'] . "</td></tr>";
echo "</table>";
}
// I didn't have a lot of time so these are a little off, for example , it's going to create a new list for every row, then a new table for every row so instead if you use the list have the
echo "<ul>"; or whatever before the while statement
and the end tag after it, the same with table start and finish
also while and foreach are roughly the same thing, after asking hundreds of developers none of them notice a difference so you can safely use either.
As for updating, you can set up a straight update, where you have fields, and  they update the stuff automatically, without testing, or test for hte existence of first, then update if it doesn't exist.
to update it's just the exact same query above just with alter
or insert if you are just adding data, you could also use update, which is what I always use.
$select = "UPDATE columnname FROM tablename WHERE whatever.
You get the point as soon as that query hits mysql query it's run automatically so be carefuly if you do
$select = "UPDATE mycolumn SET whatever
it allows you to set it but as soon as you use
mysql_query($select)
then the data is updated.
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.