gpt4 book ai didi

php - 将 html 表单中的数据插入 sql 数据库(html、php、sql)时,输入的值未保存

转载 作者:可可西里 更新时间:2023-11-01 07:39:30 25 4
gpt4 key购买 nike

问题:insert.php 连接正常,但仅在我点击 html 表单上的“保存”时插入空值 ('')。我尝试插入的键入文本未保存。某个地方没有建立连接并且数据丢失但我无法弄清楚确切的位置。有帮助吗?

HTML 插入表单(收集 2 个参数的数据,'user' 和 'thread')

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

最佳答案

试试这个

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

?>

关于php - 将 html 表单中的数据插入 sql 数据库(html、php、sql)时,输入的值未保存,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27208496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com