使用 php 将数据输入 mysql 数据库
Entering data into mysql database using php
客户将填写表格并输入他们希望将 CSV 导出到的路径。使用 php 的顶部部分输入路径(如下):
<form action="register.php" method="post">
Enter file pathway where CSV will be saved: <input type="text" name="username" required="required"/> <br/>
<input type="submit" value="Enter"/>
</form>
</body>
我想创建一个名为 pathway 的变量。目前我可以将文本输入到 mysql 数据库的正确行中(我可以在数据库中打印 John),但不能输入到表单中的正确文本(即 $pathway)。
我想创建一个变量,因为在数据库中保存路径后,我还想在 export.php.
中使用它
我假设我也需要这样的东西:
if($_SERVER["REQUEST_METHOD"] == "POST"){
$pathway = mysql_real_escape_string($_POST['pathway']);
// but i can't seem to piece it altogether.
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "first_db";
$table_users = $row['pathway'];
$pathway = "pathway";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO users (pathway)
VALUES ('John')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
您的数据库用户名是 Null
将$username = "";
更改为$username = "root";
您输入的字段名称是用户名
将其更改为 pathway
以便 $_POST['pathway']
起作用
<form action="register.php" method="post">
Enter file pathway where CSV will be saved:
<input type="text" name="pathway" required="required"/> <br/>
<input type="submit" value="Enter"/>
</form>
首先,您已将 'username' 作为用于键入路径的字段的名称,因此将其重命名为 'pathway'。
我不知道我是否理解你,但你只是想阅读发布的内容吗?
尝试类似的东西:
$pathway = $_POST['pathway']
我强烈建议使用面向对象的风格
$conn = new mysqli...
然后
mysqli->prepare(), mysqli->bind_param(), mysqli->execute()
有了这个你就不用再处理 mysqli_real_escape_string 等等
这应该可以,如果不行,请检查您的用户名和密码...
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "first_db";
$pathway = $_POST['username']; username - is the name of your input.
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO users (pathway)
VALUES ('$pathway')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
客户将填写表格并输入他们希望将 CSV 导出到的路径。使用 php 的顶部部分输入路径(如下):
<form action="register.php" method="post">
Enter file pathway where CSV will be saved: <input type="text" name="username" required="required"/> <br/>
<input type="submit" value="Enter"/>
</form>
</body>
我想创建一个名为 pathway 的变量。目前我可以将文本输入到 mysql 数据库的正确行中(我可以在数据库中打印 John),但不能输入到表单中的正确文本(即 $pathway)。 我想创建一个变量,因为在数据库中保存路径后,我还想在 export.php.
中使用它我假设我也需要这样的东西:
if($_SERVER["REQUEST_METHOD"] == "POST"){
$pathway = mysql_real_escape_string($_POST['pathway']);
// but i can't seem to piece it altogether.
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "first_db";
$table_users = $row['pathway'];
$pathway = "pathway";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO users (pathway)
VALUES ('John')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
您的数据库用户名是 Null
将$username = "";
更改为$username = "root";
您输入的字段名称是用户名
将其更改为 pathway
以便 $_POST['pathway']
起作用
<form action="register.php" method="post">
Enter file pathway where CSV will be saved:
<input type="text" name="pathway" required="required"/> <br/>
<input type="submit" value="Enter"/>
</form>
首先,您已将 'username' 作为用于键入路径的字段的名称,因此将其重命名为 'pathway'。 我不知道我是否理解你,但你只是想阅读发布的内容吗? 尝试类似的东西:
$pathway = $_POST['pathway']
我强烈建议使用面向对象的风格
$conn = new mysqli...
然后
mysqli->prepare(), mysqli->bind_param(), mysqli->execute()
有了这个你就不用再处理 mysqli_real_escape_string 等等
这应该可以,如果不行,请检查您的用户名和密码...
<?php
$servername = "localhost";
$username = "";
$password = "";
$dbname = "first_db";
$pathway = $_POST['username']; username - is the name of your input.
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn)
{
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO users (pathway)
VALUES ('$pathway')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
}
else
{
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>