简体   繁体   中英

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. 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

<?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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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