简体   繁体   English

将值插入多个表PHP

[英]inserting values into multiple tables PHP

I am stuck at writing this kind of service where information is being added into multiple tables. 我坚持写这种服务,将信息添加到多个表中。 my connection is established column names are perfect but don't know why my first insert query doesn't work. 我的连接已建立,列名是完美的,但不知道为什么我的第一个插入查询不起作用。 FYI i am testing this service by REST api Client. 仅供参考,我正在通过REST api客户端测试此服务。 Please help me out.. 请帮帮我..

<?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 );
}
?>

My connection.php file is 我的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();

            }

It should work 它应该工作

$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')";

Please noted that time, date etc are mysql reserved keywords, you should use `time` instead of time and so on 请注意,时间,日期等是mysql保留的关键字,您应该使用“时间”而不是时间等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM