gpt4 book ai didi

php - 将数据从一个 PHP 文件发布到另一个文件会出错

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:38 24 4
gpt4 key购买 nike

我有两个 php 文件,一个文件将数据提交到第二个文件,以便对 mysql 数据库进行更新操作。

下面是提交数据文件的代码

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
$server_name="localhost";


// Create connection
$con = new mysqli($server_name, $username, $password, $db_name , 3306);

if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}

// get value of id that sent from address bar
$id=$_GET['id'];

// Retrieve data from database
$sql="SELECT * FROM $tbl_name WHERE id='$id'";

$result = $con->query($sql);

$rows = $result->fetch_assoc();

?>

<table width="400" border="0" cellspacing="1" cellpadding="0">
<tr>
<form name="form1" method="post" action="update_ac.php">
<td>
<table width="100%" border="0" cellspacing="1" cellpadding="0">
<tr>
<td>&nbsp;</td>
<td colspan="3"><strong>Update data in mysql</strong> </td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
<td align="center">&nbsp;</td>
</tr>
<tr>
<td align="center">&nbsp;</td>
<td align="center"><strong>Name</strong></td>
<td align="center"><strong>Lastname</strong></td>
<td align="center"><strong>Email</strong></td>
</tr>
<tr>
<td>&nbsp;</td>
<td align="center">
<input name="name" type="text" id="name" value="<?php echo $rows['name']; ?>">
</td>
<td align="center">
<input name="lastname" type="text" id="lastname" value="<?php echo $rows['lastname']; ?>" size="15">
</td>
<td>
<input name="email" type="text" id="email" value="<?php echo $rows['email']; ?>" size="15">
</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>
<input name="id" type="hidden" id="id" value="<?php echo $rows['id']; ?>">
</td>
<td align="center">
<input type="submit" name="Submit" value="Submit">
</td>
<td>&nbsp;</td>
</tr>
</table>
</td>
</form>
</tr>
</table>

<?php
// close connection
$con->close();
?>

更新操作的第二个文件如下所示

<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="test"; // Database name
$tbl_name="test_mysql"; // Table name
$server_name="localhost";

// Create connection
$con = new mysqli($server_name, $username, $password, $db_name , 3306);

if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}

// update data in mysql database
$sql="UPDATE $tbl_name SET name='$name', lastname='$lastname', email='$email' WHERE id='$id'";
$result=$con->query($sql);

// if successfully updated.
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

错误页面如下所示。 enter image description here

解决问题的建议

最佳答案

嗯,您还没有设置这些值,这就是它出现错误的原因。首先,您必须包装您的第二个文件以检查它是否已提交表单。然后在里面设置那些变量。

<?php
if(isset($_POST['Submit'])) {
$name = $_POST['name'];
$lastname = $_POST['lastname'];
$email = $_POST['email'];
$id = $_POST['id'];

// rest of your code goes here
}

关于php - 将数据从一个 PHP 文件发布到另一个文件会出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31397666/

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