Hello i am doing my uni work and we are working with curl and php, i have made a script as told. the script retrieves an id from a html form then using curl pass it to a php script to update a database, this all works fine, But in my curl script i read the response from the php page which echos
OK
after updating the database, and my if statement fails to read the OK, What have i done wrong ? as i said the code works fine it just does not display the right message in the if statement after reading the response, Here's what the curl script displays if i try it, again the database is updated fine ect, just not reading the response properly however as you see in the code i echo the response to test it and it displays OK, with a space before the OK so i tried " OK" and 'OK' and again with a space but all fail to match it to the response.
error
test responce = OK
the code.
Curl code
<?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 script that it access -user info to connect changed for security
<?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;
?>