我有一个PHP文件,其中有两组PHP代码。第一个还行,第二个就不行了。该文件完美地呈现了第一组php代码,然后呈现了一个表单,然后,它没有运行第二组php代码,而是将其打印出来,下面是代码:
<!DOCTYPE html>
<head>
<title>Rate your favorite movie</title>
<link rel='stylesheet' type='text/css' href='//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css'>
</head>
<html>
<body>
<div class="container">
<div class="row">
<div class="col-md-2"> </div>
<div class="col-md-5" style="background:#2b1b17;color:white">
<?php // FIRST SET OF PHP CODE
function get_movie_information($name) {
$url = "http://www.omdbapi.com/?t=".urlencode($name);
// send request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$curlData = curl_exec($curl);
curl_close($curl);
return json_decode($curlData, true);
}
$arr = get_movie_information($_POST["name"]);
echo "<br>";
$poster = $arr['Poster'];
echo '<img src="' . $poster . '">';
echo "<br>";
echo $arr['Title'];
echo "<br>";
echo $arr['Year'];
echo "<br>";
echo $arr['Rated'];
echo "<br>";
echo $arr['Genre'];
echo "<br>";
echo $arr['Plot'];
echo "<br>";
?>
<form Id='Form' Method='post' Action='index.php'>
<input type="submit" value="Add Movie">
</form>
<? // SECOND SET OF PHP CODE
$hostname='127.0.0.1';
$username='root';
$password='password';
If(isset($_POST['submit'])){
try {
$db = new PDO("mysql:host=$hostname;dbname=baboon;charset=utf8", $username, $password);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $db->prepare("INSERT INTO movies (title, year, rated, plot) VALUES (:title, :year, :rated, :plot)");
// old code that didnt work
// $stmt->bindParam(':title', $title);
// $stmt->bindParam(':year', $year);
// $stmt->bindParam(':rated', $rated);
// $stmt->bindParam(':plot', $plot);
$stmt->execute(array(
$title = $arr['Title'];
$year = $arr['Year'];
$rated = $arr['Rated'];
$plot = $arr['Plot'];
));
} catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
$db = null;
}
?>
</div>
</div>
</div>
<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
</body>
</html>
$db->setAttribute之后的内容都打印出来了
我在审查你的代码时注意到的一些问题:
- 您的
<head></head>
标签在<body></body>
标签之外。 - 你的第二个php标签被标记为
<?
而不是<?php
像你的第一个php标签。 - 你在第二个php代码中的
if
有一个大写的'I'。