gpt4 book ai didi

php - 如何制作包含引号等字符的 Web 服务器 URL?

转载 作者:行者123 更新时间:2023-11-29 23:30:52 24 4
gpt4 key购买 nike

我的 iOS 应用程序中有这行代码尝试将数据插入我的 mysql 数据库:

NSString *strURL = [NSString stringWithFormat:@"http://example.org/postSqueal.php?thePost=%@&byUserID=%@&nickname=%@", self.textView.text, [UniqueUserIdentification getUserID], nickname];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

基本上,我让用户在 UITextView 中输入一堆内容(例如 Twitter 推文),当他们按下“发布”按钮时,它应该存储这些数据在我的数据库中以及一些其他信息,例如他们的昵称。我的代码工作得很好,直到我开始输入空格。那么我的代码将不会插入到数据库中。我发现这是因为它意味着网址中的空格不是有效的网址,因此我得出的结论是,我需要以某种方式在网址中用引号将 TextView 文本引起来。我尝试添加引号,如下所示:

NSString *strURL = [NSString stringWithFormat:@"http://example.org/postSqueal.php?thePost=\"%@\"&byUserID=%@&nickname=%@", self.textView.text, [UniqueUserIdentification getUserID], nickname];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];

但是它仍然不起作用。有人知道可能出了什么问题吗?当我手动输入带有空格的文本周围带引号的网址时,它可以工作,但在我的代码中,当我尝试添加这样的引号时,插入不起作用。还有其他字符我可能需要担心在将其转换为网址时给我带来麻烦吗?

这是我的 php 脚本:

<?php

$con=mysqli_connect("editout","editout","editout","editout");

if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$thePost = $_GET["thePost"];
$byUserID = $_GET["byUserID"];
$nickname = $_GET["nickname"];

mysqli_query($con,"INSERT INTO myTable
VALUES ('$thePost', '$byUserID', '$nickname', 0, 0)");

mysqli_close($con);

?>

最佳答案

URL encode查询参数。请参阅this question有关 Objective-C 的更多信息。 urlencode是 PHP 中对字符串进行编码的函数。

关于php - 如何制作包含引号等字符的 Web 服务器 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26585323/

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