jquery - Why am I inserting NULL values into a MySQL table with PHP? -
i have been assigned create web page school project posts data table called 'studyabroad'
in mysql database
. right of values inserting null
(the default values) , can't identify why references html select/option tags
not working. assignment has been first exposure php , have limited knowledge of language.
the code uses jquery populate cascading dropdowns
country , continent @ time.
i intending code insert studyid
, term
( semester student studied abroad), studentid
(which session variable), name of country
student studied in, , continent
country located on.
studyabroad table structure:
name type null default 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, , html code:
<?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 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 , 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 of information below comfortable sharing study abroad experience, if applicable.</p> <p> 1. did 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 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. did study abroad? </p> <label class="page1">continent</label> <div class="tooltips" title="please select continent 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 country 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 , 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 , barbuda','bahamas','barbados','belize','canada', 'costa rica','cuba','dominica','dominican republic','el salvador','grenada','guatemala', 'haiti','honduras','jamaica','mexico','nicaragua','panama','saint kitts , nevis', 'saint lucia','saint vincent , grenadines','trinidad , 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 , 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 able inserts work eliminating useless code , adding needed code. action attribute throwing me off. 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 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 , 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 of information below comfortable sharing study abroad experience, if applicable.</p> <p> 1. did 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 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. did study abroad? </p> <label class="page1">continent</label> <div class="tooltips" title="please select continent 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 country 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 , 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 , barbuda','bahamas','barbados','belize','canada', 'costa rica','cuba','dominica','dominican republic','el salvador','grenada','guatemala', 'haiti','honduras','jamaica','mexico','nicaragua','panama','saint kitts , nevis', 'saint lucia','saint vincent , grenadines','trinidad , 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 , 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>