我试图从MySQL切换到MySQLi。
如果你到我的工作页面: Your Favorite Movie 点击列表电影并向下滚动,您会看到所有字段中都有数据。
但是如果你去: Your Favorite Movie (MySQLi) 单击列表电影,您将看到仅填充ID和日期和时间字段。
我在代码中遗漏了什么吗?
以下是MySQLi页面的相关代码:
if ($choice == 'list movies') {
$query = "SELECT movie_id, movie_name, name, comment, date FROM movie";
if ($stmt = mysqli_prepare($con, $query)) {
/* execute statement */
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $movie_id, $movie_name, $nname, $ccomment, $date);
echo "<table border=1><tr><th>ID</th><th>Movie Name</th><th>Submitter Name</th><th>Comment</th><th>Date and Time</th></tr>";
/* fetch values */
while (mysqli_stmt_fetch($stmt)) {
if ($movie_name == '') $movie_name = ' ';
if ($nname == '') $nname = ' ';
if ($ccomment == '') $ccomment = ' ';
echo "<tr><td>$movie_id</td><td>$movie_name</td><td>$nname</td><td>$ccomment</td><td>$date</td></tr>";
}
echo "</table>";
}
/* close statement */
mysqli_stmt_close($stmt);
/* close connection */
mysqli_close($con);
}
2015年1月26日上午9:39更新: 这是来自http://bretleduc.com/FavMov(完整)的代码:
<html>
<head>
<title>Your Favorite Movie</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<h1> Your Favorite Movie </h1>
<br>
This webpage simply records the names of your favorite movies. I created it to practice PHP and MySQL coding.
<br><br>
<p style="text-align:center"><a href="http://bretleduc.com">Return to BretLeduc.com</a></p>
<br><br>
<?php
// connect to the Database
include('connect.php');
$id = $_POST['id'];
$moviename = $_POST['moviename'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$choice = $_POST['submit'];
$id_mysql = mysql_real_escape_string($id);
$movie_mysql = mysql_real_escape_string($moviename);
$name_mysql = mysql_real_escape_string($name);
$comment_mysql = mysql_real_escape_string($comment);
//list movies
if ($choice == 'list movies') {
$sql = "select * from movie";
echo "<br />" . $sql . "<br />";
$result = mysql_query($sql);
echo "<table border=1><tr><th>ID</th><th>Movie Name</th><th>Submitter Name</th><th>Comment</th><th>Date and Time</th></tr>";
while ($row = mysql_fetch_assoc($result)) {
if ($row['movie_name'] == '') $row['movie_name'] = ' ';
if ($row['name'] == '') $row['name'] = ' ';
if ($row['comment'] == '') $row['comment'] = ' ';
$mn = nl2br($row['movie_name']);
$n = nl2br($row['name']);
$c = nl2br($row['comment']);
echo "<tr><td>{$row['movie_id']}</td><td>$mn</td><td>$n</td><td>$c</td><td>{$row['date']}</td></tr>";
}
echo "</table>";
}
// add a movie
if ($choice == 'add movie') {
$sql="INSERT INTO movie (movie_id, movie_name, name, comment, date)
VALUES (null, '$movie_mysql', '$name_mysql', '$comment_mysql', NOW());";
echo "<br />".$sql."<br />";
$r = mysql_query ($sql);
if ($r) echo "<br />Insert succeeded<br />";
else
echo "<br />Insert failed <br /> MySQL Error Number: " . mysql_errno($r) . ": " . mysql_error($r). "<br />";
}
//delete a movie
if ($choice == 'delete movie') {
$q = "delete from movie where movie_id = '$id_mysql'";
echo "<br />".$q."<br />";
$r = mysql_query($q);
if (mysql_affected_rows() > 0 ) echo "<br />delete succeeded<br>";
else
echo "<br />Delete failed <br /> MySQL Error Number: " . mysql_errno() . ": " . mysql_error(). "<br />";
}
?>
<br />
<form action="" method="post"/>
What is the name of your favorite movie? <textarea name="moviename" rows="1" cols="20"></textarea><br />
What is your name? <input type="text" name="name" value="<?php echo $name ?>"/><br />
Comment: <textarea name="comment" rows="1" cols="20"></textarea><br />
<br /> <br />
If you would like to delete an entry, enter its ID here and press "delete movie": <input name="id" type="text"/><br />
<br /> <br />
<input type="submit" name="submit" value="list movies"/>
<input type="submit" name="submit" value="add movie"/>
<input type="submit" name="submit" value="refresh"/>
<input type="submit" name="submit" value="delete movie"/>
</form>
<br />
<br />
<br />
<p style="text-align:center"><a href="http://bretleduc.com">Return to BretLeduc.com</a></p>
<br />
<br />
<br />
<br />
<br />
</body>
</html>
以下是来自http://bretleduc.com/FavMov/indexm.php的代码(完整地),但不起作用:
<html>
<head>
<title>Your Favorite Movie</title>
<link rel="shortcut icon" type="image/x-icon" href="favicon.ico">
</head>
<body>
<h1> Your Favorite Movie </h1>
<br>
This webpage simply records the names of your favorite movies. I created it to practice PHP and MySQL coding.
<br><br>
<p style="text-align:center"><a href="http://bretleduc.com">Return to BretLeduc.com</a></p>
<br><br>
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
date_default_timezone_set('America/Los_Angeles');
// connect to the Database
include('mysql_connect(powweb).php');
date_default_timezone_set('America/Los_Angeles');
$con=mysqli_connect("host","username","password","database");
// Check connection
if (mysqli_connect_errno()) {
echo "<br />Failed to connect to MySQL: " . mysqli_connect_error() . "<br />";
}
$id = $_POST['id'];
$moviename = $_POST['moviename'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$choice = $_POST['submit'];
// escape variables for security
$id_mysqli = mysqli_real_escape_string($con, $id);
$movie_mysqli = mysqli_real_escape_string($con, $moviename);
$name_mysqli = mysqli_real_escape_string($con, $name);
$comment_mysqli = mysqli_real_escape_string($con, $comment);
//list movies
if ($choice == 'list movies') {
$query = "SELECT movie_id, movie_name, name, comment, date FROM movie";
if ($stmt = mysqli_prepare($con, $query)) {
/* execute statement */
mysqli_stmt_execute($stmt);
/* bind result variables */
mysqli_stmt_bind_result($stmt, $movie_id, $movie_name, $nname, $ccomment, $date);
echo "<table border=1><tr><th>ID</th><th>Movie Name</th><th>Submitter Name</th><th>Comment</th><th>Date and Time</th></tr>";
/* fetch values */
while (mysqli_stmt_fetch($stmt)) {
if ($movie_name == '') $movie_name = ' ';
if ($nname == '') $nname = ' ';
if ($ccomment == '') $ccomment = ' ';
echo "<tr><td>$movie_id</td><td>$movie_name</td><td>$nname</td><td>$ccomment</td><td>$date</td></tr>";
}
echo "</table>";
}
/* close statement */
mysqli_stmt_close($stmt);
/* close connection */
mysqli_close($con);
}
// add a movie
if ($choice == 'add movie') {
$sql="INSERT INTO movie (movie_id, movie_name, name, comment, date)
VALUES (null, '$movie_mysqli', '$name_mysqli', '$comment_mysqli', NOW());";
echo "<br />" . $sql . "<br />";
if (!mysqli_query($con, $sql)) {
die("<br />Insert failed <br /> MySQL Error Number: " . mysql_errno($con) . ": " . mysql_error($con). "<br />");
}
echo "<br />Insert succeeded<br />";
mysqli_close($con);
}
//delete a movie
if ($choice == 'delete movie') {
$q = "delete from movie where movie_id = '$id_mysqli'";
echo "<br />" . $q . "<br />";
$r = mysqli_query($con, $q);
if (mysqli_affected_rows($con) > 0 ) echo "<br />Delete succeeded<br />";
else
echo "<br />Delete failed <br />";
}
?>
<br />
<form action="" method="post"/>
<br />
What is the name of your favorite movie? <textarea name="moviename" rows="1" cols="20"></textarea><br />
What is your name? <input type="text" name="name" value="<?php echo $name ?>"/><br />
Comment: <textarea name="comment" rows="1" cols="20"></textarea><br />
<br /> <br />
If you would like to delete an entry, enter its ID here and press "delete movie": <input name="id" type="text"/><br />
<br /> <br />
<input type="submit" name="submit" value="list movies"/>
<input type="submit" name="submit" value="add movie"/>
<input type="submit" name="submit" value="refresh"/>
<input type="submit" name="submit" value="delete movie"/>
</form>
<br />
<br />
<br />
<p style="text-align:center"><a href="http://bretleduc.com">Return to BretLeduc.com</a></p>
<br />
<br />
<br />
<br />
<br />
</body>
</html>
我认为应该指出,在这个indexm.php页面中,我检查$ movie_name,$ nname和$ ccomment是否为空,然后为它们分配
的过程正常工作并且表格围绕这些空单元格绘制边框。这似乎意味着网页正在从数据库接收这些变量的数据为空(空白)。当我注释掉那个过程时,空单元格周围没有边框。
我还补充说:
error_reporting(E_ALL); ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
到Fred要求的代码。