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

时间:2015-04-09 22:29:38

标签: php jquery mysql insert null

我被指派为学校项目创建一个网页,该网页将数据发布到'STUDYABROAD'中名为MySQL database的表格中。现在我的所有值都插入NULL(默认值),我无法确定为什么我对html select/option tags的引用不起作用。这项任务是我第一次接触PHP,而且我对该语言的了解非常有限。

此时代码使用jQuery为国家和大陆填充级联dropdowns

我打算让代码插入StudyIDTerm(学生在国外学习的学期),StudentID(会话变量),{{1一个学生在那里学习,以及该国家所在的name of the country

STUDYABROAD表结构:

continent

PHP,jQuery和HTML代码:

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

1 个答案:

答案 0 :(得分:0)

通过消除一些无用的代码并添加一些所需的代码,我能够使插件工作。额外的动作属性让我失望。谢谢你的帮助!

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