skip to Main Content

I have this piece of code where i am trying to update some values.
I can see that I am getting all the data passed from my ajax call which is…

$(document).on('click', '#commitbtn1', function() {
    var did = document.getElementById("chgelement").value;
    var pname = document.getElementById("projects").value;
    var elem = document.getElementById("elem").value;
    var respon = document.getElementById("respon").value;
    var coord = document.getElementById("coord").value;
    var total = document.getElementById("total").value;
    var submt = document.getElementById("submt").value;
    var delv = document.getElementById("delv").value;
    $.ajax({
        type:'POST',
        url:'update_details.php',
        data: {pname:pname,elem:elem,respon:respon,coord:coord,total:total,submt:submt,delv:delv,did:did},
        timeout: 30000,
        success:function(html){
            alert(html);
            alert("Updated !!");
            $('#prevrec').html(html);
        },
        error:function(error){
            console.error(error);
            alert(error);
        }
    });
});

which redirects or calls the php page ‘update_details.php’ via ajax,
I can see the alert(“Updated !!”).. However the query which i validated in phpmyadmin is not getting executed…
This is my file:

<?php
include 'connect.php';
if(isset($_POST['elem'])||isset($_POST['respon'])||isset($_POST['coord'])||isset($_POST['total'])||isset($_POST['submt'])||isset($_POST['delv'])){
$pname=$_POST['pname'];
$elem=$_POST['elem'];
$respon=$_POST['respon'];
$coord=$_POST['coord'];
$total=$_POST['total'];
$submt=$_POST['submt'];
$delv=$_POST['delv'];
$did=$_POST['did'];


if ( mysqli_connect_errno() ) {
printf( "Connect failed: %sn", mysqli_connect_error() );
exit();
}
$q1 = $conn->query("SELECT `project_id` FROM `projects` WHERE `project_name` = '$pname';");
$rowCount1 = $q1->num_rows;
if($rowCount1 > 0){
    while($row1 = $q1->fetch_assoc()){
        $pid = $row1['project_id'];
        $conn->query("UPDATE `project_details` SET `elements` = '$elem', `responsibilty` = '$respon', `coordinator` = '$coord', `total_level` = '$total', `sub_status` = '$submt', `del_status` = '$delv' WHERE `project_details`.`detail_id` = '$did';");
        $q2 = $conn->query("SELECT * FROM `project_details` WHERE `detail_id` = (SELECT MAX(`detail_id`) FROM `project_details`);");
        $rowCount2 = $q2->num_rows;
        if($rowCount2 > 0){
        echo'
        <div class="text-primary">Previous status row entered</div>
        <div class="row">
            <div class="col-11"><br>
                <table>
                  <thead>
                    <tr>
                      <th class=""></th>
                      <th class=""></th>
                      <th class=""></th>
                      <th class=""></th>
                      <th class=""></th>
                      <th class=""></th>                                      
                    </tr>
                  </thead>
                  <tbody>';
                  while($row = $q2->fetch_assoc()){
                    echo'
                    <tr>
                        <td><input type="text" class="prev_input" id="pre_elem" placeholder="" readonly value="'.$row["elements"].'"></td>
                        <td><input type="text" class="prev_input" id="pre_respon" placeholder="" readonly value="'.$row["responsibilty"].'"></td>
                        <td><input type="text" class="prev_input" id="pre_coord" placeholder="" readonly value="'.$row["coordinator"].'"></td>
                        <td><input type="text" class="prev_input" id="pre_total" placeholder="" readonly value="'.$row["total_level"].'"></td>
                        <td><input type="text" class="prev_input" id="pre_submt" placeholder="" readonly value="'.$row["sub_status"].'"></td>
                        <td><input type="text" class="prev_input" id="pre_delv" placeholder="" readonly value="'.$row["del_status"].'"></td>                                        
                    </tr>';
                  }
                    echo'
                  </tbody>
                </table>
            </div>
        </div>';
        }
    }
}
}
?>

I cant understand what I am doing wrong. Can anyone help?
Thanks in advance !

3

Answers


  1. You have missed $ sign before writing conn->query("UPDATE ... It will be $conn->query("UPDATE ...

    Login or Signup to reply.
  2. The alert shows because the php file loads successfully. Try the following:

    $conn->query("UPDATE `project_details` SET `elements` = '$elem', `responsibilty` = '$respon', `coordinator` = '$coord', `total_level` = '$total', `sub_status` = '$submt', `del_status` = '$delv' WHERE `project_details`.`detail_id` = '$did';") or die($con->error);
    

    To see if shows any errors in the query.

    Login or Signup to reply.
  3. This will work:

     $conn->query("UPDATE project_details SET elements = '$elem', responsibilty = '$respon', coordinator = '$coord', total_level = '$total', sub_status = '$submt', del_status = '$delv' WHERE project_details.detail_id = '$did';"); 
    

    Attention: you copied the query from phpmyadmin so you have “ signs that php doesn’t accept make sure you remove them.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search