php 将输入选择选项存储到数据库中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32219928/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:51:03  来源:igfitidea点击:

Store input select option into database

phpsqlhtml

提问by ABHI_SINGH

I want to save the input select option in the database with value of the option. But nothing is getting saved. What is wrong? I am saving the value as varchar (10)in the database.

我想使用该选项的值将输入选择选项保存在数据库中。但什么都没有得到拯救。怎么了?我将值保存varchar (10)在数据库中。

FORM

形式

<form name="stdntdetails" action="study.php" method="post">
    <select name="department">
        <option value="IT">Information Technology</option>
        <option value="IS">Information System</option>
        <option value="CS">Computer Science</option>
    </select>
    <input type="submit" id="loginbtn" />
</form>

PHP

PHP

<?php
require_once("navig.php");
require_once('connect.php');
$dbb = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
  or die('Error communicating to MySQL server.');
if(isset($_POST['loginbtn'])){
  if(isset($_POST['department'])){
    $department=$_POST['department'];
    $querye = "INSERT INTO tbl_name(department) ".
                        "VALUES ('$department')";
    $sql=mysqli_query($dbb,$querye);
    mysqli_close($dbb);                     
  }
}
?>

回答by Syed mohamed aladeen

change your query like this

像这样更改您的查询

$querye = "INSERT INTO tbl_name(department) VALUES ('$department')";

and

if(isset($_POST['submit']))

and

<input type="submit" id="loginbtn" value="submit" />

回答by Rohan

Change this query

更改此查询

$querye = "INSERT INTO tbl_name(department) ".
                        "VALUES ('$department')";

to

$querye = "INSERT INTO tbl_name(department) 
                        VALUES ('$department')";

Also try

也试试

<input type="submit" id="loginbtn" name="loginbtn" value="Submit" />

回答by Acharya Anurag

You have several errors in your code. Try the following:

您的代码中有几个错误。请尝试以下操作:

FORM

形式

<form name="stdntdetails" action="study.php" method="post">
    <select name="department">
        <option value="IT">Information Technology</option>
        <option value="IS">Information System</option>
        <option value="CS">Computer Science</option>
    </select>
    <input type="submit" id="loginbtn" name="submit"/>
</form>

PHP

PHP

<?php
require_once("navig.php");
require_once('connect.php');
$dbb = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
  or die('Error communicating to MySQL server.');
if(isset($_POST['submit'])){
  if(isset($_POST['department'])){
    $department=$_POST['department'];
    $querye = "INSERT INTO tbl_name (department) VALUES ('$department')";
    $sql=mysqli_query($dbb,$querye);
    mysqli_close($dbb);                     
  }
}
?>

I tested the code and it works just fine. The query sent to MySQL is:

我测试了代码,它工作得很好。发送到 MySQL 的查询是:

INSERT INTO tbl_name (department) VALUES ('IT')

INSERT INTO tbl_name (department) VALUES ('IT')

回答by HEMANT SUMAN

make your query like -

让你的查询像 -

"INSERT INTO tbl_name(department) VALUES ('$department')";

and set the name of submit button

并设置提交按钮的名称

<input type="submit" id="loginbtn" name="loginbtn" />

回答by kim de castro

Try to echo your $_POST("department"); if display the right value then there's a problem with the query.

尝试回显您的 $_POST("department"); 如果显示正确的值,则查询有问题。

try this:

尝试这个:

$querye = "INSERT INTO tbl_name(department) VALUES ('$department')";