在 sql 查询中用整个引号连接 php 值

concat php value with entire quotes in sql query

如何在 sql 查询中将此 php 值与整个引号连接起来,以便它在 phpmyadmin 数据库中正确保存?

 $db = new mysqli('localhost','root','','geo'); 

// Each value is a placeholder

$sql = "UPDATE file_contents SET Origin_URL = CONCAT('https://www.google.com/maps/dir/', ?, ',' ?, '/', ?, ',', ?) WHERE Sl = 1 LIMIT 6";

$stmt = $db ->prepare($sql);

// First parameter should correspond to number and types of your arguments
// You have 5, first four are strings, fifth is a number, so "ssssd"

$stmt->bind_param($OriginLatId,$OriginLongId,$DestinationLatId,$DestinationLongId);

$stmt->execute();

请帮助我获得正确的 sql 查询,以便在我的数据库中成功插入此 url,这是 table,并且我已经制作了 Origin_URL 列进入 varchar 列。数据进入此列。

永远不要在 PHP 或任何其他支持绑定语句的语言(几乎是任何现代语言)中连接 SQL 查询。

要使用绑定语句,您首先需要准备它:

// Each value is a placeholder
$sql = "UPDATE file_contents SET Origin_URL = CONCAT('https://www.google.com/maps/dir/', ?, ',' ?, '/', ?, ',', ?) WHERE Sl = ?";

$stmt = $db->prepare($sql);

// First parameter should correspond to number and types of your arguments
// You have 5, first four are strings, fifth is a number, so "ssssd"
$stmt->bind_param('ssssd', $OriginLatId, $OriginLongId, $DestinationLatId, $DestinationLongId, $id);
$stmt->execute();

我建议你使用 PDO

    <?php
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
?>

建立连接后您可以准备声明

    <?php
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (:name, :value)");
$stmt->bindParam(':name', $name);
$stmt->bindParam(':value', $value);

// insert one row
$name = 'one';
$value = 1;
$stmt->execute();

// insert another row with different values
$name = 'two';
$value = 2;
$stmt->execute();
?>

期待这对你有所帮助

你的字符串连接有很多问题,+是为了添加,单引号中的变量是字符串,不是变量,你似乎在太多情况下添加了引号。

您应该能够使用双引号中的复杂大括号构建您的字符串:

$val1 = "https://www.google.com/maps/dir/{$OriginLatId},{$OriginLongId}/{$DestinationLatId},{$DestinationLongId}";

您可以在此处阅读更多相关信息,http://php.net/manual/en/language.types.string.php

或通过标准连接:

$val1 = 'https://www.google.com/maps/dir/' .  $OriginLatId .',' . $OriginLongId . '/' . $DestinationLatId . ',' . $DestinationLongId;

您可以在此处阅读更多相关信息,http://php.net/manual/en/language.operators.string.php

然后将其写入数据库。不需要 mysql concat 函数。

$sql = 'UPDATE file_contents 
SET Origin_URL = ? 
WHERE Sl = 1 LIMIT 6';
$stmt = $db->prepare($sql);
$stmt->bind_param('s', $val1);
$stmt->execute();