doqo89924 2010-10-26 19:13
浏览 125

sprintf()与mysql_query()

Having trouble formatting my code to execute without error using sprintf() When I run the code I get this error: Parse error: syntax error, unexpected T_VARIABLE in /location on line 16

 $query = sprintf('UPDATE `%s` SET `stock` = :amount WHERE `itemname` = '$q'', $tablename);

Above is line 16 in my code. I'm assuming it is syntax related.

I am now receiving the following error:

Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1065 Query was empty' in /home/content/63/6563663/html/inventory/pg.php:19 Stack trace: #0 /home/content/63/6563663/html/inventory/pg.php(19): PDOStatement->execute() #1 {main} thrown in /home/content/63/6563663/html/inventory/pg.php on line 19

This is my entire code block:

 <?php

 $u=$_GET["u"];

 if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form2")) {
 $amount = isset($_POST['amount']) ? $_POST['amount'] : null;
 if (null != $amount) {

 $user = 'username';
 $pass = 'password';
 $pdo = new PDO('mysql:localhost', $user, $pass);
 $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 session_start();
 $tablename = $_SESSION['MM_Username'];
 $query = sprintf('UPDATE %s SET stock= :amount WHERE itemname= '.$u, $tablename);
 $stmt = $pdo->prepare($UpdateQuery);
 $stmt->bindParam('amount', $amount);
 $stmt->execute();
 }
 }

 ?>

Thank you, seems my error is dealing with the PDO execution, not the query itself. If anyone has any ideas on that it would be great. Alan, I used your method for the quotes and also am running the query through mysql_real_escape_string().

  • 写回答

3条回答 默认 最新

  • drflkphi675447 2010-10-26 19:16
    关注

    Try this:

    $query = sprintf('UPDATE%sSETstock= :amount WHEREitemname= '.$q, $tablename);

    You need to put a concatenation operator between strings and variables to combine them together. You also can get rid of the '' after $q because it is not changing the string at all.

    Edit:

    I believe I misread what you are trying to do. Try this instead:

    $query = sprintf("UPDATE%sSETstock= :amount WHEREitemname= '$q'", $tablename);

    By changing your PHP string to be within double quotes, you do not need to escape your single quotes, and $q will be expanded to its value.

    Also, be sure that you run $q and $tablename through mysql_real_escape_string() to prevent SQL injection.

    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码