Fateslayer
Fateslayer

Reputation: 91

PHP not updating all data in database

I have a table which fetches data from a server and can be edited too. When I update a few data fields, it updates perfectly, but if I try to update lots of data fields together then most of the fields fails to update.

I think SQL is a lot slower than PHP and PHP is not waiting for the SQL query to finish which causes code to assume it didn't work and hence shows an error. I also tried sleep(), but even after sleep(2); it still misses some values if I make lots of changes. This is my jQuery code:

$(document).ready(function(){
    $("#edit").click(function(){
        $("td").attr('contenteditable', 'true');
    });
    var old_message;
    $("td").click(function(){
        old_message = $(this).text();
    });

    $("td").blur(function(){
        var new_message = $(this).text();
        if(old_message!=new_message)
        {
            $(this).addClass("changed");
            old_message = null;
            new_message = null;
        }
    });

    $("#submit").click(function(){
        $(".changed").each(function(){
            var row_num = $(this).siblings().eq(0).text();
            var col_name = $(this).attr("id");
            var value = $(this).text();
            var current = $(this);
            $.post("../php/update_table.php",
                {
                    id:row_num,
                    cn:col_name,
                    data:value
                },
                function(response,status){
                    if(response=="updated")
                    {
                        $(current).removeClass("changed");
                    }
                    else
                    {
                        alert("Failed");
                    }
                });
        });
        location.reload(true);
    });
});

This is my update.php:

<?php
    if(isset($_POST['id']) && isset($_POST['cn']) && isset($_POST['data']))
    {
        $id = $_POST['id'];
        $col_name = $_POST['cn'];
        $data = $_POST['data'];
        include "mysql_login.php";
        $mysql_database = "bus_database";
        $connect = mysqli_connect($host, $mysql_user, $mysql_pass, $mysql_database) or die("Couldn't Connect To The Database");
    $time = false;
    if($col_name=='a1_a1t' || $col_name=='a2_a2t' || $col_name=='d1_d1t' || $col_name=='d2_d2t')
    {
        $t_data = substr($data,0,-2);
        $tz_data = substr($data, 9);
        $t = substr($col_name,0, 2);
        $tz = substr($col_name, 3);
        $time = true;
    }
    if($time)
    {
        if($t_data == '' || $tz_data == '')
        {
            $t_data = "NULL";
            $tz_data = "NULL";
            $result = mysqli_query($connect, "UPDATE buses SET $t=$t_data, $tz=$tz_data WHERE id='$id'");
        }
        else
        {
            $result = mysqli_query($connect, "UPDATE buses SET $t='$t_data', $tz='$tz_data' WHERE id='$id'");
        }

        if($result)
        {
            echo("updated");
        }
        else
        {
            echo("failed");
        }
    }
    else if($col_name == 'route' || $col_name == 'bus_num')
    {
        $result = mysqli_query($connect, "UPDATE buses SET $col_name='$data' WHERE id='$id'");
        if($result)
        {
            echo("updated");
        }
        else
        {
            echo("failed");
        }
    }
    mysqli_close($connect);
    }
    else
    {
        echo("Failed");
    }
?>

Upvotes: 1

Views: 126

Answers (1)

Fateslayer
Fateslayer

Reputation: 91

I was using version 5 of XAAMP which caused the problem. Version 7 fixed everything.

Upvotes: 1

Related Questions