我想在php中用PDO更新mysql中的數據庫。更新與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"];
}
>
除了實際更新之外,它完成所有工作。所以這不正確我猜:updateMovie($newTitle, $newYear, $newGenreID);
有沒有人在這裏看到一個問題?
我已經嘗試了很多東西,但它不工作。
謝謝!
這段代碼非常難讀,很多評論都沒用。爲什麼會有這麼多額外的空間?請遵守常規編碼慣例。 –
在每個文件中都有相同的用戶名和密碼。爲什麼不把它從功能中解脫出來。你爲什麼不使用課堂? –
你也應該分開HTML和PHP。如果需要,請包含一個php文件,但不要將它們混合在一起!它使一切都很難閱讀。 –