html按鍵開關如何提交我想需要的值到數據庫
問題描述
<!DOCTYPE html><html><head> <meta charset="UTF-8"> <title>Document</title> <script type="text/javascript" src="js/jquery-1.12.4.min.js"></script> <style type="text/css">body>div{height: 40px;}label{ display: inline-block; width: 50px; height: 22px; background:#dc3545; border-radius: 30px ; transition: background-color .6s linear;}/*在標簽的前面插入一小圓形*/label::after{ content: "關";width: 17px; height: 17px; background-color:#FFFFFF;color:#dc3545;display: inline-block;text-align: center;line-height: 17px;position: relative;left:3px; transition: transform.6s linear; border-radius: 50%;}/*獲得復選框后面的第一個兄弟元素label*/input[type=checkbox]:checked+label{ background-color:#28a745;}input[type=checkbox]:checked+label::after{ content: "開";color:#28a745;transform: translate(27px);/*向右移動27px*/} </style></head><body> <form action="1.php" method="post" >
<input name="yfkg" type="submit" value="yfkg" onclick="submit"($yfkg=000)>
<input name="tfkg" type="submit" value="tfkg" onclick="submit"($tfkg=000)>
</form></html> ```php<?php header("Content-type: text/html; charset=utf-8"); //建立數據庫連接*/ $servername = 'localhost'; $username ='root'; $password = '123'; $dbname = "nongyedapeng"; $conn = new mysqli($servername, $username, $password, $dbname); function execute_sql($link, $database, $sql){ mysqli_select_db($link, $database) or die("打開數據庫失敗: " . mysqli_error($link)); $result = mysqli_query($link, $sql); return $result; } switch ($_POST["submit"]){case "yfkg": if ($conn){ $sql = "UPDATE control SET yfkg=111"; $conn1= execute_sql($conn, "nongyedapeng", $sql); echo "
設計閾值成功
";} else{echo "請重新輸入
";} break;case "tfkg": if ($conn){ $sql = "UPDATE control SET yfkg=000"; $conn1= execute_sql($conn, "nongyedapeng", $sql); echo "設計閾值成功
";} else{echo "請重新輸入
";} break;default:}?>問題解答
回答1:你這個PHP代碼完全不能執行,你鏈接數據那里都寫得是錯誤,你有類?為什么要new mysqli ? 再說你鏈接數據庫的函數也不對,人家是 mysqli_connect()
回答2:你應該是想要在表單中填入數據,然后將表單中的數據通過一個按鈕提交到數據庫中對吧?
根據你這個代碼,你需要在這個html的目錄下新建一個1.php,將你的php代碼放入其中即可
