gpt4 book ai didi

php - 如何将表单数据传递到另一个页面和数据库?

转载 作者:行者123 更新时间:2023-11-30 21:59:54 24 4
gpt4 key购买 nike

实际上,我正在尝试根据用户建议的名称创建一个表,并将数据插入该表,这也是根据用户的建议。

我有两个 php 文件:CreateTable.phpEnterData.php

这是我的 CreateTable.php 代码:

<?php 

$conn = new mysqli("localhost","root","","mywebsite");

if (isset($_POST['tbButton'])) {
$qry = "Create Table ".$_POST['tableName']."(firstname varchar(25),lastname varchar(25));";
$res = mysqli_query($conn,$qry);
if ($res) {
echo "Table Created!";
}
else{
die("query failed!");
}
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Create Table</title>
</head>
<body>
<form action="EnterData.php" method="post">
<p><input type="text" name="tableName" placeholder="Enter Table Name..."></p>
<p><input type="submit" name="tbButton"></p>
</form>
</body>
</html>

这是我的 EnterData.php 代码:

<?php 
$tbname = $_POST['tableName'];
$conn = new mysqli("localhost","root","","mywebsite");

if (isset($_POST['dataButton'])) {

$qry = "Insert into ".$tbname."(firstname,lastname) values('".$_POST['firstname']."','".$_POST['lastname']."');";
$res = mysqli_query($conn,$qry);
if ($res) {
echo "Data Inserted!";
}
else{
die("query failed!");
}
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Create Table</title>
</head>
<body>
<form action="" method="post">
<p><input type="text" name="firstname" placeholder="Enter First Name..."></p>
<p><input type="text" name="lastname" placeholder="Enter Last Name..."></p>
<p><input type="submit" name="dataButton"></p>
</form>
</body>
</html>

问题 是当我编写 action="EnterData.php" 表时不会在数据库中创建但表单值会传递给 'EnterData'文件。当我编写 action="CreateTable.php" 时,表是在数据库中创建的,但值不会传递给 'EnterData' 文件。我也想将值传递给 EnterData 文件和数据库。


这是我第一次尝试使用 stackoverflow,希望我能很好地解释我的问题

最佳答案

你可以通过get方法传递你的表名

创建表格.php

<?php 

$conn = new mysqli("localhost","root","","mywebsite");
$tableName = $_POST['tableName'];

if (isset($_POST['tbButton'])) {
$qry = "Create Table ".$tableName ."(firstname varchar(25),lastname varchar(25));";
$res = mysqli_query($conn,$qry);
if ($res) {
header("Location: EnterData.php?tableName=".$tableName);
}
else{
die("query failed!");
}
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Create Table</title>
</head>
<body>
<form action="CreateTable.php" method="post">
<p><input type="text" name="tableName" placeholder="Enter Table Name..."></p>
<p><input type="submit" name="tbButton"></p>
</form>
</body>
</html>

输入数据.php

<?php 
$tbname = $_GET['tableName'];
$conn = new mysqli("localhost","root","","mywebsite");

if (isset($_POST['dataButton'])) {

$qry = "Insert into ".$tbname."(firstname,lastname) values('".$_POST['firstname']."','".$_POST['lastname']."');";
$res = mysqli_query($conn,$qry);
if ($res) {
echo "Data Inserted!";
}
else{
die("query failed!");
}
}

?>
<!DOCTYPE html>
<html>
<head>
<title>Create Table</title>
</head>
<body>
<form action="EnterData.php?tableName=<?php echo $tbname;?>" method="post">
<p><input type="text" name="firstname" placeholder="Enter First Name..."></p>
<p><input type="text" name="lastname" placeholder="Enter Last Name..."></p>
<p><input type="submit" name="dataButton"></p>
</form>
</body>
</html>

关于php - 如何将表单数据传递到另一个页面和数据库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43730223/

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