|
| Web Hosting Deals | Holiday Logo Design | Webcam Chat | Website Header Templates | Register domain | Search Engine Optimisation | Web Hosting |
|
|||||||
| PHP / Perl / Java / JavaScript / CGI Tutorials PHP / Perl / Java / JavaScript / CGI Tutorials Please do not use this Forum to advertise your site or to link to tutorials. |
![]() |
|
|
Submit Tools | LinkBack | Thread Tools | Display Modes |
|
||||
|
The CMS (#6) PHP
Ok, so in this kind of mammoth tutorial, we are going to be:
Adding users Editing users So lets start off with some add-user.php in your admin directory! PHP Code:
Lines 2 to 8 are are security and including our database connection. Line 11: if($_POST['submit']) { - So we are only going to execute this code, if someone has clicked one of the form buttons. $username = $_POST['username']; $pw = $_POST['pw']; In the case of someone editing a user, we need to get their username & password. $query = "INSERT INTO users (ID, username, pw) VALUES ('', '$username', '$pw')"; Inserting the new users into the database! mysql_query($query) or die(mysql_error()); echo "User has been added to the database sucessfully"; Running the query, and echoing to the user. } - Ending the if statement. Nothing then needs explaining untill this line: <?php } ?> This is just just the end of the security, so if a user visits the page and is not logged in, they won't see anything before they are redirected. -_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_-_ PHP Code:
The first 17 lines (up to: The MySQL query. Select all from the table users where the ID equals the id sent in URL.) We should all know before if we have looked at my previous tutorials. $query = "SELECT * FROM users WHERE ID='$id'"; - remember we are getting the variable $id from here: <input type="hidden" name="id" value="<?php echo "$ID"; ?>" /> Onwards! extract($row); $query = "UPDATE users SET username = '$username', pw = '$pw' WHERE ID = '$id'"; So we are extracting the row now, and creating this query: "Update the table users, and set the username to $useraname, and the password to $pw where the row ID is $id" Get it? } else { // Lets get into the code! $query = "SELECT * FROM users ORDER BY ID DESC"; So this code will only happen if nobody has clicked "modify" i.e. they havent submitted the edit. $result = mysql_query($query) or die(mysql_error()); //Displaying the results of the query. while ($row = mysql_fetch_array($result)) { Again some nice and simple code, just running our query, and putting it into a while loop. So to explain it. All of the code in between this: while ($row = mysql_fetch_array($result)) { And this } (this is at the bottom of the code) Will be placed into a while loop, so baisicly for every result it will execute that code. You get me? Hope you have gleamed that little bit more information and knoledge. As allways a file is included. Cheers! Base
__________________
http://www.pointserv.co.uk/gfx/deal_..._pointserv.png PointServ.co.uk - One of the UK's cheapest webhosting companies. |
![]() |
| Bookmarks |
| Thread Tools | |
| Display Modes | |
|
|