-2

i tried to send a php array values using ajax but nothing happened and mysql table didn't update....

html:

//edit topic
if(isset($_GET['edit_topic']) && is_numeric($_GET['edit_topic'])  ){
    $topic_id=htmlspecialchars($_GET['edit_topic']);


$get_topic=$db->query("select * from articles where art_id='$topic_id'");
    $topic=$get_topic->fetch_assoc();
    ?>

    <form role="form"  action="" id="edit_topic_form" method="post">
    <div class="row">
    <div class="col-md-6">عنوان الموضوع</div>
    <div class="col-md-6"><input type="text" class="form-control" name="art_title" id="art_title" value="<?php echo $topic['art_title']; ?>"></div>
    </div>
.........................
    </form>
    <?php


}

php:

if($_POST){
        $topic_array=array(
        "art_title"=>$_POST['art_title'],
        "art_subtitle"=>$_POST['art_subtitle'],
        "art_desc"=>$_POST['art_desc'],
        "art_tags"=>$_POST['art_tags'],
        "art_download"=>$_POST['art_download'],
        "art_yt"=>$_POST['art_yt'],
        "art_instructor"=>$_POST['art_instructor'],
        "art_com_no"=>$_POST['art_com_no'],
        "art_likes"=>$_POST['art_likes']
        );

        $update_topic=$db->query("update articles set 
        art_title='".$topic_array['art_title']."',
        art_subtitle='".$topic_array['art_subtitle']."',
        art_desc='".$topic_array['art_desc']."',
        art_tags='".$topic_array['art_tags']."',
        art_download='".$topic_array['art_download']."',
        art_yt='".$topic_array['art_yt']."',
        art_instructor='".$topic_array['art_instructor']."',
        art_com_no='".$topic_array['art_com_no']."',
        art_likes='".$topic_array['art_likes']."'
        where art_id='$topic_id'
        ");
        }

jquery:

$(document).ready(function(){
var request;

$("#edit_topic_form").submit(function(event){


    if (request) {
        request.abort();
    }

    var $form = $(this);

    var $inputs = $form.find("input, select, button, textarea");

    var serializedData = $form.serialize();

    $inputs.prop("disabled", true);


    request = $.ajax({
        url: "dashboard.php",
        type: "post",
        data: serializedData
    });


    request.done(function (response, textStatus, jqXHR){
        // Log a message to the console
       alert("تم تعديل الموضوع بنجاح");
    });



    request.always(function () {
        // Reenable the inputs
        $inputs.prop("disabled", false);
    });


    event.preventDefault();
});




    })
Phil
  • 157,677
  • 23
  • 242
  • 245
Mohamed Amin
  • 997
  • 2
  • 12
  • 29
  • 2
    **warning** `htmlspecialchars` is insufficient for defeating SQL injection! – Daniel A. White Mar 11 '15 at 22:48
  • thanks,security issues will be taken in consideration in the final review for the project... – Mohamed Amin Mar 11 '15 at 22:52
  • What is the output of `var_dump($_POST)`? – AlexL Mar 11 '15 at 22:55
  • _"security issues will be taken in consideration in the **final review** for the project"_ that is literally the worst time to be thinking about security – Phil Aug 05 '19 at 00:25
  • Where is `$topic_id` assigned a value in your _"php"_ part? My guess is you are trying to get it from the request (either `$_GET` or `$_POST`) and it is not present. There will be an error, you just can't see it. See [How can I get useful error messages in PHP?](https://stackoverflow.com/questions/845021/how-can-i-get-useful-error-messages-in-php) – Phil Aug 05 '19 at 00:29

1 Answers1

0

Try using var_dump($_POST) to see if any of your POST data is there. Also, make sure you're AJAX request isn't being redirected to a different page (e.g. 301 redirect). The redirect will cause the POST data to be lost. You can observe your AJAX request via the Network window in your Developer Tools in Firefox/Chrome.

As for the database not updating, I would first verify that your POST data is being received correctly. Then you can look into what's going on with your database query (from which I suggest checking your PHP and/or MySQL error logs to help narrow down the issue)

Emma
  • 27,428
  • 11
  • 44
  • 69
Jarrett Barnett
  • 763
  • 3
  • 11