沃梦达 / IT编程 / 前端开发 / 正文

从html表单插入数据到sql数据库(html,php,sql)时未保存输入的值

问题: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 (&#9746)<br/> " . $conn->connect_error);
}
echo "SQL (&#9745) <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 (&#9746)<br/> " . $conn->connect_error);
}
echo "SQL (&#9745) <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)时未保存输入的值