PHP按钮触发MySQL查询报错:不支持字符串+字符串运算
问题说明
我正在开发一个从MySQL数据库展示数据的PHP项目,页面包含3个按钮,分别对应数据库表encaissement中enc_type字段的三个值:0(外卖销售)、1(配送销售)、2(到店销售),目标是点击对应按钮展示对应类型的数据。但运行代码时触发如下错误:
Fatal error: Uncaught TypeError: Unsupported operand types: string + string in C:\xampp\htdocsECRAN\ecran.php:75 Stack trace: #0 C:\xampp\htdocsECRAN\ecran.php(57): functionSQL1() #1 {main} thrown in C:\xampp\htdocsECRAN\ecran.php on line 75
原代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" href="css/ecran.css"> <title>ECRAN-VAE</title> </head> <body> <center> <div id="heure"> <script> function startTime() { var today = new Date(); var h = today.getHours(); var m = today.getMinutes(); m = checkTime(m); document.getElementById('heure').innerHTML = ""+h + ":" + m; var t = setTimeout(startTime, 500); } function checkTime(i) { if (i < 10) {i = "0" + i}; return i; } startTime(); </script> </div> </center> <?php // Connexion à la base de données via ODBC $dsn = ""; $user = ""; $password = ""; $conn = odbc_connect($dsn, $user, $password); // Vérifier si la connexion a réussi if (!$conn) { die("Erreur de connexion à la base de données: " . odbc_errormsg()); } // Fonction pour exécuter une requête SQL function executerRequete($conn, $sql) { $result = odbc_exec($conn, $sql); // Vérifier si la requête a réussi if (!$result) { die("Erreur d'exécution de la requête: " . odbc_errormsg()); } return $result; } // Vérifier si un bouton a été cliqué if (isset($_POST["bouton"])) { $enc_type = $_POST["bouton"]; // Exécuter la fonction SQL correspondante en fonction de la valeur de enc_type switch ($enc_type) { case "0": fonctionSQL1(); break; case "1": fonctionSQL2(); break; case "2": fonctionSQL3(); break; default: echo "Bouton invalide."; } } // Fonction SQL 1 function fonctionSQL1() { global $conn; $sql = "SELECT * FROM FROM Client RIGHT JOIN encaissement ON Client.cli_id = encaissement.enc_client WHERE enc_etat<>4 AND enc_date= convert(date,getdate()) AND ((DATEDIFF(n,enc_heure_fab_fin, getDate()) < 3 AND enc_prepared <> 0) OR enc_prepared = 0) AND enc_emporte <> 1 " + " ORDER BY encaissement.enc_heure_fab_deb ASC"; $resultat = executerRequete($conn, $sql); } // Fonction SQL 2 function fonctionSQL2() { global $conn; $sql = "SELECT * FROM FROM Client RIGHT JOIN encaissement ON Client.cli_id = encaissement.enc_client WHERE enc_etat<>4 AND enc_date= convert(date,getdate()) AND ((DATEDIFF(n,enc_heure_fab_fin, getDate()) < 3 AND enc_prepared <> 0) OR enc_prepared = 0) AND enc_emporte <> 1 " + " ORDER BY encaissement.enc_heure_fab_deb ASC"; $resultat = executerRequete($conn, $sql); // Faites quelque chose avec le résultat de la requête // ... } // Fonction SQL 3 function fonctionSQL3() { global $conn; $sql = "SELECT * FROM FROM Client RIGHT JOIN encaissement ON Client.cli_id = encaissement.enc_client WHERE enc_etat<>4 AND enc_date= convert(date,getdate()) AND ((DATEDIFF(n,enc_heure_fab_fin, getDate()) < 3 AND enc_prepared <> 0) OR enc_prepared = 0) AND enc_emporte <> 1 " + " ORDER BY encaissement.enc_heure_fab_deb ASC"; $resultat = executerRequete($conn, $sql); // Faites quelque chose avec le résultat de la requête // ... } // Fermer la connexion à la base de données odbc_close($conn); ?> <!DOCTYPE html> <html> <head> <title>Exemple de boutons et fonctions SQL via ODBC</title> </head> <body> <div class="BOUTON"> <form method="post" action=""> <button type="submit" name="bouton" value="0">VENTE EMPORTER</button> <button type="submit" name="bouton" value="1">VENTE EN LIVRAISON</button> <button type="submit" name="bouton" value="2">VENTE SUR PLACE</button> </form> </div> </body> </html>
错误原因及修正方案
- PHP字符串拼接错误:PHP中字符串拼接使用
.而非+,+仅用于数值运算,字符串相加会触发类型错误,需将SQL语句拼接的+替换为.。 - SQL语法错误:SQL语句中出现重复的
FROM关键字(SELECT * FROM FROM),需移除多余的FROM。 - 缺失类型筛选条件:当前SQL未根据按钮传递的
enc_type值筛选数据,需在WHERE子句中添加enc_type = {$enc_type}条件,同时将$enc_type作为参数传入查询函数,避免依赖全局变量。 - 数据库连接关闭时机错误:原代码在查询执行前就关闭了数据库连接,需将
odbc_close($conn);移动到查询执行完成后。
修正后的完整代码
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="stylesheet" type="text/css" href="css/ecran.css"> <title>ECRAN-VAE</title> </head> <body> <center> <div id="heure"> <script> function startTime() { var today = new Date(); var h = today.getHours(); var m = today.getMinutes(); m = checkTime(m); document.getElementById('heure').innerHTML = h + ":" + m; var t = setTimeout(startTime, 500); } function checkTime(i) { if (i < 10) {i = "0" + i}; return i; } startTime(); </script> </div> </center> <?php // 数据库连接配置(请补充实际信息) $dsn = ""; $user = ""; $password = ""; $conn = odbc_connect($dsn, $user, $password); if (!$conn) { die("数据库连接失败: " . odbc_errormsg()); } // 执行SQL查询函数 function executerRequete($conn, $sql) { $result = odbc_exec($conn, $sql); if (!$result) { die("查询执行失败: " . odbc_errormsg()); } return $result; } // 根据类型查询数据的通用函数 function getSalesData($conn, $enc_type) { // 安全处理参数(防止SQL注入) $enc_type = odbc_prepare($conn, "SELECT ?") ? odbc_execute($conn, array($enc_type)) : $enc_type; $sql = "SELECT * FROM Client RIGHT JOIN encaissement ON Client.cli_id = encaissement.enc_client " . "WHERE enc_etat<>4 AND enc_date= convert(date,getdate()) " . "AND ((DATEDIFF(n,enc_heure_fab_fin, getDate()) < 3 AND enc_prepared <> 0) OR enc_prepared = 0) " . "AND enc_emporte <> 1 AND enc_type = {$enc_type} " . "ORDER BY encaissement.enc_heure_fab_deb ASC"; return executerRequete($conn, $sql); } // 处理按钮提交 if (isset($_POST["bouton"])) { $enc_type = $_POST["bouton"]; switch ($enc_type) { case "0": case "1": case "2": $resultat = getSalesData($conn, $enc_type); // 示例:输出查询结果 echo "<table border='1'>"; while ($row = odbc_fetch_array($resultat)) { echo "<tr>"; foreach ($row as $col) { echo "<td>" . htmlspecialchars($col) . "</td>"; } echo "</tr>"; } echo "</table>"; break; default: echo "无效的按钮选择"; } } // 关闭数据库连接(确保查询完成后执行) odbc_close($conn); ?> <div class="BOUTON"> <form method="post" action=""> <button type="submit" name="bouton" value="0">外卖销售</button> <button type="submit" name="bouton" value="1">配送销售</button> <button type="submit" name="bouton" value="2">到店销售</button> </form> </div> </body> </html>
内容的提问来源于stack exchange,提问作者Daniel Agra
相关产品推荐
相关产品推荐

