gpt4 book ai didi

PHP 表单在 mySQL 中不显示任何内容

转载 作者:行者123 更新时间:2023-11-29 07:19:26 24 4
gpt4 key购买 nike

我试图弄清楚为什么我的 php 表单没有将字段中的数据放入 mySQL 数据库中。我一直试图解决这个问题,但已经走进了死胡同。

当使用每个字段的值进行硬编码时,我的插入 $sql 工作正常,但当我尝试使用从 php 表单输入的字段时,效果不佳。

当我点击提交时,我没有收到任何错误,但是当我检查 mySQL 以查看它是否添加了另一个所有者时,没有任何显示。

如果有人能帮我解决这个问题,我将非常感激。

顺便问一下,我的 $sql insert 语句中的引号正确吗?

    <head>
<style>
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 5px;
}
</style>
</head>
<body>
<?php #index.php for Assignment 10
$page_title = 'Assignment 10 for Marina Database';
include('header.html');
require('dbConn.php');
echo '<h1> Please enter the following fields:</h1>';


if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$OwnerNum=$_POST['OwnerNum'];
$LastName=$_POST['LastName'];
$FirstName=$_POST['FirstName'];
$Address=$_POST['Address'];
$City=$_POST['City'];
$State=$_POST['State'];
$Zip=$_POST['Zip'];

//echo test;
try {
$sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, State, Zip) VALUES
('".$OwnerNum."', '".$LastName."', '".$FirstName."', '".$Address."', '".$City."', '".$State."', '".$Zip."')";

//this works when hard coded
/*$sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, `State, Zip) VALUES ('XR34', 'Patel', 'John', '342 Picardy lane', 'Wheeling', 'IL', '60018')"; */`
$conn->exec($sql);

//echo $OwnerNum, $LastName, $FirstName, $Address, $City, $State, $Zip;
}
catch (PDOException $e)
{
echo 'Error: '.$e->getMessage();
} //end catch



}



if (isset($_POST['submit']))
{
$stmt = $conn->prepare("select* from Owner");
$stmt->execute();

$result = $stmt->setFetchMode(PDO::FETCH_ASSOC);

echo "<table style='border: solid 1px black;'>";
echo "<tr><th>OwnerNum</th><th>LastName</th><th>FirstName</th><th>Address</th><th>City</th><th>State</th><th>Zip</th></tr>";


class TableRows extends RecursiveIteratorIterator
{
function __construct($it) {
parent::__construct($it, self::LEAVES_ONLY);
}

function current() {
return "<td style='width:150px;border:1px solid black;'>" . parent::current(). "</td>";
}

function beginChildren() {
echo "<tr>";
}

function endChildren() {
echo "</tr>" . "\n";
}

}

foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) as $k=>$v)
{
echo $v;

}

$conn = null;
echo "</table>";
}

?>
<form name="createOwner" action="Assignment10newowner.php" method="POST">
<table style="width:100%">
<tr>
<td>Owner Number:</td>
<td><input type="text" name="OwnerNum"></td>
</tr>

<td>Last Name:</td>
<td><input type="text" name="LastName"></td


</tr>
<tr>
<td>First Name:</td>
<td><input type="text" name="FirstName"></td


</tr>
<tr>
<td>Address:</td>
<td><input type="text" name="Address"></td


</tr>
<tr>
<td>City:</td>
<td><input type="text" name="City"></td


</tr>
<tr>
<td>State:</td>
<td><input type="text" name="State"></td


</tr>
<tr>
<td>Zip:</td>
<td><input type="text" name="Zip"></td


</tr>
</table>
<br>
<br>
<input type="submit" value="Submit">
</form>
</body>

最佳答案

将 $_POST 值分配给类似这样的变量

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

$OwnerNum=$_POST['OwnerNum'];
$LastName=$_POST['LastName'];
$FirstName=$_POST['FirstName'];
$Address=$_POST['Address'];
$City=$_POST['City'];
$State=$_POST['State'];
$Zip=$_POST['Zip'];

try {
$sql = "INSERT INTO Owner (OwnerNum, LastName, FirstName, Address, City, State, Zip) VALUES
('".$OwnerNum."', '".$LastName."', '".$FirstName."', '".$Address."', '".$City."', '".$State."', '".$Zip."')";
$conn->exec($sql);
$select_owner = "SELECT * FROM Owner";
?>
<table>
<tr>
<td>OwnerNum</td>
<td>FirstName</td>
<td>LastName</td>
<td>Address</td>
<td>City</td>
</tr>
<?php
$conn->prepare($select_owner );
$result = $conn->fetchAll();
if (count($result)) {

foreach($result as $owner){

?>
<tr>
<td><?=$owner['OwnerNum'];?></td>
<td><?=$owner['FirstName'];?></td>
<td><?=$owner['LastName'];?></td>
<td><?=$owner['Address'];?></td>
<td><?=$owner['City'];?></td>
</tr>
<?php
}
}
else
{
?>
<tr>
<td colspan="5">No Onwers Found</td>
</tr>
<?php
}
unset($result);
unset($select_owner);
unset($conn);
}
catch (PDOException $e)
{
echo 'Error: '.$e->getMessage();
} //end catch

}

关于PHP 表单在 mySQL 中不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36709742/

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