0

I have this function to read html contents

function patient_file(){
$html= '';
$html .= '<table style="width:100%; padding:0; margin:0; color:rgb(50,50,50); ">
<tr><td style="width:17%"><b style="color:rgb(150,150,150)"> Fullname</b> </td><td>  salum said juma</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> Age / sex</b> </td><td>  12 male </td></tr>
<tr><td><b style="color:rgb(150,150,150)"> Address</b> </td><td>  kariakoo dsm</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> File number </b> </td><td>  mn234</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> kitu package </b> </td><td>  scheme name</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> kitu number </b> </td><td>  1234567890</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> kiyutu</b> </td><td>  1919181716151</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> hihi in </b> </td><td> 12-03-2023</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> hihi out</b> </td><td>   12-03-2023  </td></tr>';
$html .= '</table>';

return $html;

}

then after i call the fuction and create json encode

$folio=$_POST['folio'];
$sqlRun = mysqli_query($conn2, "SELECT *  FROM m_folio where m_No='$folio'");
$row = mysqli_fetch_assoc($sqlRun);
$row['cardata'] = patient_file();
$json_array[] = $row;
$jasondata = json_encode($json_array);
 $payload = '{"entities": ' . $jasondata . '}';
echo $payload;

now the payload generated give error on jsonformatter this is my output

{"entities": [{"cardata":"\r\n
Fullname<\/b> <\/td>    salum said juma<\/td><\/tr>\r\n
Age \/ sex<\/b> <\/td>  12 male <\/td><\/tr>\r\n
Address<\/b> <\/td> kariakoo dsm<\/td><\/tr>\r\n
File number <\/b> <\/td>    mn234<\/td><\/tr>\r\n
kitu package <\/b> <\/td>   scheme name<\/td><\/tr>\r\n
kitu number <\/b> <\/td>    1234567890<\/td><\/tr>\r\n
kiyutu<\/b> <\/td>  1919181716151<\/td><\/tr>\r\n
hihi in <\/b> <\/td>    12-03-2023<\/td><\/tr>\r\n
hihi out<\/b> <\/td>    12-03-2023\t<\/td><\/tr><\/table>"}]} 
Salum Said
  • 11
  • 5
  • 1
    What error is being thrown? – Daniel Black Mar 31 '23 at 19:12
  • You shouldn't have any real newlines in the middle of your html in the json. `\r\n` is fine, but the line break after it is breaking the format – aynber Mar 31 '23 at 19:15
  • @DanielBlack invalid character found at position 69 – Salum Said Mar 31 '23 at 19:17
  • @aynber those came automatically when json encode i did not create them – Salum Said Mar 31 '23 at 19:19
  • 1
    No, json_encode would not put those there. See https://3v4l.org/38Qft. There's may be something else causing the line breaks – aynber Mar 31 '23 at 19:20
  • 1
    **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Mar 31 '23 at 19:21
  • @aynber that actually what i want my output to be but what i get is not that – Salum Said Apr 01 '23 at 04:55

2 Answers2

1

Don't create $payload using string operations. Use json_encode().

$folio=$_POST['folio'];
$stmt = mysqli_prepare($conn, "SELECT * FROM m_folio where m_No = ?"
mysqli_stmt_bind_param($stmt, "i", $folio);
mysqli_stmt_execute($stmt);
$sqlRun = mysqli_stmt_get_result();
$row = mysqli_fetch_assoc($sqlRun);
$row['cardata'] = patient_file();
$json_array[] = $row;
$payload = json_encode(["entities" => $json_array])
echo $payload;

I've also changed the mysqli code to use a prepared statement to prevent SQL injection.

Barmar
  • 741,623
  • 53
  • 500
  • 612
-1

Remove all line breaks from $html in patient_file() before returning.

function patient_file(){

$html= '';
$html .= '<table style="width:100%; padding:0; margin:0; color:rgb(50,50,50); ">
<tr><td style="width:17%"><b style="color:rgb(150,150,150)"> Fullname</b> </td><td>  salum said juma</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> Age / sex</b> </td><td>  12 male </td></tr>
<tr><td><b style="color:rgb(150,150,150)"> Address</b> </td><td>  kariakoo dsm</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> File number </b> </td><td>  mn234</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> kitu package </b> </td><td>  scheme name</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> kitu number </b> </td><td>  1234567890</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> kiyutu</b> </td><td>  1919181716151</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> hihi in </b> </td><td> 12-03-2023</td></tr>
<tr><td><b style="color:rgb(150,150,150)"> hihi out</b> </td><td>   12-03-2023  </td></tr>';
$html .= '</table>';

/** remove all line breaks **/
$string = trim(preg_replace('/\s+/', ' ', $html));

return $string;

}

This will turn the HTML into one long string which javascript friendly.

ibrahim s
  • 297
  • 1
  • 7