值不为空时如何解决空错误

时间:2019-07-01 15:29:58

标签: php mysql

我有一个表单,该表单从mysqli查询中接受php变量,然后在输入字段中显示。我得到的错误是... “错误:SQLSTATE [23000]:违反完整性约束:1048列'profileid'不能为空”,但该列也不为空。我检查了数据库,唯一的空列是id。它工作得很好,只是停了下来。我能够为profileid加载数据,所以我不确定为什么它说不为null时不能为null。我在这里看过其他几篇文章,但是还无法解决。请帮忙!

CoinSubmission.php

<?php   
    $con=mysqli_connect("localhost","root","password","administrator_logins");
    session_start();
    if($_SESSION['profileid'] != 'profileid') {

    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }

    $selectquery = $con->prepare("SELECT adminlogin.nickname FROM adminlogin LEFT JOIN coinsub ON coinsub.profileid=adminlogin.profileid");
    $selectquery->execute(); 
    $result = $selectquery->get_result(); 
    $user = $result->fetch_assoc();
    $nickname = $user['nickname'];
    $string_nickname = implode(',', $user);
    $selectquery->close();

    $sql = $con->prepare("SELECT adminlogin.contactnumber FROM adminlogin LEFT JOIN coinsub ON coinsub.profileid=adminlogin.profileid") or die(mysqli_error($con));
    $sql->execute(); 
    $result = $sql->get_result(); 
    $contact = $result->fetch_array();
    $contactnumber = $contact['contactnumber'];
    $string_contactnumber = implode(',', $contact);
    $sql->close();

    $sql = $con->prepare("SELECT adminlogin.currentprize FROM adminlogin LEFT JOIN coinsub ON coinsub.profileid=adminlogin.profileid") or die(mysqli_error($con));
    $sql->execute(); 
    $result = $sql->get_result(); 
    $prize = $result->fetch_array();
    $currentprize = $prize['currentprize'];
    $string_currentprize = implode(',', $prize);
    $sql->close();

    $stmt = mysqli_query($con, "SELECT AUTO_INCREMENT FROM information_schema.tables WHERE TABLE_NAME = 'cwb_coins'");
    $cwb = mysqli_fetch_assoc($stmt);
    $string_cwb = implode(',', $cwb);
    $stmt->close();

    $stm = mysqli_query($con, "SELECT AUTO_INCREMENT FROM information_schema.tables WHERE TABLE_NAME = 'wc_coins'");
    $wc = mysqli_fetch_assoc($stm);
    $string_wc = implode(',', $wc);
    $stm->close();

    $st = mysqli_query($con, "SELECT AUTO_INCREMENT FROM information_schema.tables WHERE TABLE_NAME = 'lck_coins'");
    $lck = mysqli_fetch_assoc($st);
    $string_lck = implode(',', $lck);
    $st->close();

    $sts = mysqli_query($con, "SELECT AUTO_INCREMENT FROM information_schema.tables WHERE TABLE_NAME = 'tp_coins'");
    $tp = mysqli_fetch_assoc($sts);
    $string_tp = implode(',', $tp);
    $sts->close();

    mysqli_close($con);
    }
    class MyDateTime extends DateTime
    {
        public function nthDayOfMonth($n, $day, $month = null, $year = null)
        {
            $timestr = "$n $day";
            if(!$month) $month = $this->format('M');
            $timestr .= " of $month $year";
            $this->setTimestamp(strtotime($timestr));
            $this->setTime(0, 0, 0);
            return $this;
        }

        public function secondFriday()
        {
            $timestr = 'second friday of ' . date('M') . ' ' . date('Y');
            $this->setTimestamp(strtotime($timestr));
            $this->setTime(0, 0, 0);
            return $this;
        }

        public function fourthFriday()
        {
            $timestr = 'fourth friday of ' . date('M') . ' ' . date('Y');
            $this->setTimestamp(strtotime($timestr));
            $this->setTime(0, 0, 0);
            return $this;       
        }
    }
    $dateTime = new MyDateTime();
?>

<!DOCTYPE html>
<html lang="en">
<head>  
<title>Coin Submission</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
    <link rel="stylesheet" href="assets/css/style.css">
    <style>
        html,body,h1,h2,h3,h4 {font-family:"Lato", sans-serif}
        .mySlides {display:none}
        .w3-tag, .fa {cursor:pointer}
        .w3-tag {height:15px;width:15px;padding:0;margin-top:6px}
        #about {padding-top:2%;}
        #store {-webkit-user-select: none; /* Safari 3.1+ */
                 -moz-user-select: none; /* Firefox 2+ */
                 -ms-user-select: none; /* IE 10+ */
                 user-select: none; /* Standard syntax */}
    </style>
</head>
    <body>
        <div class="w3-row" id="about">
            <div class="w3-center"> 
<form id="test" autocomplete="off" action="form-handler.php" method="POST">
  <h1 id="litheader">Catch Coin Submission</h1>
    <div class="inset">
          <input type="hidden" name="profileid" value="<?php echo $profileid; ?>">
        <p>
        <label>STORE:</label>
            <input type="text" list="www" name="store" placeholder="Choose a store..." required>
                <datalist id="www">
                  <option value="Causeway Bay">
                  <option value="Wan Chai">
                  <option value="Lai Chi Kok">
                  <option value="Tai Po">
                </datalist>
        </p>
        <p>
        <label>POSITION:</label>
            <input type="text" list="position" name="position" placeholder="Choose a position..." required>
                <datalist id="position">
                    <option value="cwb_coins"><?php echo "CWB ", $string_cwb; ?></option>
                    <option value="wc_coins"><?php echo "WC ", $string_wc; ?></option>
                    <option value="lck_coins"><?php echo "LCK ", $string_lck; ?></option>
                    <option value="tp_coins"><?php echo "TP ", $string_tp; ?></option>
                </datalist>
        </p>
        <p> 
        <label>NICKNAME:</label>
          <input type="text" name="nickname" id="nickname" value="<?php echo $_SESSION['nickname'];?>" readonly>
        </p>
        <p>
        <label>CONTACT-NUMBER:</label>
          <input type="text" name="contactnumber" id="contactnumber" value="<?php echo $_SESSION['contactnumber'];?>" readonly>
        </p>
        <p>
        <label>MACHINE-COUNT:</label>
          <input autocomplete="off" type="text" name="machinecount" id="machinecount" placeholder="Enter machine count..." required>
        </p>
        <p>
          <label for="cutoffdate">CUT-OFF-DATE:</label>
          <input type="text" name="cutoffdate" value="<?php echo $dateTime->secondFriday()->format('m-d-Y') . ' / ' . $dateTime->fourthFriday()->format('m-d-Y') ; ?>" readonly>
        </p>
        <p>
        <label>COINS:</label>
          <input type="text" name="coins" id="coins" placeholder="Enter coin amount..." required>
        </p>
        <br>
        <p>
          <input type="file" type="text" name="location" accept="image/*" required>
        </p>
        <p>
          <input type="hidden" name="lastsubmission" id="lastsubmission" readonly>
        </p>
        <p>
          <input type="hidden" name="rank" />
        </p> 
        <p>
          <input type="hidden" name="currentprize" id="currentprize" readonly>
        </p>
        <p>
          <input type="hidden" name="leasestartdate" id="leasestartdate" readonly>
        </p>
        <p>
          <input type="hidden" name="leasedaysremaining" id="leasedaysremaining" readonly>
        </p>
        <p>
          <input type="hidden" name="monthlyrent" id="monthlyrent" readonly>
        </p>
        <p>
          <input type="hidden" name="prepaymentamount" id="prepaymentamount" readonly>
        </p>
        <p>
          <input type="hidden" name="securitydeposit" id="securitydeposit" readonly>
        </p>
        <p>
          <input type="hidden" name="paymentmethod" id="paymentmethod" readonly>
        </p>
        <p>
          <input type="hidden" name="leaseenddate" id="leaseenddate" readonly>
        </p>
        <p>
          <input type="hidden" name="leasenoticedate" id="leasenoticedate" readonly>
        </p>
        <p>
          <input type="hidden" name="prepaymentmonths" id="prepaymentmonths" readonly>
        </p>
        <p>
          <input type="hidden" name="amountpaid" id="amountpaid" readonly>
        </p>
        <p>
          <input type="hidden" name="paymentoutstanding" id="paymentoutstanding" readonly>
        </p>
        <div class="btnConfirm">       
            <input class="loginLoginValue" type="hidden" name="" value="" />
        </div>
    </div>
        <div class="btnConfirm">
            <input type="submit" onclick="location.href='CoinSubmission.php';" name="Submit" value="Confirm">
        </div><br><br>
        <div class="wrapper2">
        <nav>
            <ul>
              <li><a href="AdminSubmission.php" class="active">SUBMISSION</a></li>
              <li><a href="OccupancyListAdmin.php">OCCUPANCY</a></li>
              <li><a href="#">ANALYTICS</a></li>
              <li><a href="#">SEARCH</a></li>
            </ul>
        </nav>
        </div>
    </form>
        </div>
        </div>
        <script>
        var currentDate = new Date(new Date().getTime())
        document.getElementById('lastsubmission').value = (currentDate.getDate()) +'/' + (currentDate.getMonth()+1) +'/'+ currentDate.getFullYear() +' @ '+ currentDate.getHours() +':'+ currentDate.getMinutes() +':'+ currentDate.getSeconds();
        </script>
        <script src='https://cdnjs.cloudflare.com/ajax/libs/tinycolor/1.4.1/tinycolor.min.js'></script>
        <script  src="assets/js/index.js"></script>

    </body>
</html>

form-handler.php

    <?php
    $recipient = $_POST['position'];

    //Based on the value of the $recipient value redirect to the correct page 
    //using the header function

    switch($recipient) {
        case 'cwb_coins':
            header('Location: cwb_coin_code.php'); exit;
        case 'wc_coins':
            header('Location: wc_coin_code.php'); exit;
        case 'lck_coins':
            header('Location: lck_coin_code.php'); exit;
        case 'tp_coins':
            header('Location: tp_coin_code.php'); exit;           
    }

cwb_coin_code.php

<?php {
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "administrator_logins";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    // set the PDO error mode to exception
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    // prepare sql and bind parameters
    $stmt = $conn->prepare("INSERT INTO cwb_coins (id, profileid, store, position, 
        nickname, contactnumber, machinecount, cutoffdate, coins, location, lastsubmission, rank, currentprize, leasestartdate, leasedaysremaining, monthlyrent, prepaymentamount, securitydeposit, paymentmethod, leaseenddate, leasenoticedate, prepaymentmonths, amountpaid, paymentoutstanding) 
        VALUES (:id,:profileid,:store,:position, :nickname,:contactnumber,:machinecount,:cutoffdate, :coins,:location,:lastsubmission,:rank,:currentprize,:leasestartdate,:leasedaysremaining,:monthlyrent,:prepaymentamount,:securitydeposit,:paymentmethod,:leaseenddate,:leasenoticedate,:prepaymentmonths,:amountpaid,:paymentoutstanding)");

    $stmt->bindParam(':id', $_POST['id']);
    $stmt->bindParam(':profileid', $_POST['profileid']);
    $stmt->bindParam(':store', $_POST['store']);
    $stmt->bindParam(':position', $_POST['position']);
    $stmt->bindParam(':nickname', $_POST['nickname']);
    $stmt->bindParam(':contactnumber', $_POST['contactnumber']);
    $stmt->bindParam(':machinecount', $_POST['machinecount']);
    $stmt->bindParam(':cutoffdate', $_POST['cutoffdate']);
    $stmt->bindParam(':coins', $_POST['coins']);
    $stmt->bindParam(':location', $_POST['location']);
    $stmt->bindParam(':lastsubmission', $_POST['lastsubmission']);
    $stmt->bindParam(':rank', $_POST['rank']);
    $stmt->bindParam(':currentprize', $_POST['currentprize']);
    $stmt->bindParam(':leasestartdate', $_POST['leasestartdate']);
    $stmt->bindParam(':leasedaysremaining', $_POST['leasedaysremaining']);
    $stmt->bindParam(':monthlyrent', $_POST['monthlyrent']);
    $stmt->bindParam(':prepaymentamount', $_POST['prepaymentamount']);
    $stmt->bindParam(':securitydeposit', $_POST['securitydeposit']);
    $stmt->bindParam(':paymentmethod', $_POST['paymentmethod']);
    $stmt->bindParam(':leaseenddate', $_POST['leaseenddate']);
    $stmt->bindParam(':leasenoticedate', $_POST['leasenoticedate']);
    $stmt->bindParam(':prepaymentmonths', $_POST['prepaymentmonths']);
    $stmt->bindParam(':amountpaid', $_POST['amountpaid']);
    $stmt->bindParam(':paymentoutstanding', $_POST['paymentoutstanding']);

    $stmt->execute();

    echo "Success";
    }
catch(PDOException $e)
    {
    echo "Error: " . $e->getMessage();
    }
$conn = null;
}

?>

cwb_coins table admin_logins

0 个答案:

没有答案