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.

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题