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代码放入其中即可