使用Ajax删除数据库行

时间:2015-03-22 14:48:45

标签: php jquery 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:\ xampp \ htdocs \ testing \ delete.php中的post_id

注意:未定义的索引:第32行的C:\ xampp \ 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>';} ?>

2 个答案:

答案 0 :(得分:1)

$id = intval($_GET['id']); 

当AJAX通过POST发送数据时,您正在使用GET,但无论哪种方式,都没有发送名为“id”的值。只设置了post_id。

data: post_id,

答案 1 :(得分:1)

我看到了一行:

$mysqli = 'omitted';

它显示您没有任何数据库连接。您需要将其设置为使用like:

连接到数据库
 $mysqli = new mysqli("localhost", "my_user", "my_password", "mydb");

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