duandianwen1723 2013-12-08 03:20
浏览 22
已采纳

附加查询字符串 - PHP

I have a php variable

$query="insert into msnowplaying values";

Then, I want to append a query string

$q1="('1', '$selMovieTitle1', 'ST001', '1', '' )";
$query.=$q1;
echo "<script type='text/javascript'>alert('$query');</script>";

However, the alert shows nothing. Somehow, there's an error on the console:

[10:12:06.851] SyntaxError: missing ) after argument list @ http://localhost:1234/2013_12_2/controller/doaddNowPlaying.php:1

What am I missing? I've changed $q1 to " a" and the alert shows insert into msnowplaying values a

I don't know what happened, I really need some help. Thanks.

  • 写回答

2条回答 默认 最新

  • dongxia9620 2013-12-08 03:30
    关注

    Remember, PHP is parsed on the server, before the HTML hits the viewer, and javascript is parsed in the browser, after the HTML has been received by the user.

    What you are sending to the user is written by php into the data as if you wrote it by hand, including anything that may later be interpreted as javascript code.

    When you are echoing $query to the browser, you are most likely ending up with something that looks like this:

    <script type='text/javascript'>alert('('1', '$selMovieTitle1', 'ST001', '1', '' )');</script>
    

    Do you see how this is invalid javascript? You are using unescaped single quotes inside the alert('..'). First, if you want to see the correct string in javascript, try this:

    echo '<script type="text/javascript">alert("'.addslashes($query).'");</script>';
    

    Here we are using the function addslashes() ( http://php.net/addslashes ) to escape the quotes appropriately.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元