php编程错误

时间:2018-04-27 02:19:29

标签: php

<?php
if (isset($_POST['value'])) {
    $StudentFirstName = $_POST['FirstName'];
    $StudentLastName = $_POST['LastName'];
    $OrgID = $_POST['OrganizationId'];
    $AmountRaised = $_POST['AmountRaised'];
    $Captain = $_POST['Captain'];
}

$server = "mssql.up.ist.psu.edu";
$connectionInfo = array(
    'Database' => 'pjb5422',
    'UID' => 'sqlpjb5422',
    'PWD' => 'vIPY5De2',
    'Encrypt' => '0',
    'CharacterSet' => 'UTF-8');
$connection = sqlsrv_connect($server, $connectionInfo);

if (!($connection)) {
    echo "Connection could not be established.";
    die(print_r(sqlsrv_errors(), true));
} else {

    $query = "INSERT INTO STUDENT VALUES (?,?, ?, ?, ?, ?)"; //**BUILDING AN INSERT STATEMENT

    $var = array(rand(40, 100000000), $StudentFirstName, $StudentLastName, $OrgId, $AmountRaised, $Captain);

    $sendIt = sqlsrv_query($connection, $query, $var); //submit the query

    echo "<br>Thanks for Registering!!<br><br>Go <a href='MiniThon.html'>Home</a>";
}

?>

我收到以下错误,它从未提交到我的SQL数据库

  

注意:未定义的变量:第50行的\ UP.IST.LOCAL \ WEBSITES \ pjb5422 \ AddDancer1.php中的StudentFirstName

     

注意:未定义的变量:第50行的\ UP.IST.LOCAL \ WEBSITES \ pjb5422 \ AddDancer1.php中的StudentLastName

     

注意:未定义的变量:第50行的\ UP.IST.LOCAL \ WEBSITES \ pjb5422 \ AddDancer1.php中的OrgId

     

注意:未定义的变量:第50行的\ UP.IST.LOCAL \ WEBSITES \ pjb5422 \ AddDancer1.php中的AmountRaised

     

注意:未定义的变量:第50行的\ UP.IST.LOCAL \ WEBSITES \ pjb5422 \ AddDancer1.php中的队长

3 个答案:

答案 0 :(得分:4)

更改此行

 if (isset($_POST['value'])) { 

检查表单中所需的其他值,因为值中没有任何值,导致声明的变量为null。

我还建议在执行插入查询之前,首先检查是否

  

$ StudentFirstName

在执行插入查询之前,

和其他变量不为空。

else {
    if(!empty($StudentFirstName) && !empty($StudentLastName)){
         $query = "INSERT INTO STUDENT VALUES (?,?, ?, ?, ?, ?)"; //**BUILDING AN INSERT STATEMENT

         $var = array(rand(40,100000000),$StudentFirstName, $StudentLastName, $OrgId, $AmountRaised, $Captain);

         $sendIt = sqlsrv_query($connection, $query, $var); //submit the query

         echo "<br>Thanks for Registering!!<br><br>Go <a href='MiniThon.html'>Home</a>"; 
    }else{
      echo "Failed to insert record empty variables!";

    }

}

答案 1 :(得分:1)

if (isset($_POST['value'])) {内的所有内容似乎都没有执行。 您需要检查$ _POST [&#39;值&#39;]值是否实际设置为某个值,以便变量采用所需的值。

答案 2 :(得分:0)

isset($_POST['value'])返回false时,您的$StudentFirstName应该Undefined variable显然

if (isset($_POST['value'])) {
    $StudentFirstName =$_POST['FirstName'];
    //...
}