MySQL Forums
Forum List  »  PHP

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.


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)


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

<td colspan="2" align="center">
<input type="submit" value="Submit"/></td></tr>

and the INSERT code:



@$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>";

@$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.";

$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.";
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;




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

Written By
INSERT Database Records with PHP
August 07, 2013 12:29PM

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.