我正在尝试制作一个包含一些文本输入,选择输入和日期输入的表单,其中一些输入是可选的。所以,如果有人将这些输入留空并提交,我想要" NULL"要输入mysql数据库而不是' '
以下是我尝试的代码的一部分:
<?php
$con = mysqli_connect('localhost','root','abcdef','trademarkfilingproject');
error_reporting(1);
$Preference2 = 'Null';
if(!$con)
{
echo "Not Connected To Server";
}
else{
$Department = $_POST['department'];
$Classs = $_POST['classs'];
$Preference1 = $_POST['preference1'];
function replaceWithNull(&$Preference2)
{
if (empty($Preference2) || $Preference2 == ' ') {
$Preference2 = "NULL";
}
}
array_walk($_POST, "replaceWithNull");
$Preference3 = '';
$Preference4 = '';
$Preference5 ='';
$Goods = $_POST['goodsnservice'];
$Mark = $_POST['mark'];
$MarkYesWhen = $_POST['yeswhen'];
$MarkYesCountry = $_POST['yescountry'];
$MarkNoWhen = '';
$MarkNoCountry = '';
$MarkAppliedCountry = $_POST['markappliedcountry'];
//echo $_POST['prefer1'];
//print_r($_POST['yescountry']); die();
$sql = "INSERT INTO requestor (Department,Class,Preference_One,Preference_Two,Preference_Three,Preference_Four,Preference_Five,Goods_Services,Mark_In_Use,Mark_Yes_When,Mark_Yes_Country,Mark_No_When,Mark_No_Country,Mark_Applied_Country) VALUES('$Department','$Classs','$Preference1','$Preference2','$Preference3','$Preference4','$Preference5','$Goods','$Mark','$MarkYesWhen','$MarkYesCountry','$MarkNoWhen','$MarkNoCountry','$MarkAppliedCountry')";
$r = mysqli_query($con,$sql);
header("refresh:1 ; url=success.html");
}
?>
如果我使用上面的代码,首选项2得到&#34; NULL&#34;如果是空的但是,如果输入不是空白,我正在尝试使用以下代码:
function replaceWithNull(&$Preference2)
{
if (empty($Preference2) || $Preference2 == ' ') {
$Preference2 = "NULL";
}
else {
$Preference2 = $_POST['preference2'];
}
}
array_walk($_POST, "replaceWithNull");
以下是我的HTML代码:
<!DOCTYPE html>
<html>
<head>
<link rel="icon" href="\images\trademark_icon.png" type="image/gif" sizes="16x16">
</head>
<style>
.error {color: #FF0000;}
#markyes {
display: none;
padding: 10px;
background-color: #f2f2f2;
}
#markno {
display: none;
padding: 10px;
background-color: #f2f2f2;
}
input[value="yes"]:checked ~ #markyes {
display: block;
}
input[value="no"]:checked ~ #markno {
display: block;
}
input[type=text], select {
width: 90%;
padding: 12px 20px;
margin: 8px 0;
display: inline-block;
border: 1px solid #ccc;
border-radius: 4px;
box-sizing: border-box;
}
input[type=submit] {
width: 100%;
background-color: #4CAF50;
color: white;
padding: 14px 20px;
margin: 8px 0;
border: none;
border-radius: 4px;
cursor: pointer;
}
input[type=button] {
width: 10%;
color: black;
padding: 10px 15px;
margin: 8px 0;
outline: 2px black;
border-radius: 4px;
cursor: pointer;
}
input[type=submit]:hover {
background-color: #45a049;
}
div {
border-radius: 5px;
background-color: #f2f2f2;
padding: 10px 40px;
}
body{
background-image: url("images/bg-01.jpg");
background-repeat:repeat;
z-index:1;
}
.form-title {
display: block;
width: 100%;
font-size: 30px;
color: white;
text-transform: uppercase;
text-align: center;
margin:auto;
padding:50;
}
.herologo {
hight:70px;
width:60px;
background-position: top-left;
float:left;
}
</style>
<body>
<h2 ><img src="images/hero_logo.png" class="herologo" ><span class="form-title">Trademark Filing Application</span></h2><br>
<div>
<p style="padding-left:20px"><span class="error">* required field.</span></p>
<form action="insert.php" method="post" enctype="multipart/form-data">
<!-- Department -->
<b>Select Department:</b><span class="error"> *</span>
<select id="dept" name="department" required>
<option value="">Please Select...</option>
<option value="ceo">CEO Office</option>
<option value="global">Global</option>
<option value="marketing">Marketing</option>
<option value="parts">Parts</option>
</select>
<br><br><br>
<!-- Class -->
<b>Select Class:</b><span class="error"> *</span><br><br>
<input type="radio" id="wordradio" name="classs" value="Word" required>Word
<input type="radio" id="deviceradio" name="classs" value="Device" style="margin-left:20px">Device
<input type="radio" id="logoradio" name="classs" value="Logo" style="margin-left:20px">Logo
<input type="radio" id="soundradio" name="classs" value="Sound" style="margin-left:20px">Sound
<input type="radio" id="taglineradio" name="classs" value="Tagline" style="margin-left:20px">Tagline
<br><br><br>
<!-- Preferences -->
<b>Enter Preference(s):</b><br>
1. <input type="text" id="pref1" name="preference1" style="width:30%" placeholder="Preference 1" required>
<input type="file" id="prefbtn1" name="files[]" multiple="" required>
<span class="error">*</span>
<br>
2. <input type="text" id="pref2" name="preference2" style="width:30%" placeholder="Preference 2">
<input type="file" id="prefbtn2" name="files[]" multiple="" >
<br>
3. <input type="text" id="pref3" name="preference3" style="width:30%" placeholder="Preference 3">
<input type="file" id="prefbtn3" name="files[]" multiple="" >
<br>
4. <input type="text" id="pref4" name="preference4" style="width:30%" placeholder="Preference 4">
<input type="file" id="prefbtn4" name="files[]" multiple="" >
<br>
5. <input type="text" id="pref5" name="preference5" style="width:30%" placeholder="Preference 5">
<input type="file" id="prefbtn5" name="files[]" multiple="" >
<br><br><br>
<!-- Goods and Services -->
<b>Enter The Types of Goods & Services :</b><span class="error"> *</span>
<br>
<input type="text" id="goodss" name="goodsnservice" placeholder="Select the type of Goods and Services" required>
<br><br><br>
<!-- Mark in Use? -->
<b>Is Mark In Use?</b><span class="error"> *</span><br><br>
<input type="radio" id="marky" name="mark" value="yes" onclick="disableNo()" required>Yes
<!-- Mark in Use?: YES -->
<div id="markyes" class="show-hide" style="background-color:white;width:600px ;margin-left:40px; border:1px solid black; border-radius:5px"><br>
<b>When Did You Use It?</b>
<input type="date" name="yeswhen" style="margin-left:10px" id="markyesdate" required><span class="error"> *</span><br><br>
<b>Where Did You Use It?</b><span class="error"> *</span><br>
<select name="yescountry[]" style="width:20%" id="markyescountry" multiple required>
<option value="Afghanistan">Afghanistan</option>
...
<option value="Zimbabwe">Zimbabwe</option>
</select><br><br>
<b>Proof Of First Use:</b><span class="error"> *</span><br>
<input type="file" id="markyesproof" name="files[]" value="Upload one or more files" multiple="" required>
</div><br>
<!-- Mark in Use?: NO -->
<input type="radio" name="mark" value="no" onclick="disableYes()" >No<br>
<div id="markno" class="show-hide" style="background-color:white;width:600px ;margin-left:40px; border:1px solid black; border-radius:5px"><br>
<b>When Are You Expected To Use It?</b>
<input type="date" name="nowhen" style="margin-left:10px" id="marknodate" required><span class="error"> *</span><br><br>
<b>Where will You Use It?</b><span class="error"> *</span><br>
<select name="nocountry[]" style="width:20%" id="marknocountry" multiple required>
<option value="Afghanistan">Afghanistan</option>
...
<option value="Zimbabwe">Zimbabwe</option>
</select><br><br>
</div><br>
<!-- Mark Applied Country -->
<b style="text-height:50%">Mark Applied Country:</b><br>
<select name="markappliedcountry[]" style="width:20%" multiple >
<option value="Afghanistan">Afghanistan</option>
...
<option value="Zimbabwe">Zimbabwe</option>
</select><br><br>
<br><br><br>
<input type="submit" value="Submit">
</form>
</div>
<script>
function disableNo() {
document.getElementById("marknocountry").required = false;
document.getElementById("marknodate").required = false;
}
function disableYes() {
document.getElementById("markyescountry").required = false;
document.getElementById("markyesdate").required = false;
document.getElementById("markyesproof").required = false;
}
</script>
</body>
</html>
答案 0 :(得分:2)
使用"'NULL'"
代替"NULL"
。这将在所需位置插入"NULL"
字符串。基本上$Preference2 = "NULL";
应为$Preference2 = "'NULL'";
你在做什么是这样的:
insert into myTb (val) values (NULL);
你应该做的是:
insert into myTb (val) values ('NULL');