将值插入多个表PHP

时间:2015-05-17 07:31:40

标签: php web-services mysqli

我坚持写这种服务,其中信息被添加到多个表中。我的连接是建立的列名称是完美的,但不知道为什么我的第一个插入查询不起作用。仅供参考我正在通过REST api Client测试此服务。请帮帮我......

<?php
include_once 'connection.php';
$json = "";
$userId;
$shareId;
if ($_SERVER ['REQUEST_METHOD'] == "POST") {

    if (isset ( $_POST ["alarmId"] ) && isset ( $_POST ["phone"] ) && isset ( $_POST ["title"] ) && isset ( $_POST ["time"] ) && isset ( $_POST ["ringtone"] ) && isset ( $_POST ["snoozeDuration"] ) && isset ( $_POST ["gentleWakeup"] ) && isset ( $_POST ["mon"] ) && isset ( $_POST ["tues"] ) && isset ( $_POST ["wed"] ) && isset ( $_POST ["thu"] ) && isset ( $_POST ["fri"] ) && isset ( $_POST ["sat"] ) && isset ( $_POST ["sun"] ) && isset ( $_POST ["isOn"] ) && isset ( $_POST ["isShared"] ) && isset ( $_POST ["isRepeating"] ) && 
     //share content
    isset ( $_POST ["shallInform"] ) && isset ( $_POST ["textMsg"] ) && isset ( $_POST ["contacts"] )) {

        // alarm content..
        $alarmId = $_POST ["alarmId"];
        $phone = $_POST ["phone"];
        $title = $_POST ["title"];
        $time = $_POST ["time"];
        $date = $_POST ["date"];
        $ringtone = $_POST ["ringtone"];
        $snoozeDur = $_POST ["snoozeDuration"];
        $gentleWake = $_POST ["gentleWakeup"];
        $mon = $_POST ["mon"];
        $tues = $_POST ["tues"];
        $wed = $_POST ["wed"];
        $thu = $_POST ["thu"];
        $fri = $_POST ["fri"];
        $sat = $_POST ["sat"];
        $sun = $_POST ["sun"];
        $isOn = $_POST ["isOn"];
        $isShared = $_POST ["isShared"];
        $isRepeating = $_POST ["isRepeating"];

        // share content...
        $shallInform = $_POST ["shallInform"];
        $textMsg = $_POST ["textMsg"];
        ##$contact_list = $_POST ["contacts"];

        //inserting into alarm table..

        $s1 = "INSERT INTO alarm (alarmId,phone,title,time,date,ringtone,snoozeDuration,gentleWakeup,isMon,isTues,isWed,isThu,isFri,isSat,isSun,isOn,isShared,isRepeating) VALUES ('$alarmId','$phone','$title','$time', '$date' ,'$ringtone','$snoozeDur','$gentleWake','$mon','$tues','$wed','$thu','$fri','$sat','$sun','$isOn','$isShared','$isRepeating')";

        $qur = mysqli_query ( $con, $s1 );

        if ($qur) {
            echo "if it is inside.....";
            $json = array (
                    "status_code" => 200,
                    "message" => "alarm is successfully added" 
            );
            //getting user id..

            $getUserId = "SELECT userId from users WHERE phone = '" . $phone . "' LIMIT 1 ";
            $userQuery = mysqli_query ( $con, $getUserId );
            if($userQuery){
                while ( $row = mysqli_fetch_assoc ( $userQuery ) ) {
                    $userId = $row ['userId'];
                }   
            }
            else{
                $json = array (
                    "status_code" => 0,
                    "message" => "Invalid phone number, Alarm isn't added" 
            );
            }


            //inserting into share table..

            $insShare = "INSERT INTO share (alarmId,userId,shallInform,textMsg) VALUES ('$alarmId','$userId','$shallInform','$textMsg')";
            $shareResult = mysqli_query ( $con, $insShare );

            if($shareResult){
                $shareId = mysqli_insert_id ( $con );
            }// end of share insert Query
            else{
                $json = array (
                        "status_code" => 0,
                        "message" => "content to be shared cannot added"
                );
            }

            // sharing alarm with friends...
            for($i=0;$i<count($contact_list);$i++){
                $shareAlarm = "INSERT INTO shared_with (shareId,friendId)VALUES ('$shareId,'$contact_list[i])";
                $shareResult = mysqli_query($shareAlarm);

                if($shareResult){

                }
                else{
                    $json = array (
                            "status_code" => 0,
                            "message" => "alarm isn't shared"
                    );

                }


            }



        } else {
            $json = array (
                    "status_code" => 0,
                    "message" => "error adding alarm" 
            );
        }
    }
} else {
    $json = array (
            "status_code" => 400,
            "message" => "Request method not accepted" 
    );
}

    enter code here

mysqli_close ( $con );
//header ( 'Content-type: application/json' );
if ($json != "") {

    echo json_encode ( $json );
} else {
    $json = array (
            "status_code" => 0,
            "message" => "please set all required values before transmitting to server" 
    );
    echo json_encode ( $json );
}
?>

我的connection.php文件是

<?php

            define("SERVER", '127.0.0.1');
            define("USER", 'root');
            define("PASSWORD", "");
            define("DB", 'alarm_clock');


            $con = new mysqli(SERVER,USER,PASSWORD,DB);

            if ($con->connect_errno){
                die("Database Connection Failed");
                exit();

            }

1 个答案:

答案 0 :(得分:0)

应该有效

$s1 = "INSERT INTO alarm (`alarmId`,`phone`,`title`,`time`,`date`,`ringtone`,`snoozeDuration`,`gentleWakeup`,`isMon`,`isTues`,`isWed`,`isThu`,`isFri`,`isSat`,`isSun`,`isOn`,`isShared`,`isRepeating`) VALUES ('$alarmId','$phone','$title','$time', '$date' ,'$ringtone','$snoozeDur','$gentleWake','$mon','$tues','$wed','$thu','$fri','$sat','$sun','$isOn','$isShared','$isRepeating')";

请注意,时间,日期等是mysql保留的关键字,你应该使用`time`代替时间等等