gpt4 book ai didi

php - 将长字符串从 iOS 传递到 MySQL 数据库

转载 作者:太空宇宙 更新时间:2023-11-03 11:50:36 26 4
gpt4 key购买 nike

我可以从我的 iOS 应用程序发布到我的 MySQL 数据库,但是当我有长句子时

我正在尝试发布此字符串:"Hello, how are you today?"

它不会做任何事情。所以我即兴创作并用 _ 替换了空格,我的字符串看起来像这样 "Hello,_how_are_you_today?" 和成功了!

但我认为我必须这样做很奇怪。

所以我的问题是,如何发布带有空格的长字符串而不像我那样修改它?

这是我的代码

iOS

NSString *strURL = [NSString stringWithFormat:@"http://www.example.com/post.php?dishname=%@&description=%@, dishname.text, description.text]";
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(@"%@", strResult);

php代码

<?php

// Create connection
$servername = "localhost";
$username = "admin";
$password = "root";
$dbname = "dbname";
$con=mysqli_connect("localhost","admin","root","dbname");

if (!$con) {
die("Connection failed: " . mysqli_connect_error());
echo "Nothing happened";

}else{


}


if (isset ($_GET["dishname"]))
$dishname = $_GET["dishname"];

else
$dishname = "Null";
if (isset ($_GET["description"]))
$description = $_GET["description"];
else
$description = "Null";

echo "dishname : ". $dishname;
echo "description : ". $description;

$sql = "insert into RecipeFeed (DishName, Description) values ('".$dishname."','".$description."')";
$result = mysqli_query($con, $sql);
?>

最佳答案

如果使用面向对象的接口(interface),代码会更简洁:

$con = new mysqli("localhost","admin","root","dbname");

if ($mysqli->connect_errno) {
die("Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error);
// Note, nothing happens after die inside the same block, so
// don't put code here. It will never, ever run.
}

那里还有很多死代码,它们什么都不做,但已被删除。

使用 prepared statements使您的语句易于阅读,并在您注意不使用字符串插值的情况下避免 SQL 注入(inject)问题:

$stmt = $mysqli->prepare("INSERT INTO RecipeFeed (DishName, Description) VALUES (?, ?)");

$stmt->bind_param('ss', $_GET['description'], $_GET['dishname']);

$result = $stmt->execute();

这将为您处理所有报价,作为奖励,您不必担心它们是否已设置。

关于php - 将长字符串从 iOS 传递到 MySQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35711990/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com