使用Ajax删除数据库行


Deleting database row using Ajax

我正试图从数据库中删除某一行,并使用while循环将其全部回显,并将x号放在右上角,使其看起来像:

echo '<a href="#delete" onclick="delete_post('.$post_id.')">';

然后使用

function delete_post(post_id)
    {
        $('table').fadeOut(1000, function () {

            // tried adding "var post_id = post_id;";didn't work
            $.ajax({
                type: "POST",
                url: "delete.php",
                data: post_id,
                beforeSend: function()
                {$('.loading_gif').css('display:inline');},
                success: function(response)
                {
                    $("table").html(response);
                    $("table").fadeOut(1000);//sakrivanje tablice kod ajax zahtjeva
                    $('table').fadeIn(1000);//prikazivanje tablice kod ajax zahtjeva
                }
            });
        })}

它会像预期的那样淡出和进入,但信息保持不变。Ajax请求到达delete.php,因为它抛出:

注意:第10行C:''examplep''htdocs''testing''delete.php中的未定义索引:post_id

注意:第32行上C:''examplep''htdocs''testing''delete.php中的未定义索引:id

无论如何,这是delete.php:

 <?php
/**
 * Created by PhpStorm.
 * User: Hrvoje
 * Date: 3/17/2015
 * Time: 4:47 PM
 */$mysqli = 'omitted';
//$mysqli ='omitted';// komentirano samo za testiranje,maknuti komentar za live stranicu
if(isset($_POST['post_id'])){echo 'zahtjev';}
$post_id=$_POST['post_id']; // line 10
$delete_query=mysqli_query($mysqli,"DELETE FROM posts WHERE post_id = '$post_id'") or die("Failed");

header('Expires: Sun, 01 Jan 2014 00:00:00 GMT');
header('Cache-Control: no-store, no-cache, must-revalidate');
header('Cache-Control: post-check=0, pre-check=0', FALSE);
header('Pragma: no-cache');
//$check_db = mysqli_query($mysqli,"CREATE TABLE IF NOT EXISTS users(post_id,)");

$txtSmileys = array('&lt;3', ' :P', ' :O', ' :D', ' :(', ' :/', " :'(", ' O:)', ' :X');   // one smylie code for each image
$imgSmileys = array('<img src="srce.jpg"/>', '<img src="p.jpg"/>', '<img src="O.jpg" />', '<img src="D.jpg"/>', '<img src="sad.jpg" />', '<img src="neutral.jpg" />', '<img src="crying.jpg" />', '<img src="anđel.jpg" />', '<img src="x.jpg" />');
// The Regular Expression filter
//example text

$id = intval($_GET['id']); // line 32

$result = mysqli_query($mysqli, "SELECT * FROM posts, users WHERE  posts.poster_id = users.id AND posts.privatnost = 'Javno' ORDER BY post_id DESC LIMIT 15;") or die("Database Error");
// run the query. Will return a resource or false
if (mysqli_num_rows($result) == 0) {
echo '<div class="no_posts">Nemate Objava</div>';
}
// if it ran OK
    $pattern = "/(http|https|ftp|ftps)':'/'/[a-zA-Z0-9'-'.]+'.[a-zA-Z]{2,3}('/'S*)?/";
    $_SESSION['id'] = '23368';//need to be amended to receive session value from login.php
    if ($result) {
    echo '<img class="ajax_gif" src="loading_gif.gif" style="margin-top:185px;margin-left:950px;position:absolute;display:none;">';
    echo '<table cellspacing="10" >';// used to display posts in order,continued after while command
    while ($user = mysqli_fetch_array($result)) {
    $user['post'] = str_replace($txtSmileys, $imgSmileys, $user['post']);// adding smiley
    $user['post'] = wordwrap($user['post'], 60, "'n", TRUE);//spliting string into multiple rows,every 60 characters
    $user['post'] = preg_replace("/@+([a-zA-ZA-z]+)/", "<a href='"$1'">$1</a>", $user['post']);//tagging system,no notification for tagged user
    $user['post'] = preg_replace($pattern, "<a href='"''0'" rel='"nofollow'">''0</a>", $user['post']);//converting links
    //$user['post'] = preg_replace("/'bfuck'b/i", '****', $user['post']); //censoring system
    $avatar = $user['avatar'];
    $objavljivač = $user['ime'];
    $username = $user['username'];
    $session_poster_id = intval($user['poster_id']);
    $post_id = intval($user['post_id']);
    echo '<tr>';
    echo '<td>';
    echo '<a href="korisnik.php?id='.$session_poster_id.'" style="width:40px;height:40px;">';
    echo '<div class="profile_pic_div" style="margin-left:5px;margin-top:5px;width:40px;height:40px;position:absolute;">';
    echo "<img src='$avatar' style='width:40px;height:40px;'>";
    echo '</div>';
    echo '</a>';
    echo '<div class="timestamp" style="margin-left:50px;margin-top;font-size:15px;">';
    echo $user['vrijeme'];
    echo '</div>';
    if($_SESSION['id'] == $session_poster_id){
        echo '<a href="#delete" onclick="delete_post('.$post_id.')">';
        echo '<div class="icon-x" style="margin-left:550px;margin-top:-10px;position:absolute;">';
        echo '</div>';
        echo '</a>';}
    echo '<div class="post_div" style="margin-top:30px;">';
    if (strlen($user['post']) > 500) {
        $user['post'] = substr($user['post'], 0, 500);
        $user['post'] = substr($user['post'], 0, strrpos($user['post'], ' ')).'... <a href="story.php?post_id='.$post_id.'">Read More</a>';}
    echo  $user['post'];
    echo '</div>';
    echo '<br>';
    echo '<a href="story.php?id='.$post_id.'">Komentiraj</a>';
    echo '</td>';
    echo '</tr>';

}
echo '</table>';} ?>
$id = intval($_GET['id']); 

当AJAX通过POST发送数据时,您正在使用GET,但无论哪种方式,都不会发送名为"id"的值。仅设置了post_id。

data: post_id,

我看到行:

$mysqli = 'omitted';

它显示您没有任何数据库连接。你需要设置它连接到数据库使用类似:

 $mysqli = new mysqli("localhost", "my_user", "my_password", "mydb");

其中参数是主机名、用户名、密码和数据库名称。