0

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: %s\n", 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 !

VPR
  • 165
  • 2
  • 9
  • 1
    You have a few queries here, which one is failing? What is the response from the server? What are the runtime values of the actual queries being executed? – David Mar 05 '19 at 18:46
  • 1
    There is error: `conn->query("UPDATE project_details` should start with `$`. Turn on error reporting. Also make sure you don't need to call `->execute()` on your queries – Justinas Mar 05 '19 at 18:51
  • See about sql injection and the importance of prepared and bound queries – Strawberry Mar 05 '19 at 19:02
  • Sorry about the $ typo.. its copying error...@Justinas. The update query is not running actually @David – VPR Mar 05 '19 at 19:13
  • @VishnuPrasad.R: How do you know it's not running? You never examine its result. It could be producing an error and you wouldn't know. Your code is wide open to SQL injection so you don't even know what query you're executing. Examine the runtime value of the query you're executing and test it directly. Also examine the result of the query and check for MySQL errors. – David Mar 05 '19 at 19:18
  • @david can you help me with the sql injection part... – VPR Mar 06 '19 at 00:55
  • @VishnuPrasad.R: https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php – David Mar 06 '19 at 01:29
  • @David I can see not update happening in the db even if values are passed correct. – VPR Mar 06 '19 at 06:28

3 Answers3

0

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

Reza Mamun
  • 5,991
  • 1
  • 43
  • 42
0

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.

  • In my personal experience, what I do when I send via ajax, is I first echo the data that I sent, if it doesn't show, then there's something wrong with the script. – Rafael Hernandez Mar 07 '19 at 19:55
0

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.

Batsheva Hansav
  • 316
  • 2
  • 11
  • PHP doesnt have problems with `` , i have other php files with same format running perfectly fine.. @Richard Socker. It seems to be something else, but I'll try what you suggested me and I'll update here.. – VPR Mar 06 '19 at 00:57
  • So what was the problem? – Batsheva Hansav Mar 07 '19 at 17:25