下拉其他值不保存到数据库 [英] dropdown other value not saving to database
问题描述
< script type =text / javascript>
function showfield(name){
if(name =='Other')document.getElementById('div1')。innerHTML ='other:< input type =textname =other />';
else document.getElementById('div1')。innerHTML ='';
}
< / script>
< html>
< head>
< meta name =viewportcontent =width = device-width,initial-scale = 1.0>
< link href =css / bootstrap.min.css =stylesheet>
< title>插入< /标题>
< / head>
< body>
< h1>将数据插入到Db< / h1>
< form action =teachersubmit.phpmethod =postid =form>
< input type =hiddenname =submittedvalue =true/>
< label>学校编号:< input type =textname =scode/>< / label>
< label>分类:< input type =textname =category/>< / label>
< label>名称:< input type =textname =sname/>< / label>
< label>地址:< input type =textname =sadd>< / label>< br>
< label>名称:< select name =genderid =gender>
< option value =male> male< / option>
< option value =女性>女性< / option>
< option value =其他>其他< / option>
< / select>< / label>
< div id =div1>< / div>
< button type =submitform =formvalue =Submit> Submit< / button>
< / form>
if(isset($ _ POST ['submitted'])){
$ con = mysqli_connect(localhost,root,,school);
if(mysqli_connect_errno())
{
echo无法连接到MySQL:。 mysqli_connect_error();
$ b if(!empty($ _ POST ['other']))
{
$ sname = $ _ POST [ 'SNAME'];
$ scode = $ _ POST ['scode'];
$ category = $ _ POST ['category'];
$ sadd = $ _ POST ['sadd'];
$ gender = $ _ POST ['other'];
$ b $ sqlinsert =INSERT INTO form1(sname,scode,category,sadd,gender)VALUES('$ sname','$ scode','$ category','$ sadd','$性别');
if(!mysqli_query($ con,$ sqlinsert)){
die('not inserted');
}}
else {
$ sname = $ _ POST ['sname'];
$ scode = $ _ POST ['scode'];
$ category = $ _ POST ['category'];
$ sadd = $ _ POST ['sadd'];
$ gender = $ _ POST ['gender'];
$ b $ sqlinsert =INSERT INTO form1(sname,scode,category,sadd,gender)VALUES('$ sname','$ scode','$ category','$ sadd','$性别');
if(!mysqli_query($ con,$ sqlinsert)){
die('not inserted');
}
}
解决了.....谢谢所有你帮助我只是把这个条件,如果该隐藏领域是空的,然后采取下拉值,如果没有,然后采取文本框的值......干杯:)
这是数据库结构:
sname varchar(100)
category varchar(100)
sadd varchar (100)
scode varchar(20)
gender varchar(100)
< option> male< / option>
<选项>女性< / option>
这将是
<选项值=男性>男性< / option>
< option value =女性>女性< / option>
要检查查询中的错误,请阅读
http://php.net/manual/en/mysqli.error.php
您的代码已经打开,用于sql注入。阅读此内容以防止形成它
EDITED
数据库中没有插入数据,因为 sname,scode,category,sadd
是整数类型
你试图插入字符
更改 sname,scode,category,sadd
作为 varchar
归档到您的数据库中
<script type="text/javascript">
function showfield(name){
if(name=='Other')document.getElementById('div1').innerHTML='other: <input type="text" name="other" />';
else document.getElementById('div1').innerHTML='';
}
</script>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link href = "css/bootstrap.min.css" rel = "stylesheet">
<title> Insert</title>
</head>
<body>
<h1>Insert Data In to Db</h1>
<form action="teachersubmit.php" method="post" id="form">
<input type="hidden" name="submitted" value ="true" />
<label> School Code: <input type="text" name="scode" /></label>
<label> Category: <input type="text" name="category" /></label>
<label> name: <input type="text" name="sname" /></label>
<label> Address: <input type="text" name="sadd"></label><br>
<label> name: <select name="gender" id="gender">
<option value="male">male</option>
<option value="female">female</option>
<option value="Other">Other</option>
</select></label>
<div id="div1"></div>
<button type="submit" form="form" value="Submit">Submit</button>
</form>
if (isset($_POST ['submitted']) ){
$con = mysqli_connect("localhost","root","","school");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
if(!empty($_POST['other']))
{
$sname=$_POST['sname'];
$scode=$_POST['scode'];
$category=$_POST['category'];
$sadd=$_POST['sadd'];
$gender=$_POST['other'];
$sqlinsert= "INSERT INTO form1 (sname,scode,category,sadd,gender) VALUES ('$sname' , '$scode', '$category', '$sadd', '$gender')";
if(!mysqli_query($con, $sqlinsert)){
die ('not inserted');
}}
else{
$sname=$_POST['sname'];
$scode=$_POST['scode'];
$category=$_POST['category'];
$sadd=$_POST['sadd'];
$gender=$_POST['gender'];
$sqlinsert= "INSERT INTO form1 (sname,scode,category,sadd,gender) VALUES ('$sname' , '$scode', '$category', '$sadd', '$gender')";
if(!mysqli_query($con, $sqlinsert)){
die ('not inserted');
}
}
Solved ..... thanks all of u for helping i just put the condition that if that hidden field is empty then take the dropdown value and if not then take the textbox value ......cheers :)
This is the database structure:
sname varchar(100)
category varchar(100)
sadd varchar(100)
scode varchar(20)
gender varchar(100)
Value
is missing in option
that's the reason dropdown value not posting form form and not save into database
<option>male</option>
<option>female</option>
It would be
<option value="male">male</option>
<option value="female">female</option>
To check error in your query read
http://php.net/manual/en/mysqli.error.php
Your code is open for sql injection . Read this to prevent it form it
How can I prevent SQL injection in PHP?
EDITED
Data is not inserted because sname,scode,category,sadd
are integer type
in database and you are trying to insert character
ALter sname,scode,category,sadd
filed as varchar
into your database
这篇关于下拉其他值不保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!