Skip to content

Instantly share code, notes, and snippets.

@pipster
Created December 4, 2012 18:41
Show Gist options
  • Save pipster/4207333 to your computer and use it in GitHub Desktop.
Save pipster/4207333 to your computer and use it in GitHub Desktop.
how to use prepared statements and oop php with mysqli
new-film.php
<?php
if(isset($_POST['submit'])) {
$mysqli = new mysqli('localhost', 'root', 'root', 'sakila');
$statement = $mysqli->prepare("INSERT INTO film(first_name, last_name) VALUES(?, ?)");
$statement->bind_param('ss', $_POST['first_name'], $_POST['last_name']);
$statement->execute();
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<?php
if(isset($_POST['submit'])) {
echo "Done! <a href='index.php'>Go to see new film</a>";
} else {
?>
<form action="insert-film.php" method="POST">
<p><label for="firstName">First Name</label>
<input type="text" name="firstName" /></p>
<p><label for="lastName">Last Name</label>
<input type="text" name="lastName" /></p>
<p><input type="submit" name="submit" /></p>
</form>
<?php } ?>
</body>
</html>
index.php
<?php
$mysqli = new mysqli('localhost', 'root', 'root', 'sakila');
$result = $mysqli->query("SELECT * FROM film");
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<table>
<?php while($row = $result->fetch_object()) : ?>
<tr>
<td><?php echo $row->id; ?></td>
<td><?php echo $row->first_name; ?></td>
<td><?php echo $row->last_name; ?></td>
</tr>
<?php endwhile; ?>
</table>
<body>
</body>
</html>
@jasonsarino
Copy link

what do you mean?

@chirchirezra
Copy link

Thanks you saved me a hassle..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment