简体   繁体   English

为什么用PHP将NULL值插入MySQL表?

[英]Why am I inserting NULL values into a MySQL table with PHP?

I have been assigned to create a web page for a school project that posts data to a table called 'STUDYABROAD' in a MySQL database . 我已被分配为一个学校项目创建一个网页,该网页将数据发布到MySQL database中名为'STUDYABROAD'的表中。 Right now all of my values are inserting as NULL (the default values) and I can't identify why my references to the html select/option tags are not working. 现在,我所有的值都将以NULL (默认值)插入,并且我无法确定为什么我对html select/option tags引用不起作用。 This assignment has been my first exposure to PHP and I have a very limited knowledge of the language. 这项工作是我第一次接触PHP,而我对该语言的了解非常有限。

The code uses jQuery to populate cascading dropdowns for country and continent at this time. 该代码目前使用jQuery填充国家和大洲的级联dropdowns

I am intending the code to insert the StudyID , Term ( the semester that a student studied abroad), the StudentID (which is a session variable), the name of the country that a student studied in, and the continent that the country is located on. 我打算在代码中插入StudyIDTerm (一个学生在国外学习的学期), StudentID (这是一个会话变量),一个学生学习name of the countryname of the countrycontinent上。

STUDYABROAD table structure: StudyABROAD表结构:

Name        Type              Null    Default    Extra
StudyID     int(11)           No      None       AUTO-INCREMENT
Term        varchar(15)       Yes     NULL
StudentID   varchar(37)       Yes     NULL
CountryName varchar(75)       Yes     NULL
Continent   varchar(15)       Yes     NULL

PHP, jQuery, and HTML code: PHP,jQuery和HTML代码:

<?php

if(isset($_POST['submit']))
{
    upload();
}

function upload() {

require'dbc.php';

$servername = "localhost";
$username = "root";
$password = "isasurvey";
$dbname = "isasurvey";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
$StudyID=null;
$term = $_POST['sa_term'];  
$countryname = $_POST['sa_country'];
$continent = $_POST['sa_continent'];

//Insert statement
$stmt = $conn->prepare(" INSERT INTO STUDYABROAD(StudyID, Term, StudentID, CountryName, Continent)  VALUES ( ?, ?, ?, ?, ?)");
$stmt->bind_param("sssss", $studyid, $term, $_SESSION["studentID"], $countryname, $continent);
$stmt->execute();

//if ($conn->query($sql) === TRUE) {
//    echo "New record created successfully";
//} else {
//    echo "Error: " . $sql . "<br>" . $conn->error;
//}

$conn->close();
?>


<!DOCTYPE html>

<head>
<link href="Style.css" rel="stylesheet">

<h1> Miami University </h1>
<h4> Information Systems and Analytics Department </h4>  

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script type="text/javascript">

function go()
{
var Count = document.getElementById("sa_yesno").options[document.getElementById("sa_yesno").selectedIndex].value;
if(Count==1)
{
document.getElementById("info").style.display = 'none';
}
if(Count==2)
{
    document.getElementById("info").style.display = '';
}
}


</script>

</head>


<body>

<div class="StyleDiv"> 
<form action="Page3.php" method="post" id="myForm" onsubmit="return submit()" action="upload()">

<p> Please share any of the information below that you are comfortable sharing about your study abroad experience, if applicable.</p>
<p> 1. Did you study abroad? </p>

<p><select onchange="go()" name="sa_yesno" id="sa_yesno">
<option value="1">No</option> 
<option value="2">Yes</option> 

</select></p>
</div>
<div class="styleDiv" id="info" style="display:none">

<p> 2. When did you study abroad? </p> 

<p><select name="sa_term" id="sa_term">
<option value='Fall'>Fall Semester</object> 
<option value='Spring'>Spring Semester</object> 
<option value='J-term'>J-Term</object> 
<option value='Summer'>Summer Term</object> 
</select></p>

<p> 3. Where did you study abroad? </p> 

<label class="page1">Continent</label>
<div class="tooltips" title="Please select the continent where you studied.">
    <select id="sa_continent" name="sa_continent" placeholder="Continent">
        <option></option>
        <option>Africa</option>
        <option>Australia</option>
        <option>North America</option>
        <option>South America</option>
        <option>Europe</option>
        <option>Asia</option>
    </select>
</div>

<br/>

<label class="page1">Country</label>
<div class="tooltips" title="Please select the country where you studied.">
    <select id="sa_country" name="sa_country" placeholder="Country"></select>
</div>

<script type="text/javascript">
jQuery(function($) {
    var locations = {

        'Africa': ['Algeria','Angola','Benin','Botswana','Burkina','Burundi','Cameroon', 'Cape Verde',
        'Central African Republic','Chad','Comoros','Congo','Congo, Democratic Republic of','Djibouti',
        'Egypt','Equatorial Guinea','Eritrea','Ethiopia','Gabon','Gambia','Ghana','Guinea',
        'Guinea-Bissau','Ivory Coast','Kenya','Lesotho','Liberia','Libya','Madagascar','Malawi',
        'Mali','Mauritania','Mauritius','Morocco','Mozambique','Namibia','Niger','Nigeria','Rwanda',
        'Sao Tome and Principe','Senegal','Seychelles','Sierra Leone','Somalia','South Africa',
        'South Sudan','Sudan','Swaziland','Tanzania','Togo','Tunisia','Uganda','Zambia','Zimbabwe'],

        'Australia': ['Australia','Fiji','Kiribati','Marshall Islands','Micronesia','Nauru',
        'New Zealand','Palau','Papua New Guinea','Samoa','Solomon Islands','Tonga','Tuvalu','Vanuatu'],

        'North America': ['Antigua and Barbuda','Bahamas','Barbados','Belize','Canada',
        'Costa Rica','Cuba','Dominica','Dominican Republic','El Salvador','Grenada','Guatemala',
        'Haiti','Honduras','Jamaica','Mexico','Nicaragua','Panama','Saint Kitts and Nevis',
        'Saint Lucia','Saint Vincent and the Grenadines','Trinidad and Tobago','United States'],

        'South America': ['Argentina','Bolivia','Brazil','Chile','Colombia','Ecuador',
        'Guyana','Paraguay','Peru','Suriname','Uruguay','Venezuela'],

        'Europe': ['Albania','Andorra','Armenia','Austria','Azerbaijan','Belarus','Belgium',
        'Bosnia and Herzegovina','Bulgaria','Croatia','Cyprus','Czech Republic','Denmark',
        'Estonia','Finland','France','Georgia','Germany','Greece','Hungary','Iceland',
        'Ireland','Italy','Latvia','Liechtenstein','Lithuania','Luxembourg','Macedonia','Malta','Moldova','Monaco',
        'Montenegro','Netherlands','Norway','Poland','Portugal','Romania','San Marino','Serbia','Slovakia','Slovenia',
        'Spain','Sweden','Switzerland','Ukraine','United Kingdom','Vatican City'],

        'Asia': ['Afghanistan','Bahrain','Bangladesh','Bhutan','Brunei','Burma (Myanmar)','Cambodia',
        'China','East Timor','India','Indonesia','Iran','Iraq','Israel','Japan','Jordan','Kazakhstan',
        'Korea, North','Korea, South','Kuwait','Kyrgyzstan','Laos','Lebanon','Malaysia','Maldives',
        'Mongolia','Nepal','Oman','Pakistan','Philippines','Qatar','Russian Federation','Saudi Arabia',
        'Singapore','Sri Lanka','Syria','Tajikistan','Thailand','Turkey','Turkmenistan','United Arab Emirates',
        'Uzbekistan','Vietnam','Yemen'],
    }

    var $locations = $('#sa_country');
    $('#sa_continent').change(function () {
        var country = $(this).val(), lcns = locations[country] || [];

        var html = $.map(lcns, function(lcn){
            return '<option name="sa_country" id="sa_country" value="' + lcn + '">' + lcn + '</option>'
        }).join('');
        $locations.html(html)
    });
});


</script>

<br />
</div>
<input type="submit" value="Next" name="submit" id="submit">
</form>


</body>
</html>

?>

I was able to get the inserts to work by eliminating some useless code and adding some needed code. 通过消除一些无用的代码并添加一些所需的代码,我能够使插入工作。 The extra action attribute was throwing me off. 额外的动作属性使我不知所措。 Thanks for the help! 谢谢您的帮助!

 <?php session_start(); require'dbc.php'; require 'uuid.php'; if(isset($_POST['submit'])) { upload(); } function upload() { $servername = "localhost"; $username = "root"; $password = "isasurvey"; $dbname = "isasurvey"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } $StudyID=null; $term = $_POST['sa_term']; $countryname = $_POST['sa_country']; $continent = $_POST['sa_continent']; $stmt = $conn->prepare(" INSERT INTO STUDYABROAD(StudyID, Term, StudentID, CountryName, Continent) VALUES ( ?, ?, ?, ?, ?)"); $stmt->bind_param("sssss", $studyid, $term, $_SESSION["studentID"], $countryname, $continent); $stmt->execute(); //if ($conn->query($sql) === TRUE) { // echo "New record created successfully"; //} else { // echo "Error: " . $sql . "<br>" . $conn->error; //} $conn->close(); } ?> <!DOCTYPE html> <head> <link href="Style.css" rel="stylesheet"> <h1> Miami University </h1> <h4> Information Systems and Analytics Department </h4> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script> <script type="text/javascript"> function go() { var Count = document.getElementById("sa_yesno").options[document.getElementById("sa_yesno").selectedIndex].value; if(Count==1) { document.getElementById("info").style.display = 'none'; } if(Count==2) { document.getElementById("info").style.display = ''; } } </script> </head> <body> <div class="StyleDiv"> <form action="Page3.php" method="post" id="myForm" onsubmit="return submit()"> <p> Please share any of the information below that you are comfortable sharing about your study abroad experience, if applicable.</p> <p> 1. Did you study abroad? </p> <p><select onchange="go()" name="sa_yesno" id="sa_yesno"> <option value="1">No</option> <option value="2">Yes</option> </select></p> </div> <div class="styleDiv" id="info" style="display:none"> <p> 2. When did you study abroad? </p> <p><select name="sa_term" id="sa_term"> <option value="Fall">Fall Semester</object> <option value="Spring">Spring Semester</object> <option value="J-term">J-Term</object> <option value="Summer">Summer Term</object> </select></p> <p> 3. Where did you study abroad? </p> <label class="page1">Continent</label> <div class="tooltips" title="Please select the continent where you studied."> <select id="sa_continent" name="sa_continent" placeholder="Continent"> <option></option> <option value="Africa">Africa</option> <option value="Australia">Australia</option> <option value="North America">North America</option> <option value="South America">South America</option> <option value="Europe">Europe</option> <option value="Asia">Asia</option> </select> </div> <br/> <label class="page1">Country</label> <div class="tooltips" title="Please select the country where you studied."> <select id="sa_country" name="sa_country" placeholder="Country"></select> </div> <script type="text/javascript"> jQuery(function($) { var locations = { 'Africa': ['Algeria','Angola','Benin','Botswana','Burkina','Burundi','Cameroon', 'Cape Verde', 'Central African Republic','Chad','Comoros','Congo','Congo, Democratic Republic of','Djibouti', 'Egypt','Equatorial Guinea','Eritrea','Ethiopia','Gabon','Gambia','Ghana','Guinea', 'Guinea-Bissau','Ivory Coast','Kenya','Lesotho','Liberia','Libya','Madagascar','Malawi', 'Mali','Mauritania','Mauritius','Morocco','Mozambique','Namibia','Niger','Nigeria','Rwanda', 'Sao Tome and Principe','Senegal','Seychelles','Sierra Leone','Somalia','South Africa', 'South Sudan','Sudan','Swaziland','Tanzania','Togo','Tunisia','Uganda','Zambia','Zimbabwe'], 'Australia': ['Australia','Fiji','Kiribati','Marshall Islands','Micronesia','Nauru', 'New Zealand','Palau','Papua New Guinea','Samoa','Solomon Islands','Tonga','Tuvalu','Vanuatu'], 'North America': ['Antigua and Barbuda','Bahamas','Barbados','Belize','Canada', 'Costa Rica','Cuba','Dominica','Dominican Republic','El Salvador','Grenada','Guatemala', 'Haiti','Honduras','Jamaica','Mexico','Nicaragua','Panama','Saint Kitts and Nevis', 'Saint Lucia','Saint Vincent and the Grenadines','Trinidad and Tobago','United States'], 'South America': ['Argentina','Bolivia','Brazil','Chile','Colombia','Ecuador', 'Guyana','Paraguay','Peru','Suriname','Uruguay','Venezuela'], 'Europe': ['Albania','Andorra','Armenia','Austria','Azerbaijan','Belarus','Belgium', 'Bosnia and Herzegovina','Bulgaria','Croatia','Cyprus','Czech Republic','Denmark', 'Estonia','Finland','France','Georgia','Germany','Greece','Hungary','Iceland', 'Ireland','Italy','Latvia','Liechtenstein','Lithuania','Luxembourg','Macedonia','Malta','Moldova','Monaco', 'Montenegro','Netherlands','Norway','Poland','Portugal','Romania','San Marino','Serbia','Slovakia','Slovenia', 'Spain','Sweden','Switzerland','Ukraine','United Kingdom','Vatican City'], 'Asia': ['Afghanistan','Bahrain','Bangladesh','Bhutan','Brunei','Burma (Myanmar)','Cambodia', 'China','East Timor','India','Indonesia','Iran','Iraq','Israel','Japan','Jordan','Kazakhstan', 'Korea, North','Korea, South','Kuwait','Kyrgyzstan','Laos','Lebanon','Malaysia','Maldives', 'Mongolia','Nepal','Oman','Pakistan','Philippines','Qatar','Russian Federation','Saudi Arabia', 'Singapore','Sri Lanka','Syria','Tajikistan','Thailand','Turkey','Turkmenistan','United Arab Emirates', 'Uzbekistan','Vietnam','Yemen'], } var $locations = $('#sa_country'); $('#sa_continent').change(function () { var country = $(this).val(), lcns = locations[country] || []; var html = $.map(lcns, function(lcn){ return '<option name="sa_country" id="sa_country" value="' + lcn + '">' + lcn + '</option>' }).join(''); $locations.html(html) }); }); </script> <br /> </div> <input type="submit" value="Next" name="submit" id="submit"> </form> </body> </html> 

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

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