0

I have an array of numbers that i want to insert into one column as an string of numbers separated by comma(,) I have the below code for now-

    if (!($mains = $pdo->prepare("UPDATE `KB_SCHOOL_DETAILS` SET `AcessTo` = ? WHERE Docid = ?"  )))

   {
      DisplayErrMsg(sprintf("internal error %d:%s\n", $pdo->errorCode(), $pdo->errorInfo()));
      return 0;
    }
    $docid=$input->Docid;
    $principalid = $input->principalid;
      $principalids = implode(',', $principalid);
    $principalids = ''.$principalids.'';
  
    $mains->execute([$principalids,$docid]);
   echo $principalids;    
    
    echo $docid;;
    $arr=array("result"=>"done");

Input parameters are

["Docid":"MTEwMjQ1NjExOTkxNTE4NDEyODA/documents/NjgzMzMzODMyMjc5MDY0NTc2MA","principalid":["4705569","4705570","4705571","4705572","4705573","4706190","4706243"]}: 

When I run the query in the database, it is working. But when I pass the parameters to an Ajax call, it is not working. I'm new to programming please help me.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
DAG_noob
  • 1
  • 1
  • 1
    "Not working" isn't helpful, have you checked your browser's Network tab in the Developer toolbar? There you can click the AJAX request and see the response. It should show you the error if there is one. – El_Vanja Feb 03 '21 at 10:07
  • Side note: this line does nothing: `$principalids = ''.$principalids.'';` - implode already produces a string. – El_Vanja Feb 03 '21 at 10:08
  • Also, you say that running the query directly on the database works. Can you share the query you are using in that case? – El_Vanja Feb 03 '21 at 10:10
  • There is no error shown in networks. Input parameters mentioned is the headers and in response i get - '4705572,4705573'MTEwMjQ1NjExOTkxNTE4NDEyODA/documents/MTI5NzI4NzA1MTQ4MDM0NzQ0MzI{"result":"done"} – DAG_noob Feb 03 '21 at 10:11
  • If this is in an AJAX call and you're echoing `$principalids` and `$docid`, those should also be visible in the response under the Network tab. – El_Vanja Feb 03 '21 at 10:13
  • Database querry that is working- UPDATE `KB_SCHOOL_DETAILS` SET `AcessTo` = '342,432423,4324,432' WHERE Docid = 'MTEwMjQ1NjExOTkxNTE4NDEyODA/documents/MTI5NzI4NzA1MTQ4MDM0NzQ0MzI' – DAG_noob Feb 03 '21 at 10:14
  • Alright, that part seems ok. However, you're only checking for errors in preparation, but not execution. See this guide on [discovering the actual PDO error](https://stackoverflow.com/questions/32648371/my-pdo-statement-doesnt-work/32648423#32648423). – El_Vanja Feb 03 '21 at 10:21
  • Is there a way in my case to see what went wrong in execute statement? – DAG_noob Feb 03 '21 at 10:27
  • Have you read through that linked answer? It explains in general terms how to get the PDO error. – El_Vanja Feb 03 '21 at 10:30
  • Ya i have but did not understand much but I'm already using $dbh->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); – DAG_noob Feb 03 '21 at 11:03
  • So now when you make a request, if there's an error, you should see the exception in the response under the Network tab. – El_Vanja Feb 03 '21 at 11:05

0 Answers0