Skip navigation links

MySQL Forums :: PHP :: INSERT Database Records with PHP


Advanced Search

INSERT Database Records with PHP
Posted by: Gregory Robb ()
Date: August 07, 2013 12:29PM

I am having trouble getting a simple form to submit data to a database. I have followed an example in a PHP/MySQL book (Welling and Thomson) and created a simple form to update a DVD collection. Right now I just have a form started and am just trying to get it to INSERT records into my database. It is making a connection to the database, but it returns my Error stating that the record could not be added. While all of my code is very basic (aka Bad) I am just trying to get an understanding as to how it is working... I have looked in MySQL through command prompt and the database exists, but records are not being added. Im not sure if I should be posting this on a PHP forum instead, but I thought I would try here first. I will post my code for the database and my two php files for inserting records.

Database:

create database movie_info;

use movie_info;

create table movies
(movieid int unsigned not null auto_increment primary key,
title char(50) not null,
movieyear char(4) not null,
genre char(25) not null,
subgenre char(25),
director char(30),
actor1 char(30),
actor2 char(30),
actor3 char(30),
discs char(2),
season char(2),
comments char(200)
);



Form:



function input_form(){
?>
<form method="post" action="insert_movie.php">
<table bgcolor="#cccccc">
<tr>
<td colspan="2">Enter a new DVD:</td>
<tr>
<td>Title:</td>
<td><input type="text" name="title"/></td></tr>
<tr>
<td>Year:</td>
<td><input type="text" name="year"/></td></tr>
<tr>
<tr>
<td>Genre:</td>
<td><input type="text" name="genre"/></td></tr>
<tr>
<tr>
<td>Sub-Genre:</td>
<td><input type="text" name="subgenre"/></td></tr>
<tr>
<tr>
<td>Director:</td>
<td><input type="text" name="director"/></td></tr>
<tr>
<tr>
<td>Actor:</td>
<td><input type="text" name="actor1"/></td></tr>
<tr>
<tr>
<td>Actor:</td>
<td><input type="text" name="actor2"/></td></tr>
<tr>
<tr>
<td>Actor:</td>
<td><input type="text" name="actor3"/></td></tr>
<tr>
<tr>
<td>Number of discs:</td>
<td><input type="text" name="discs"/></td></tr>
<tr>
<tr>
<td>Season:</td>
<td><input type="text" name="season"/></td></tr>
<tr>
<tr>
<td>Comments:</td>
<td><input type="text" name="comments"/></td></tr>
<tr>

<td colspan="2" align="center">
<input type="submit" value="Submit"/></td></tr>
<tr>
</table></form>
<?php
}



and the INSERT code:



<?php
require_once('movie_functions.php');
//require_once('db_functions.php');

do_html_header('Moviebase');

@$title = $_POST['title'];
@$year = $_POST['year'];
@$genre = $_POST['genre'];
@$subgenre = $_POST['subgenre'];
@$director = $_POST['director'];
@$actor1 = $_POST['actor1'];
@$actor2 = $_POST['actor2'];
@$actor3 = $_POST['actor3'];
@$discs = $_POST['discs'];
@$season = $_POST['season'];
@$comments = $_POST['comments'];

if (!$title || !$year || !$genre) {
echo "You have not entered all of the required details. <br />"
."Please go back and try again.<br /><br />"
."<a href='movies.php'>Go Back</a>";
exit;
}

@$db = new mysqli('localhost', 'root', '********', 'movie_info');

//if (!$result) {
//throw new Exception('Could not connect to database server');
//} else {
//return $result;
//}

if (mysqli_connect_errno()) {
echo "Error: Could not connect to database. Please try again later.";
exit;
}

$query = "INSERT INTO movies (movieid, title, movieyear, genre, subgenre,
director, actor1, actor2, actor3, discs, season, comments) VALUES
(NULL, '".$title."', '".$year."', '".$genre."', '".$subgenre."',
'".$director."', '".$actor1."', '".$actor2."', '".$actor3."',
'".$discs."', '".$season."', '".$comments."')";

$result = $db->query($query);

if ($result)
{
echo $db->affected_rows." has been inserted into the database.";
//input_form();
}
else
{
echo "An error has occurred. The item was not added.";

//for testing
echo "<br />Result: ".$result;
echo "<br />".$title;
echo "<br />".$year;
echo "<br />".$genre;
echo "<br />".$subgenre;
echo "<br />".$director;
echo "<br />".$actor1;
echo "<br />".$actor2;
echo "<br />".$actor3;
echo "<br />".$discs;
echo "<br />".$season;
echo "<br />".$comments;
//input_form();
}

$db->close();

footer();

?>

This all will return all variable values (except $result), so it seems like $result is empty.

Any help in understanding this would be greatly appreciated, Thanks!

Options: ReplyQuote


Subject Written By Posted
INSERT Database Records with PHP Gregory Robb 08/07/2013 12:29PM
Re: INSERT Database Records with PHP Rick James 08/09/2013 08:01PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.