问题:insert.php连接正常,但是当我在html表单上点击’save’时,只插入空值(”).我正在尝试插入的文字我没有保存.在某个地方没有建立连接,数据丢失但我无法确切地知道在哪里.有帮助吗?HTML插入表单(收集2个参数的数...
问题:insert.php连接正常,但是当我在html表单上点击’save’时,只插入空值(”).我正在尝试插入的文字我没有保存.在某个地方没有建立连接,数据丢失但我无法确切地知道在哪里.有帮助吗?
HTML插入表单(收集2个参数的数据,’用户’和’线程’)
<form action="insert.php" method="post">
user: <input type="text" name="user"><br>
thread: <input type="text" name="thread"><br>
<input type="submit" value="Save">
</form>
PHP代码连接到SQL,插入输入的值
<?php
$user = $_POST['user'];
$thread = $_POST['thread'];
$servername = "##.##.###";
$username = "harwoodjp";
$password = "~";
$dbname = "332";
//create connection
$conn = new mysqli($servername, $username, $password, $dbname);
//check connection
if ($conn->connect_error) {
die("SQL (☒)<br/> " . $conn->connect_error);
}
echo "SQL (☑) <br/>";
$sql = mysql_connect($servername,$username,$password);
mysql_connect($servername,$username,$password);
mysql_select_db("332project");
//insert values
$insert_query = "INSERT INTO test1(user,thread) VALUES ('$user', '$thread')";
mysql_query($insert_query);
echo "<script>window.location='select.php'</script>"; //select.php displays the full table
//close MySQL
mysql_close($sql);
?>
解决方法:
试试这个
<?php
$user = $_POST['user'];
$thread = $_POST['thread'];
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";
//create connection
$conn = mysql($servername, $username, $password, $dbname);
//check connection
if ($conn->connect_error) {
die("SQL (☒)<br/> " . $conn->connect_error);
}
echo "SQL (☑) <br/>";
$sql = mysql_connect($servername,$username,$password);
mysql_select_db("db");
//insert values
$insert_query = "INSERT INTO test1(user,thread) VALUES ('$user', '$thread')";
mysql_query($insert_query);
echo "<script>window.location='select.php'</script>"; //select.php displays the full table
//close MySQL
mysql_close($sql);
?>
沃梦达教程
本文标题为:从html表单插入数据到sql数据库(html,php,sql)时未保存输入的值
猜你喜欢
- 详解CSS不定宽溢出文本适配滚动 2022-11-13
- AjaxFileUpload结合Struts2实现多文件上传(动态添加文件上传框) 2023-02-14
- Ajax的jsonp方式跨域获取数据的简单实例 2022-12-28
- Ajax获取数据然后显示在页面的实现方法 2023-01-21
- springboot+vue3搭建项目 2023-10-08
- vue文章下载功能实现 2023-10-08
- Ajax写分页查询(实现不刷新页面) 2023-01-31
- HTML怎么设置下划线?html文字加下划线方法 2022-09-21
- uniapp开发安卓App实现高德地图路线规划导航功能的全过程 2022-10-22
- PHP Ajax实现表格实时编辑 2023-02-01