问题: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)时未保存输入的值
基础教程推荐
猜你喜欢
- IE8/IE9下Ajax缓存问题 2023-01-20
- Ajax表单异步上传文件实例代码(包括文件域) 2023-01-21
- Ajax中post方法直接返回以0开头数字出错问题分析 2023-01-31
- 四步轻松实现ajax发送异步请求 2023-02-14
- Vue自学之路5-vue模版语法(v-text,v-html,v-pre) 2023-10-08
- vue 组件之间数据传递10种 2023-10-08
- vue websocket 发送消息 2023-10-08
- Vue 配置脚手架CLI 3 2023-10-08
- Vue-vue-router(二)嵌套路由 2023-10-08
- JavaScript操作元素教你改变页面内容样式 2023-08-12