- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
你好,我正在做我的大学工作,我们正在使用curl和php,我已经制作了一个脚本。该脚本从html表单中检索一个id,然后使用curl将其传递给php脚本来更新数据库,这一切都工作正常,但是在我的curl脚本中,我从php页面读取了回显的响应
OK
更新数据库后,我的 if 语句无法读取 OK,我做错了什么?正如我所说,代码工作正常,只是在读取响应后没有在 if 语句中显示正确的消息,这是如果我尝试的话,curl 脚本会显示的内容,数据库再次更新良好等,只是没有正确读取响应正如您在代码中看到的,我回显响应来测试它,它显示“确定”,“确定”之前有一个空格,所以我尝试了“确定”和“确定”,然后再次尝试了一个空格,但都无法将其与响应匹配。
error
test responce = OK
代码。
curl 代码
<?php
$id = $_POST["id"];
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, "http://southamptonstudentrooms.com/Wad/downloadwebservice3.php");
$dataToPost = array ("id" => $id);
curl_setopt($connection,CURLOPT_RETURNTRANSFER,1);
curl_setopt($connection,CURLOPT_POSTFIELDS,$dataToPost);
$response = curl_exec($connection);
// display any errors
if(curl_errno($connection)){
echo 'Curl error: ' . curl_error($ch);
}
curl_close($connection);
if ($response == " OK") {
echo "updated";
}
else if ($response == " ERROR") {
echo "updated failed";
}
else {
echo "error";
}
echo "<br><br>test response =" . $response; // displays " OK"
var_dump($response); // displays: string(4) " OK"
?>
为了安全起见,它访问的 PhP 脚本 - 要连接的用户信息已更改
<?php
$servername = "localhost";
$username = "south609_edwin";
$password = "Zx10r2006";
$dbname = "south609_test_database";
$ID = $_POST["id"];
$status;
// 11: Chris Palmer, username ChrisPalmer, Password tree987, Balance 2.00
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT * FROM ht_users WHERE username='ChrisPalmer'";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
if($row["balance"] >= 0.79)
{
mysqli_close($conn);
try {
//WAI
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql = "UPDATE wadsongs SET downloads=downloads + 1 WHERE songid='$ID'";
// Prepare statement
$stmt = $conn->prepare($sql);
// execute the query
$stmt->execute();
$status = "OK";
// echo a message to say the UPDATE succeeded
// echo $stmt->rowCount() . " records UPDATED successfully";
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage();
}
$conn = null;
}
else{
$status = "ERROR";
}
}
}
else {
echo "0 results";
mysqli_close($conn);
}
echo $status;
?>
最佳答案
您遇到的问题是您得到的响应有一个与“OK”不匹配的空格,因此您可以借助 PHP 中的 trim() 函数修剪空格
所以现在你的代码将变成
if(trim($response) == "OK"){
//Your success code
}
关于php Curl 无法在 if 语句中正确读取响应,回显正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46570910/
"); ..... 回显 (""); ?> 是什么意思?
问题是标签 我看过我正在编写的一个脚本,并使用了它: echo (""); ........ echo (""); 它到底做了什么? 它是 Html 标签还是 PHP? 我在 Google 上进行了
我正在创建一个结帐系统。我分为三个部分: 送货信息 付款信息 订单确认 我正在尝试找出一种方法,当客户输入他们的送货信息时,该数据可以回显到我的订单确认部分,这样他们就可以确认这是他们想要将其运送到的
我正在 MYSQL 表上运行 SELECT 代码 我有一个 MYSQL 表: CREATE TABLE mytable ( wf_id int(11) UNSIGNED NOT NULL, sessi
我想要做的是回显页面顶部的内容,同时捕捉页面底部的内容。 这是我的脚本: Westpop Wachtwoord moet minstens 8 tekens lang
我在将选择列表放在 HTML 中时遇到了一些困难,其中的选项或值是从数据库中的表中检索的。我得到以下结果: 如您所见,这不是我的本意,我宁愿将所有选项都放在 1 个选择列表中,并且可以单击多个选项。
我的代码有问题。我有这段代码: Title Welcome loggedin:
我需要使用 PHP 仅回显 .txt 文件中的选定行。txt 文件内容如下所示: 1 111 111 111 111 111 111 111 111 111 111 2 196 182 227 190
我需要使用 PHP 仅回显 .txt 文件中的选定行。txt 文件内容如下所示: 1 111 111 111 111 111 111 111 111 111 111 2 196 182 227 190
我想从 PHP 回显一些 HTML,其中包含一个链接,上面有文本“ ' '; 最佳答案 如果您想显示文字小于号,请根据 HTML 规范将其编码为 <。 您可能还想查看另一个相关实体:«。这会产生一个
如果我从名为 categories 的 mysql 表中调用链接列表,我想我使用的选择语句如下: $query = "SELECT cat_name, cat_description FROM c
所以,伙计们,我的问题是我正在从一个 mysql 列创建一个数组,但是,当我回显数组项时,它什么也没返回,我有一段时间没有看到一个可能的错误,希望能得到一些帮助。这是我的代码:(我知道 mysql 到
在下面的代码中,我在 php 文件中回显了 $strXML,它显示了整个 $strXML,但我只想在 javascript 中显示“名称”元素值。谁能帮帮我? PHP: $strXML = ''."\
我有一节课用这段代码 public boolean busybox() throws IOException { try { Process p =Ru
我有一个 SQL 查询,出于安全目的,我最近将其转换为准备好的语句。我有一个查询返回许多行,每行由许多列组成。我的问题是如何使用 while 循环回显结果。到目前为止我的例子: $stmt = $co
我尝试这样的事情: function userrank($userid){ $sql = mysql_query("SELECT * FROM users ORDER BY atacs DES
我正在尝试进行选择查询并将此信息输出到 html 标签中,但是它不断将 php 代码转换为: 我做错了什么?为什么它不返回错误而不是将其转换为上面的错误? 代码 $con = mysqli_conn
抱歉含糊其辞,但我有一个 PHP 脚本,它从数据库中提取数据并显示它,但是它不是输出数据库的内容,而是输出“数组”的次数。 (无法再次工作,脚本已上传 here ),脚本的输出也是 here .)谢谢
我有一个工作代码并将其移动到一个新服务器,从 php5 到 php7。谷歌搜索一周没有帮助我,所以我问你们。 我在 sql 数据库中有一些带有欧元符号的信息: 99.00该信息存储为 utf16-ge
这是我的代码: 0){ $info = mysql_fetch_assoc($result); while($row = mysql_fetch_assoc($result)){
我在查找有关此主题的信息时遇到了一些真正的困难,如果您有任何帮助,我将非常感激。简而言之,我有一个表单,用户可以从下拉列表中选择一个类别,输入一些内容,然后点击提交,然后转到 SQL。下拉列表中的每个
我是一名优秀的程序员,十分优秀!