我是 MySQLi 的新手,我没有做 MySQL 和 php 的任何部分,但没有加载,我正在尝试正确学习,但是使用 MySQLi,因为 MySQL 现在已弃用。我的问题是,当我添加一些详细信息时,它会将我重定向到我的页面想要添加详细信息但实际上并没有将任何信息插入数据库时应该这样做,我尝试查看 phpMyAdmin 并且它不起作用。此外,当我单击编辑文件时,它给了我这个错误:致命错误:在 ....
这是文件:
<?php
include("includes/connecti.php");
function renderForm($title = '', $poster ='', $date = '', $story = '', $error = '', $id = '')
{ ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>
<?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?>
</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<h1><?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?></h1>
<?php if ($error != '') {
echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error
. "</div>";
} ?>
<form action="" method="post">
<div>
<?php if ($id != '') { ?>
<input type="hidden" name="id" value="<?php echo $id; ?>" />
<p>ID: <?php echo $id; ?></p>
<?php } ?>
<strong>Title: *</strong> <input type="text" name="title" value="<?php echo $title; ?>"/><br/>
<strong>Poster: *</strong> <input type="text" name="poster" value="<?php echo $poster; ?>"/>
<strong>Date: *</strong> <input type="text" name="date" value="<?php echo $date; ?>"/>
<strong>Story: *</strong> <input type="text" name="story" value="<?php echo $story; ?>"/>
<p>* required</p>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</body>
</html>
<?php }
/*
EDIT RECORD
*/
if (isset($_GET['id']))
{
if (isset($_POST['submit']))
{
if (is_numeric($_POST['id']))
{
$id = $_POST['id'];
$title = htmlentities($_POST['title'], ENT_QUOTES);
$poster = htmlentities($_POST['poster'], ENT_QUOTES);
$date = htmlentities($_POST['date'], ENT_QUOTES);
$story = htmlentities($_POST['story'], ENT_QUOTES);
if ($title == '' || $poster == '' || $date == '' || $story == '')
{
$error = 'ERROR: Please fill in all required fields!';
renderForm($title, $poster, $date, $story, $error, $id);
}
else
{
if ($stmt = $mysqli->prepare("UPDATE news SET title = ?, poster = ?, date = ?, story = ? WHERE id=?"))
{
$stmt->bind_param("ssi", $title, $poster, $date, $story, $id);
$stmt->execute();
$stmt->close();
}
else
{
echo "ERROR: could not prepare SQL statement.";
}
header("Location: view.php");
}
}
else
{
echo "Error!";
}
}
else
{
if (is_numeric($_GET['id']) && $_GET['id'] > 0)
{
$id = $_GET['id'];
if($stmt = $mysqli->prepare("SELECT * FROM news WHERE id=?"))
{
$stmt->bind_param("i", $id);
$stmt->execute();
$stmt->bind_result($id, $title, $poster, $date, $story);
$stmt->fetch();
renderForm($title, $poster, $date, $story, NULL, $id);
$stmt->close();
}
else
{
echo "Error: could not prepare SQL statement";
}
}
else
{
header("Location: view.php");
}
}
}
/*
NEW RECORD
*/
// if the 'id' variable is not set in the URL, we must be creating a new record
else
{
// if the form's submit button is clicked, we need to process the form
if (isset($_POST['submit']))
{
// get the form data
$title = htmlentities($_POST['title'], ENT_QUOTES);
$poster = htmlentities($_POST['poster'], ENT_QUOTES);
$date = htmlentities($_POST['date'], ENT_QUOTES);
$story = htmlentities($_POST['story'], ENT_QUOTES);
if ($title == '' || $poster == '' || $date == '' || $story == '')
{
$error = 'ERROR: Please fill in all required fields!';
renderForm($title, $poster, $date, $story, $error);
}
else
{
if ($stmt = $mysqli->prepare("INSERT news (title, poster, date, story) VALUES (?, ?, ?, ?)"))
{
$stmt->bind_param("ss", $title, $poster, $date, $story);
$stmt->execute();
$stmt->close();
}
else
{
echo "ERROR: Could not prepare SQL statement.";
}
header("Location: view.php");
}
}
else
{
renderForm();
}
}
$mysqli->close();
?>