PHP从下拉列表中更新数据

时间:2016-07-20 07:21:53

标签: php mysql

如何使用php将所选数据从下拉列表更新到mysql(mysql update)?

<form>
<select name="maintenance_mode">
  <option>Maintenance On</option>
  <option>Maintenance Off</option>
</select>
<button type="submit" name="update">Apply</button>
</form>

我将列类型设置为tinyint(1)。基本上只需更新0或1。

6 个答案:

答案 0 :(得分:0)

您可以根据您的选择传递0或1。请参考以下代码

<form action = "updateMode.php" action = "POST">
<select name="maintenance_mode">
  <option value = "1">Maintenance On</option>
  <option value = "0">Maintenance Off</option>
</select>
<button type="submit" name="update">Apply</button>
</form>

这是updateMode.php

$mode = $_POST['maintenance_mode'];

//My SQL DB update logic should be written here

答案 1 :(得分:0)

1)在选项

中设置值
SparkContext.parallelize

2)在php中使用更新查询

<form method="post" action="filename.php">
<select name="maintenance_mode">
  <option value="1">Maintenance On</option>
  <option value="0">Maintenance Off</option>
</select>
<button type="submit" name="update">Apply</button>
</form>

答案 2 :(得分:0)

<form method="POST"> <!-- IF YOU WANT IT TO POST TO ANOTHER FILE THEN USE: action="filename.php" -->
<select name="maintenance_mode">
  <option value="1">Maintenance On</option>
  <option value="off">Maintenance Off</option>
</select>
<button type="submit" name="update">Apply</button>
</form>
<?php
if(isset($_POST['update'])){
$maintenance = $_POST['maintenance_mode'];

//UPDATE DB HERE WITH VALUE

}
?>

我假设您在上面的代码中想要在数据库中进行更新。我标记了你可以执行它的行,它应该更新它的值是来自变量的值&#34; maintenance&#34;

答案 3 :(得分:0)

value元素添加option标记。

<option value="1">Maintenance On</option>
<option value="0">Maintenance Off</option>

然后为action元素添加methodform标记:

<form action="update.php" method="POST"> <!-- REPLACE FILE/DIRECTORY DEPENDING ON WHERE YOU PROCESS THIS SUBMITTED FORM -->

然后在update.php

<?php

    /* ESTABLISH YOUR CONNECTION */
    $con = new mysqli("localhost", "my_user", "my_password", "world");

    /* check connection */
    if (mysqli_connect_errno()) {
        printf("Connect failed: %s\n", mysqli_connect_error());
        exit();
    }

    $stmt = $con->prepare("UPDATE table SET maintenance = ?"); /* PREPARE YOUR QUERY */
    $stmt->bind_param("i", $_POST["maintenance_mode"]); /* BIND THIS PASSED ON DATA TO YOUR QUERY */
    $stmt->execute(); /* EXECUTE QUERY */
    $stmt->close(); /* CLOSE PREPARED STATEMENT */

    header("LOCATION:form.php"); /* CHANGE THE FILE NAME DEPENDING ON THE FILENAME OF YOUR FORM */

?>

答案 4 :(得分:0)

您应该在选择0/1

中使用options <?php if(isset($_POST['update'])){ $connct = mysqli_connect('localhost','root','password','database'); $value = $_POST['maintenance_mode']; $result = mysqli_query($connct,"UPDATE table_name SET filedname = '$value'"); } ?> <form method="post" action=""> <select name="maintenance_mode"> <option value="1">Maintenance On</option> <option value="0">Maintenance Off</option> </select> <input type="submit" name="update" value="update"> </form>
add_library(math SHARED ${MATH_SOURCES} ${SIMPLE_FUNCTION_SOURCES} ${ADVANCED_FUNCTION_SOURCES})

答案 5 :(得分:0)

我假设您已经建立了数据库连接。提到的代码将起作用

<?php

if(isset($_POST['update'])){    
    $mode = $_POST['maintenance_mode'];
    $sql = "update tablename set maintenance_mode = '".$mod."' ";
    // tablename is name of mysql table where you want to update and maintenance_mode is name of table field to which you want to update.
    mysqli_query($sql);
}
?>

<form method="POST" action="">
<select name="maintenance_mode">
  <option value = "1">Maintenance On</option>
  <option value = "0">Maintenance Off</option>
</select>
<button type="submit" name="update">Apply</button>
</form>

注意:mysql会进一步折旧,请尝试使用PDO