如何从php

时间:2018-08-20 05:16:00

标签: php html

 <?php
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "candidatesList";
        //create connection
        $conn = mysqli_connect($servername, $username, $password,$dbname);
        // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
//        $sql = "INSERT INTO candidatesListFields (Sourcing_HR, DOS, Candidate_Name, Total_Experience, Current_CTC, Expected_CTC, Current_Location, Preferred_Location) VALUES ('', '', '', '', '', '','','')";
    ?>
</head>

<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-xs-12 header">
                <h3 class="pull-left">Track your Candidates</h3>
                <button type="button" class="pull-right btn btn-primary exportBtn" data-toggle="modal" data-target=".exportTable"><i class="glyphicon glyphicon-save saveIcon"></i>&nbsp;Export to Excel</button>
                <div class="clear"></div>
            </div>
            <div class="col-xs-12">
                <div class="col-lg-12 formWrapper">
                    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="sourcingHR">Sourcing HR</label>
                            <input type="text" class="form-control" id="sourcingHR" value="<?php echo $Sourcing_HR;?>" name="Sourcing_HR" placeholder="Sourcing HR">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="dateOfSourcing">Date of Sourcing</label>
                            <input type="text" class="form-control" id="dateOfSourcing" name="DOS" placeholder="Date of sourcing">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="candidateName">Candidate Name</label>
                            <input type="text" class="form-control" id="candidateName" name="Candidate_Name" placeholder="Candidate Name">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="totalExperience">Total Experience</label>
                            <input type="text" class="form-control" id="totalExperience" name="Total_Experience" placeholder="Total Experience">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="currCTC">Curr. CTC</label>
                            <input type="text" class="form-control" id="currCTC" name="Current_CTC" placeholder="Current CTC">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="expCTC">Exp. CTC</label>
                            <input type="text" class="form-control" id="expCTC" name="Expected_CTC" placeholder="Expected CTC">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="currLocation">Current Location</label>
                            <input type="text" class="form-control" id="currLocation" name="Current_Location" placeholder="Current Location">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="prefLocation">Preferred Location</label>
                            <input type="text" class="form-control" id="prefLocation" name="Preferred_Location" placeholder="Preferred Location">
                        </div>
                        <div class="form-group col-xs-12 text-right">
                            <button class="btn btn-success" type="submit">
                                <i class="glyphicon glyphicon-floppy-disk saveIcon"></i>&nbsp;Save
                            </button>
                        </div>
                    </form>

                </div>
            </div>
        </div>
    </div>

在这里,我已经创建了数据库,现在我想将以下字段的值保存到数据库中,但是它给出了未定义的索引。我该怎么办。         

        if(!empty($_POST['Sourcing_HR'] AND $_POST['DOS'] AND $_POST['Candidate_Name'] AND $_POST['Candidate_Name'] AND $_POST['Total_Experience'] AND $_POST['Current_CTC'] AND $_POST['Expected_CTC'] AND $_POST['Current_Location'] AND $_POST['Preferred_Location'])){
            $sql = "INSERT INTO candidatesListFields (Sourcing_HR, DOS, Candidate_Name, Total_Experience, Current_CTC, Expected_CTC, Current_Location, Preferred_Location) VALUES ('$Sourcing_HR', '$DOS', '$Candidate_Name', '$Total_Experience', '$Current_CTC', '$Expected_CTC','$Current_Location','$Preferred_Location')";
            echo($_POST[$Sourcing_HR.' '. $DOS.' '. $Candidate_Name.' '. $Total_Experience.' '. $Current_CTC.' '. $Expected_CTC.' '. $Current_Location.' '. $Preferred_Location]);
        }

        if (isset($_POST['Sourcing_HR'])) {
            $Sourcing_HR = $_POST['Sourcing_HR'];
        }

    ?>

这无法正常工作,如果有人在那里请帮助*

 <?php
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "candidatesList";
        //create connection
        $conn = mysqli_connect($servername, $username, $password,$dbname);
        // Check connection
        if (!$conn) {
            die("Connection failed: " . mysqli_connect_error());
        }
//        $sql = "INSERT INTO candidatesListFields (Sourcing_HR, DOS, Candidate_Name, Total_Experience, Current_CTC, Expected_CTC, Current_Location, Preferred_Location) VALUES ('', '', '', '', '', '','','')";
    ?>
    
    <?php
        $sql = "INSERT INTO candidatesListFields (Sourcing_HR, DOS, Candidate_Name, Total_Experience, Current_CTC, Expected_CTC, Current_Location, Preferred_Location) VALUES ('', '', '', '', '', '','','')";
        // getting values of input fields from form
        $Sourcing_HR = mysqli_real_escape_string($conn,$_POST['Sourcing_HR']);
        $DOS = mysqli_real_escape_string($conn,$_POST['DOS']); 
        $Candidate_Name = mysqli_real_escape_string($conn,$_POST['Candidate_Name']); 
        $Total_Experience = mysqli_real_escape_string($conn,$_POST['Total_Experience']);
        $Current_CTC = mysqli_real_escape_string($conn,$_POST['Current_CTC']);
        $Expected_CTC = mysqli_real_escape_string($conn,$_POST['Expected_CTC']);
        $Current_Location = mysqli_real_escape_string($conn,$_POST['Current_Location']);
        $Preferred_Location = mysqli_real_escape_string($conn,$_POST['Preferred_Location']);
    
        if(!empty($_POST['Sourcing_HR'] AND $_POST['DOS'] AND $_POST['Candidate_Name'] AND $_POST['Candidate_Name'] AND $_POST['Total_Experience'] AND $_POST['Current_CTC'] AND $_POST['Expected_CTC'] AND $_POST['Current_Location'] AND $_POST['Preferred_Location'])){
            $sql = "INSERT INTO candidatesListFields (Sourcing_HR, DOS, Candidate_Name, Total_Experience, Current_CTC, Expected_CTC, Current_Location, Preferred_Location) VALUES ('$Sourcing_HR', '$DOS', '$Candidate_Name', '$Total_Experience', '$Current_CTC', '$Expected_CTC','$Current_Location','$Preferred_Location')";
            echo($_POST[$Sourcing_HR.' '. $DOS.' '. $Candidate_Name.' '. $Total_Experience.' '. $Current_CTC.' '. $Expected_CTC.' '. $Current_Location.' '. $Preferred_Location]);
        }
    
        if (isset($_POST['Sourcing_HR'])) {
            $Sourcing_HR = $_POST['Sourcing_HR'];
        }
        
    ?>
