|
|||
How to Add and Delete users to a Web Database using PHP and MySql
So if you want to learn how to add and delete users to a Web Database using PHP and Mysql, consider reading this chapter out of: Learning PHP, MySQL, and Javascript, 1st Edition
I would not use this example on a Public website, but rather on a site where you want to administer a names or items table. For instance, I have forked the code here to have a public add where I do not give access to users to delete from the table. On my staging Table, I have used the code below to allow me to administer users from both an add and delete perspective. I found this code very useful to get up and running quickly. And I am a newbie. Chapter 10 Accessing MySQL Using PHP > A Practical Example 10.2. A Practical Example I am not going to add all the verbage from the chapter, but rather just the code. First you'll need to create "login.php" with your DB particulars. <?php // login.php $db_hostname = 'localhost'; $db_database = 'nameofdb'; $db_username = 'nametologin'; $db_password = 'passauthen'; ?> Secondly, you then either edit the fields below to match a table you have or make a table with the fields author, title, category, year and ISBN. The rest is pretty simple. Or if it is not, you could read the whole chapter and be set. <?php // sqltest.php require_once 'login.php'; $db_server = mysql_connect($db_hostname, $db_username, $db_password); if (!$db_server) die("Unable to connect to MySQL: " . mysql_error()); mysql_select_db($db_database, $db_server) or die("Unable to select database: " . mysql_error()); if (isset($_POST['author']) && isset($_POST['title']) && isset($_POST['category']) && isset($_POST['year']) && isset($_POST['isbn'])) { $author = get_post('author'); $title = get_post('title'); $category = get_post('category'); $year = get_post('year'); $isbn = get_post('isbn'); if (isset($_POST['delete']) && $isbn != "") { $query = "DELETE FROM classics WHERE isbn='$isbn'"; if (!mysql_query($query, $db_server)) echo "DELETE failed: $query<br />" . mysql_error() . "<br /><br />"; } else { $query = "INSERT INTO classics VALUES" . "('$author', '$title', '$category', '$year', '$isbn')"; if (!mysql_query($query, $db_server)) echo "INSERT failed: $query<br />" . mysql_error() . "<br /><br />"; } } echo <<<_END <form action="sqltest.php" method="post"><pre> Author <input type="text" name="author" /> Title <input type="text" name="title" /> Category <input type="text" name="category" /> Year <input type="text" name="year" /> ISBN <input type="text" name="isbn" /> <input type="submit" value="ADD RECORD" /> </pre></form> _END; $query = "SELECT * FROM classics"; $result = mysql_query($query); if (!$result) die ("Database access failed: " . mysql_error()); $rows = mysql_num_rows($result); for ($j = 0 ; $j < $rows ; ++$j) { $row = mysql_fetch_row($result); echo <<<_END <pre> Author $row[0] Title $row[1] Category $row[2] Year $row[3] ISBN $row[4] </pre> <form action="sqltest.php" method="post"> <input type="hidden" name="delete" value="yes" /> <input type="hidden" name="isbn" value="$row[4]" /> <input type="submit" value="DELETE RECORD" /></form> _END; } mysql_close($db_server); function get_post($var) { return mysql_real_escape_string($_POST[$var]); } ?> 1 Reply
Hello Mike H,
Thanks for this code. I'm actually in the binging phase of web services. I used your above code in my work, but i'm struggling with To delete a record from the MYSQL table using web services. Your code work good for Adding and Displaying a record but it is not working well for deleting a record from MYSQL database. I would like to request you to take a look at your code and fix that bug. Any kind of help is appreciated. Thanks, Sajed Shaikh Email: [email protected] |
|||
|