i have php file has 2 sets of php code. first works fine, second, not much. file renders first set of php code perfectly, renders form, then, instead of running second set of php code, prints out, here code:
<!doctype html> <head> <title>rate 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 movies (title, year, rated, plot) values (:title, :year, :rated, :plot)"); // old code 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> somehow after $db->setattribute getting printed
some of issues noticed while reviewing code:
- your
<head></head>tags outside<body></body>tags. - your second php tag labeled
<?not<?phpfirst php tag. - the
ifhave in second php code has capital 'i'.
Comments
Post a Comment