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

时间:2014-11-29 23:54:24

标签: php html mysql forms

问题: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>

用于连接SQL的PHP​​代码,插入输入值

<?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);
?>

2 个答案:

答案 0 :(得分:1)

试试这个

<?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);

?>

答案 1 :(得分:0)

可能是因为default form posting method is GET

$_POST更改为$_GET或将method="POST"添加到表单标记。