我是PHP的新手,我一直在创建一个webapp,允许我在我的数据库中创建一个所有者,然后允许我阅读所有者。
我一直收到以下错误:
注意:未定义的变量:第26行的C:\ xampp \ htdocs \ public \ create.php中的new_owner
警告:array_keys()期望参数1为数组,在第26行的C:\ xampp \ htdocs \ public \ create.php中给出null
警告:implode():在第26行的C:\ xampp \ htdocs \ public \ create.php中传递的参数无效
注意:未定义的变量:第27行的C:\ xampp \ htdocs \ public \ create.php中的new_owner
警告:array_keys()期望参数1为数组,在第27行的C:\ xampp \ htdocs \ public \ create.php中给出null
警告:implode():在第27行的C:\ xampp \ htdocs \ public \ create.php中传递的参数无效
致命错误:未捕获错误:在C:\ xampp \ htdocs \ public \ create.php中调用boolean上的成员函数execute():31堆栈跟踪:在C:\ xampp \ htdocs中抛出#0 {main}第31行\ public \ create.php
我的代码如下:
<?php
if (isset($_POST['submit']))
{
require "config.php";
require "common.php";
try
{
$connection = new mysqli($host, $user, $password, $dbname, $port, $socket);
$new_user = array(
"OwnerNum" => $_POST['OwnerNum'],
"LastName" => $_POST['LastName'],
"Address" => $_POST['Address'],
"City" => $_POST['City'],
"State" => $_POST['State'],
"PostalCode" => $_POST['PostalCode']
);
$sql = sprintf(
"INSERT INTO %s (%s) values (%s)",
"owners",
implode(", ", array_keys($new_owner)),
":" . implode(", :", array_keys($new_owner))
);
$statement = $connection->prepare($sql);
$statement->execute($new_owner);
}
catch(PDOException $error)
{
echo $sql . "<br>" . $error->getMessage();
}
}
?>
<?php require "templates/header.php"; ?>
<?php
if (isset($_POST['submit']) && $statement)
{ ?>
<blockquote><?php echo $_POST['OwnerNum']; ?> successfully added.
</blockquote>
<?php
} ?>
<h2>Add an Owner</h2>
<form method="post">
<label for="OwnerNum">OwnerNum</label>
<input type="text" name="OwnerNum" id="OwnerNum">
<label for="LastName">Last Name</label>
<input type="text" name="LastName" id="LastName">
<label for="FirstName">First Name</label>
<input type="text" name="FirstName" id="FirstName">
<label for="Address">Address</label>
<input type="text" name="Address" id="Address">
<label for="City">City</label>
<input type="text" name="City" id="City">
<label for="State">State</label>
<input type="text" name="State" id="State">
<label for="PostalCode">PostalCode</label>
<input type="PostalCode" name="PostalCode" id="PostalCode">
<input type="submit" name="submit" value="Submit">
</form>
<a href="index.php">Back to home</a>
<?php require "templates/footer.php"; ?>