基于whatsapp的向导无法更新正确的字段

时间:2018-11-14 10:51:24

标签: php whatsapi

我有一个向导,可以根据twilio文档使用beta版的whatsapp api收集信息。我可以得到它来发送和接收来自用户的信息。我现在想用它来填充名为travel的mysql表中的适当字段,但是我不明白为什么它不能适当地填充字段。这是我正在使用的脚本

$twilio = new Client($sid, $token);

$number = $_POST['From'];
$body = $_POST['Body'];


$servername = "localhost";
$username = "root";
$password = "123456";
$dbname = "twilio";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}


$query = "select * from travel where telephone_number = '$number' AND msg = 'UNDONE'";
$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);

$rc = $result->num_rows;
$t = time();

$telephone_number = $row['telephone_number'];
$q1_field = $row['where_are_u_travelling_to'];
$q2_field = $row['where_are_you_travelling_from'];
$q3_field = $row['are_you_ready_to_go_on_next_bus'];
$q4_field = $row['how_many_people'];
$msg = $row['msg'];

if($rc == 0){
    $sql = "INSERT INTO travel (telephone_number,last_updated,msg)
            VALUES ('$number','$t','UNDONE')";

    if ($conn->query($sql) === TRUE) {
        echo "New record created successfully";
    }

    $message = $twilio->messages
                  ->create("$number",
                           array(
                               "body" => "Welcome To The Booking System.Where are you travellig to?",
                               "from" => "whatsapp:+14"
                           )
                  );


    print($message->sid);

}

//First
if(empty($q1_field) &&  empty($q2_field) && empty($q3_field) && empty($q4_field) && $msg == "UNDONE"){

    $sql_01 = "UPDATE travel SET
                    where_are_u_travelling_to='$body',
                    last_updated='$t' 
                WHERE telephone_number='$number'";

    if ($conn->query($sql_01) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }

    $message = $twilio->messages
                  ->create("$number",
                           array(
                               "body" => "Where are you travelling from?",
                               "from" => "whatsapp:+14"
                           )
                  );


    print($message->sid);
    exit();
}

//Second
if(!empty($q1_field) &&  empty($q2_field) && empty($q3_field) && empty($q4_field) && $msg == "UNDONE"){

    $sql_02 = "UPDATE travel SET    
                where_are_u_travelling_from='$body',
                last_updated='$t' 
                WHERE telephone_number='$number'";

    if ($conn->query($sql_02) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }


    $message = $twilio->messages
                  ->create("$number",
                           array(
                               "body" => "Are you ready to go on the next bus?",
                               "from" => "whatsapp:+14"
                           )
                  );


    print($message->sid);
    exit();
}

//Third
if(!empty($q1_field) &&  !empty($q2_field) && empty($q3_field) && empty($q4_field) && $msg == "UNDONE"){
    $sql_03 = "UPDATE travel SET 
                are_you_ready_to_go_on_next_bus='$body',
                last_updated='$t' 
                WHERE telephone_number='$number'";

    if ($conn->query($sql_03) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }
    $message = $twilio->messages
                  ->create("$number",
                           array(
                               "body" => "How many people are travelling?.",
                               "from" => "whatsapp:+14"
                           )
                  );


    print($message->sid);
    exit();
}

//Fourth
if(!empty($q1_field) &&  !empty($q2_field) && !empty($q3_field) && empty($q4_field) && $msg == "UNDONE"){
    $sql_04 = "UPDATE travel SET 
                    how_many_people='$body',
                    last_updated='$t',msg='DONE' 
                WHERE telephone_number='$number'";

    if ($conn->query($sql_04) === TRUE) {
        echo "Record updated successfully";
    } else {
        echo "Error updating record: " . $conn->error;
    }
    $message = $twilio->messages
                  ->create("$number",
                           array(
                               "body" => "Thank you for travelling with us.Yur ticket has been booked.",
                               "from" => "whatsapp:+14"
                           )
                  );


    print($message->sid);
    exit();
}

这是mysql模式

CREATE TABLE `travel` (
    `id` INT(11) NOT NULL AUTO_INCREMENT,
    `telephone_number` VARCHAR(50) NULL DEFAULT NULL,
    `where_are_u_travelling_to` VARCHAR(50) NULL DEFAULT NULL,
    `where_are_you_travelling_from` VARCHAR(50) NULL DEFAULT NULL,
    `are_you_ready_to_go_on_next_bus` VARCHAR(50) NULL DEFAULT NULL,
    `how_many_people` VARCHAR(50) NULL DEFAULT NULL,
    `msg` VARCHAR(50) NULL DEFAULT NULL,
    `last_updated` VARCHAR(50) NULL DEFAULT NULL,
    PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=MyISAM
AUTO_INCREMENT=1
;

只有第一个更新会一直更新,而其他字段不会更新。为什么?

0 个答案:

没有答案