简体   繁体   中英

PHP echo is Cutting off the Last String in Concatenation

My PHP code returns all of concatenated string except for the last string in the concatenation. How do I get the '");</script>' to echo as well?

PHP:

<?php
$server = mysqli_connect("111.111.111.111", "myname", "mypassword");

$query = "SELECT * FROM masks.maskPrimary";
$results = mysqli_query($server, $query);

$row = mysqli_fetch_array($results);
$echoer = '<script>let obj1 = new product("'.$row[2].'", "'.$row[3].'", "'.$row[4].'", "'.$row[5].'", "'.$row[1].'", "'.$row[6].'");</script>';

echo $echoer;

echo output:

<script>let obj1 = new product("Bigox Face Mask Disposable Earloop Blue 50Pcs", "$19.99", "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAwMDQsNCxAODBANEA4QExYRDRASGR8dFhsVHhgYEx4YFRsVFBwYGyAZHhsjKyQpIyA6LCYxGSYoRC5FOUsyLkIBCA4NDhITDhERExMREhYTJxsSES4cH", "paper", "amazon", "https://amazon.com/gp/slredirect/picassoRedirect.html/ref=pa_sp_atf_aps_sr_pg1_1?ie=UTF8&adId=A07729502ZDC3IUM61Q4B&url=%2FBigox-Face-Disposable-Earloop-50Pcs%2Fdp%2FB087RRWGJB%2Fref%3Dsr_1_1_sspa%3Fdchild%3D1%26keywords%3Dpaper%2Bmasks%26qid%3D1595873951%26sr%3D8-1-spons%26psc%3D1&qualifier=1595873951&id=2851535447816864&widgetName=sp_atf

Use json_encode to put the values into the javascript. It will then take care of all the proper variable formatting for the javascript to handle. Change only this line to such:

$echoer = '<script>let obj1 = new product('. 
                                 json_encode($row[2]) .','.
                                 json_encode($row[3]) .','. 
                                 json_encode($row[4]) .','.
                                 json_encode($row[5]) .','.
                                 json_encode($row[1]) .','.
                                 json_encode($row[6]) .
          ');</script>';

It works well for this purpose. Even though JSON stands for OBJECT NOTATION, the PHP function works well with simple string output as well.

The alternative is to come up with a proper string escaper that makes all your row column outputs safe for javascript.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM