我有一个包含两组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-&gt; setAttribute之后的一切都被打印了
答案 0 :(得分:0)
在审核您的代码时我注意到的一些问题:
<head></head>
代码位于<body></body>
代码之外。<?
而非<?php
,就像您的第一个php标记一样。if
有一个大写&#39;我&#39;。