开发者

Am I proceeding with coding an edit and delete feature correctly in php/mysql, phpMyAdmin

I am working on adding a edit and delete feature to my basic blog app. I am struggling with having the my edit.php code and delete.php code process correctly.

When a person clicks on the delete or edit button the code in the correlating php file does not process.

Main PHP file:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<div id="container">

  <h1>Lay Down Your Thoughts</h1>

    <div id="boxtop"></div>
  <div id="content">

     <!-- form to leave a message -->
    <form action="<?php $self ?>" method="post">
    <h2>Post your thought!</h2>

    <div class="fname"><label for="name"><p>Name:</p></label><input name="name" type="text" cols="20" /></div>
    <div class="femail"><label for="email"><p>Email:</p></label><input name="email" type="text" cols="20" /></div>
    <label for="message"><p>Message:</p></label>
    <textarea name="post" rows="5" cols="40"></textarea>
    <input name="send" type="hidden" />
    <p><input type="submit" value="send" /></p>
    </form>

 <?php
    $self = $_SERVER['PHP_SELF']; //the $self variable equals this file
    $ipaddress = ("$_SERVER[REMOTE_ADDR]"); //the $ipaddress var equals users IP
    include ('db.php');
        // checks the POST to see if something has been submitted
        if(isset($_POST['send'])) 
            if(empty($_POST['name']) || empty($_POST['email']) || empty($_POST['post'])) {
                echo('<p class="error">You did not fill in a required field.</p>');
            } else {

                // if there are no empty fields, insert into the database:

                //validate through htmlspecialchars()
                // eliminates the user from submitting harmful html 
                // also runs through mysql_real_escape_string()
                // stops users sending SQL code to infiltrate the db
                $name = htmlspecialchars(mysql_real_escape_string($_POST['name'])); 
                $email = htmlspecialchars(mysql_real_escape_string($_POST['email'])); 
                $post = htmlspecialchars(mysql_real_escape_string($_POST['post']));

                    // this is our SQL string to insert shouts into db
                    $sql = "INSERT INTO messages SET name='$name', email='$email', post='$post', ipaddress='$ipaddress';";

                        // run the SQL string
                        // if it succeeds, display message
                        if (@mysql_query($sql)) {
                            echo('<p class="success">message has been posted</p>');
                        } else {
                            // if error, send message
                            echo('<p class="error">There was an unexpected error when posting your message.</p>');
                        }
             }

        // display 8 latest messages
        $query = "SELECT * FROM messages ORDER BY `id` DESC LIMIT 8;";

        // run query if it fails display fail
        $result = @mysql_query("$query") or die('<p class="error">There was an unexpected error collecting messages.</p>');

        ?><ul><?
        // display the rows from the post
        while ($row = mysql_fetch_array($result)) {

            $ename = stripslashes($row['name']);
            $eemail = stripslashes($row['email']);
            $epost = stripslashes($row['post']);

            // gravatar image 
            $grav_url = "http://www.gravatar.com/avatar.php?gravatar_id=".md5(strtolower($eemail))."&size=70"; 

            echo('<li><div class="meta"><img src="'.$grav_url.'" alt="Gravatar" /><p>'.$ename.'</p></div><div class="message"><p>'.$epost.'</p></div></li>');

            echo ('<form action="messageME_final_delete.php" method="post"><input name="delete" type="hidden" /> <p><input type="submit" value="delete" /></p></form>');

             echo('<form action="messageME_final_update.php" method="post"><input name="edit" type="hidden" /> <p><input type="submit" value="edit" /></p></form>');


        }
        ?></ul><?
    ?>

  </div><!--/content-->
  <div id="boxbot"></div>

</div><!--/container-->

</body>
</html>

Here is the Edit php file:

<form action="messageME_final_update.php" method="post">
    <h2>Edit this Thought!</h2>

    <div class="fname"><label for="name"><p>Name:</p></label><input name="name" type="text" cols="20" /></div>
    <div class="femail"><label for="email"><p>Email:</p></label><input name="email" type="text" cols="20" /></div>
    <label for="message"><p>Mes开发者_JAVA技巧sage:</p></label>
    <textarea name="post" rows="5" cols="40"></textarea>
    <input name="send" type="hidden" />
    <p><input type="submit" value="send" /></p>
    </form>

 <?
 include ('db.php');

$query="UPDATE messages SET name='name', email='email', post='post' WHERE id='ID'";
mysql_query($query);
echo "Record Updated";
mysql_close();
?>

finally here is the delete php code:

<?php

   include ('db.php');

 $sql = "DELETE FROM `messages` WHERE `ID` ="  ." mysql_real_escape_string ( $_GET['ID'] )";

 mysql_select_db ( $database, $connect );

 if ( @mysql_query ( $sql ) )
 {
  echo 'Article ID = ' . $_POST['ID'];
  echo ' was deleted successfully';
 }
 else {
  die ( mysql_error () );
 }
?>


Your update page has no code related to identifying what post the user wants to edit at all. It just presents a new form and tries to update a row with an ID of the string 'ID'.

Your delete page tries to access both $_GET['ID'] and $_POST['ID'], which won't ever both be set since an HTTP request is always of a single method (GET, or POST, or HEAD, etc). You also fail to concatenate the string with a function correctly, instead sending the literal text "mysql_real_escape_string(..." as part of the query, which will not run.

$sql = "DELETE FROM messages WHERE ID = " . (int)$_POST['ID'];

...is closer to what you want, except that your form on the post list does not contain an element named ID. You should create one, and populate it with the ID of the post that row corresponds to.

<input type="hidden" name="ID" value="<?php echo $row['ID']; ?>" />

Do the same for the form pointing to the edit page, and use $_POST['ID'] to look up the post and populate the form fields for editing.

Suggested reading, which will walk you through building all aspects of a CMS in PHP/MySQL:

http://www.amazon.com/Build-Database-Driven-Using-MySQL/dp/0980576814/ref=dp_ob_title_bk

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