</head>

<body>
    <div class="container-fluid">
        <div class="row">
            <div class="col-xs-12 header">
                <h3 class="pull-left">Track your Candidates</h3>
                <button type="button" class="pull-right btn btn-primary exportBtn" data-toggle="modal" data-target=".exportTable"><i class="glyphicon glyphicon-save saveIcon"></i>&nbsp;Export to Excel</button>
                <div class="clear"></div>
            </div>
            <div class="col-xs-12">
                <div class="col-lg-12 formWrapper">
                    <form method="post" action="<?php echo $_SERVER['PHP_SELF'];?>">
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="sourcingHR">Sourcing HR</label>
                            <input type="text" class="form-control" id="sourcingHR" value="<?php echo $Sourcing_HR;?>" name="Sourcing_HR" placeholder="Sourcing HR">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="dateOfSourcing">Date of Sourcing</label>
                            <input type="text" class="form-control" id="dateOfSourcing" name="DOS" placeholder="Date of sourcing">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="candidateName">Candidate Name</label>
                            <input type="text" class="form-control" id="candidateName" name="Candidate_Name" placeholder="Candidate Name">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="totalExperience">Total Experience</label>
                            <input type="text" class="form-control" id="totalExperience" name="Total_Experience" placeholder="Total Experience">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="currCTC">Curr. CTC</label>
                            <input type="text" class="form-control" id="currCTC" name="Current_CTC" placeholder="Current CTC">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="expCTC">Exp. CTC</label>
                            <input type="text" class="form-control" id="expCTC" name="Expected_CTC" placeholder="Expected CTC">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="currLocation">Current Location</label>
                            <input type="text" class="form-control" id="currLocation" name="Current_Location" placeholder="Current Location">
                        </div>
                        <div class="form-group col-xs-12 col-md-3">
                            <label for="prefLocation">Preferred Location</label>
                            <input type="text" class="form-control" id="prefLocation" name="Preferred_Location" placeholder="Preferred Location">
                        </div>
                        <div class="form-group col-xs-12 text-right">
                            <button class="btn btn-success" type="submit">
                                <i class="glyphicon glyphicon-floppy-disk saveIcon"></i>&nbsp;Save
                            </button>
                        </div>
                    </form>

                </div>
            </div>
        </div>
    </div>
    

*

2 个答案:

答案 0 :(得分:1)

empty()不接受多个参数,即使接受了(如isset()这样),它也希望它们之间用逗号分隔(而不是AND)。

您可以像这样在单个调用中检查POST是否存在所有预期的isset()键:

if (isset($_POST['Sourcing_HR'], $_POST['DOS'], $_POST['Candidate_Name'],
          $_POST['Candidate_Name'], $_POST['Total_Experience'], $_POST['Current_CTC'],
          $_POST['Expected_CTC'], $_POST['Current_Location'], $_POST['Preferred_Location'])) {

或者,如果需要,请使用!empty()确保所有值都不为空并且不包含字符串0。但是!empty()必须在每个元素上调用。

if (!empty($_POST['Sourcing_HR']) && !empty($_POST['DOS']) && !empty($_POST['Candidate_Name'])
    && !empty($_POST['Candidate_Name']) && !empty($_POST['Total_Experience'])
    && !empty($_POST['Current_CTC']) && !empty($_POST['Expected_CTC'])
    && !empty($_POST['Current_Location']) && !empty($_POST['Preferred_Location'])) {

如果您希望针对不包含所需数据的特定字段提供个性化反馈,则必须将if条件(需要编写的代码更多)分开。

在检查所有期望的元素都存在之后,请使用带占位符和绑定参数的预准备语句以确保安全性和稳定性(而不是mysqli逃逸)。

这是使用面向对象语法的未经测试的建议:

if (!$conn = new mysqli("localhost", "root", "", "candidatesList")) {
    echo "Database Connection Error: " , $conn->connect_error;  // don't show exact error publicly
} else {
    if (!$stmt = $conn->prepare("INSERT INTO candidatesListFields (Sourcing_HR, DOS, Candidate_Name, Total_Experience, Current_CTC, Expected_CTC, Current_Location, Preferred_Location) VALUES (?,?,?,?,?,?,?,?)")) {
        echo "Prepare Syntax Error: " , $conn->error;   // don't show exact error publicly
    } else {
        if (!$stmt->bind_param("ssssssss", $_POST['Sourcing_HR'], $_POST['DOS'], $_POST['Candidate_Name'], $_POST['Total_Experience'], $_POST['Current_CTC'], $_POST['Expected_CTC'], $_POST['Current_Location'], $_POST['Preferred_Location']) || !$stmt->execute()) {
            echo "Query Error: " , $stmt->error;   // don't show exact error publicly
        }else{
            echo "Success";
        }
        $stmt->close();
    }
    $conn->close();
}

答案 1 :(得分:0)

U应该使用&& !empty($_POST['DOS'])而不是,