我正在尝试用php中的PDO更新mysql中的数据库。
这是我的代码:
<!DOCTYPE HTML>
<head>
<title>My Movie DataBase! - EDIT</title>
</head>
<body>
<?php
include "functions.php";
if( isset($_POST["submit"]) ) {
$postValue = $_POST["submit"];
//get title to edit from form
$title = $_POST["title"];
//get current movie info from db
$movieInfo = getMovie($title);
//set variables with movie info from db
$year = $movieInfo[0]["jaar"];
$genre_id = $movieInfo[0]["genre_id"];
$genre = getGenre($genre_id);
if( isset($_POST["change"]) ) {
$newTitle = $_POST["newTitle"];
$newYear = $_POST["newYear"];
$newGenre = $_POST["newGenre"];
$newGenreID = getGenreID($newGenre);
updateMovie($newTitle, $newYear, $newGenreID);
print("
The movie has been edited! <br />
It's now: <br />
Title: $newTitle <br />
Year: $newYear <br />
Genre: $newGenre <br />
Genre ID: $newGenreID <br />
");
print("<br />Click <a href=\"index.html\">here</a> to go to the homepage! <br />");
}
else {
print("
<form action=\"editmovie.php\" method=\"POST\">
Title: <input type=\"text\" name=\"newTitle\" value=\"$title\" /> <br />
Year: <input type=\"text\" name=\"newYear\" value=\"$year\" /> <br />
Genre ID: <input type=\"text\" name=\"newGenreID\" value=\"$genre_id\" readonly=\"TRUE\" /> <br />
Genre: <input type=\"text\" name=\"newGenre\" value=\"$genre\" /> <br />
<input type=\"hidden\" name=\"submit\" value=\"$postValue\" />
<input type=\"submit\" name=\"change\" value=\"Apply changes!\" /> <br />
</form>
");
}
}
else {
print("
Which movie do you want to edit? <br />
Type in the exact title of the movie: <br />
<form action=\"editmovie.php\" method=\"POST\" >
Title: <input type=\"text\" name=\"title\" /> <br />
<input type=\"submit\" name=\"submit\" value=\"Continue!\" />
</form>
");
}
?>
</body>
这是我的functions.php:
<?php
//function returns executed $sql array as FETCH_ASSOC
function dbConn($sql) {
//db connection
$user = "root";
$pass = "root";
$database = new PDO(
'mysql:host=localhost; port=8473; dbname=moviedb',
$user,
$pass
);
//sql statement
$statement = $database -> prepare("$sql");
//execute sql statement
$statement -> execute();
$returnArray = $statement -> fetchAll(PDO::FETCH_ASSOC);
return $returnArray;
}
function getMovie($title) {
//db connection
$user = "root";
$pass = "root";
$database = new PDO(
'mysql:host=localhost; port=8473; dbname=moviedb',
$user,
$pass
);
//sql statement
$statement = $database -> prepare("
SELECT *
FROM Film
WHERE titel = :title
");
//bindvalue $title aan :title
$statement -> bindValue(":title", $title, PDO::PARAM_STR);
//execute sql statement
$statement -> execute();
return $statement -> fetchAll(PDO::FETCH_ASSOC);
}
function updateMovie($title, $year, $genre_id) {
//db connection
$user = "root";
$pass = "root";
$database = new PDO(
'mysql:host=localhost; port=8473; dbname=moviedb',
$user,
$pass
);
//sql statement
$statement = $database -> prepare("
UPDATE Film
SET titel = :title,
jaar = :year,
genre_id = :genre_id
");
//bindvalue
$statement -> bindValue(":title", $title, PDO::PARAM_STR);
$statement -> bindValue(":year", $year, PDO::PARAM_STR);
$statement -> bindValue(":genre_id", $genre_id, PDO::PARAM_STR);
//execute sql statement
$statement -> execute();
}
//print a table of an array $movie
function movieTable($movieArray) {
//tablehead
print("<table border=\"2\">");
print("
<tr>
<th>Title</th>
<th>Jaar</th>
<th>Genre ID</th>
<th>Genre</th>
</tr>
");
foreach( $movieArray AS $index => $record ) {
$titel = $record["titel"];
$jaar = $record["jaar"];
$id = $record["id"];
$naam = $record["naam"];
print("
<tr>
<td>$titel   </td>
<td>$jaar   </td>
<td>$id   </td>
<td>$naam   </td>
</tr>
");
}
print("</table>");
}
//print the moviedb in a table
function movieDB() {
$movieArraySQL = "
SELECT F.titel, F.jaar, G.id, G.naam
FROM Film F
JOIN Genre G ON F.genre_id = G.id
ORDER BY F.titel
";
movieTable(dbConn($movieArraySQL));
}
//adds a movie with title=$title, jaar=$jaar, genre_id=$genre_id to the database. returns execute statement (TRUE if success, FALSE if failed)
function addMovie($titel, $jaar, $genre_id) {
//db connection
$user = "root";
$pass = "root";
$database = new PDO(
'mysql:host=localhost; port=8473; dbname=moviedb',
$user,
$pass
);
//sql statement
$statement = $database -> prepare("
INSERT INTO Film
VALUES (:titel, :jaar, :genre_id)
");
$statement -> bindValue(":titel", $titel, PDO::PARAM_STR);
$statement -> bindValue(":jaar", $jaar, PDO::PARAM_STR);
$statement -> bindValue("genre_id", $genre_id, PDO::PARAM_STR);
//execute sql statement
return $statement -> execute();
}
//returns genre with genre_id=$genre_id
function getGenre($genre_id) {
//db connection
$user = "root";
$pass = "root";
$database = new PDO(
'mysql:host=localhost; port=8473; dbname=moviedb',
$user,
$pass
);
//sql statement
$statement = $database -> prepare("
SELECT naam
FROM Genre
WHERE id = :genre_id
");
$statement -> bindValue(":genre_id", $genre_id, PDO::PARAM_STR);
$statement -> execute();
$tempArray = $statement -> fetchAll(PDO::FETCH_ASSOC);
return $tempArray[0]["naam"];
}
//returns genre id with genre=$genre
function getGenreID($genre) {
//db connection
$user = "root";
$pass = "root";
$database = new PDO(
'mysql:host=localhost; port=8473; dbname=moviedb',
$user,
$pass
);
//sql statement
$statement = $database -> prepare("
SELECT id
FROM Genre
WHERE naam = :genre
");
$statement -> bindValue(":genre", $genre, PDO::PARAM_STR);
$statement -> execute();
$tempArray = $statement -> fetchAll(PDO::FETCH_ASSOC);
return $tempArray[0]["id"];
}
&GT;
除了实际更新之外,它可以完成所有操作。所以这不能正确执行我猜:updateMovie($newTitle, $newYear, $newGenreID);
有人在这里看到问题吗?
我尝试了很多东西,但它没有用。
谢谢!
答案 0 :(得分:0)
UPDATE
中的updateMovie()
语句缺少WHERE
条款。