Jump to content

[SOLVED] Adding Information into Database from HTML Form


toxictoad

Recommended Posts

HI all,

 

I've gone through an online tutorial to develop a php/html page to enter data into my db and I've tried to adapt it to my database but I've ran into problems.

 

My database just has the one table

 

db name: phatjoin_mymovies

table name: mymovies

Fields: title | genre | year | stars | country | link | director | plot | image | trailer | rating

 

the tutorial suggests using 2 php pages and this is what I have

 

form.php

<html>
<head>
<title>update database</title>
</head>
<body>
<form method="post" action="update.php">
Title: <input type="text" name="title" size="70" /><br />
Genre: <input type="text" name="genre" size="30" /><br />
Year: <input type="text" name="year" size="20" /><br />
Stars: <input type="text" name="stars" size="20" /><br />
Country: <input type="text" name="country" size="30" /><br />
Link: <input type="text" name="link" size="60" /><br />
Director: <input type="text" name="director" size="40" /><br />
Plot: <input type="text" name="plot" size="80" /><br />
Image: <input type="text" name="image" size="30" /><br />
Trailer: <input type="text" name="trailer" size="30" /><br />
Rating: <input type="text" name="rating" size="30" /><br />
<input type="submit" value="Update Database" /><br />
</body>
</html>

 

update.php

<?php
$title = $_POST['title'];
$genre = $_POST['genre'];
$year = $_POST['year'];
$stars = $_POST['stars'];
$country = $_POST['country'];
$link = $_POST['link'];
$director = $_POST['director'];
$plot = $_POST['plot'];
$image = $_POST['image'];
$trailer = $_POST['trailer'];
$rating = $_POST['rating'];

mysql_connect ("localhost", "USERNAME", "PASSWORD") or die 

('error: ' .mysql_error());
mysql_select_db ("phatjoin_mymovies");

$query="INSERT INTO mymovies (filmID, title, genre, year, stars, country, link, director, plot, image, trailer, rating) VALUES ('NULL','".$title."', '"$genre."', '"$year."', '"$stars."', '"$country."', '"$link."', '"$director."', '"$plot."', '"$image."', '"$trailer."', '"rating."')";

mysql_query($query) or die ('Error updating database');

echo "Database Updated" ;

?>

 

When I enter all the data into the form and hit submit I get directed to the update.php form and it's blank?

 

Any ideas where I've gone wrong?

 

Thanks

definitely turn on error reporting...but one thing I did notice is that NULL in your SQL statement shouldn't have single quotes around it.

 

$query="INSERT INTO mymovies (filmID, title, genre, year, stars, country, link, director, plot, image, trailer, rating) VALUES ('NULL','".$title."', '"$genre."', '"$year."', '"$stars."', '"$country."', '"$link."', '"$director."', '"$plot."', '"$image."', '"$trailer."', '"rating."')";

this:

$query="INSERT INTO mymovies (filmID, title, genre, year, stars, country, link, director, plot, image, trailer, rating) VALUES ('NULL','".$title."', '"$genre."', '"$year."', '"$stars."', '"$country."', '"$link."', '"$director."', '"$plot."', '"$image."', '"$trailer."', '"rating."')";

should be this:

$query="INSERT INTO mymovies (title, genre, year, stars, country, link, director, plot, image, trailer, rating) VALUES ('".$title."', '".$genre."', '".$year."', '".$stars."', '".$country."', '".$link."', '".$director."', '".$plot."', '".$image."', '".$trailer."', '".$rating."')";

you were missing a ***t load of periods (.) to successfully concatenate variables to the sql string, and $rating was missing the dollar sign.

i also assume that filmID auto-increments, since you try to set it to null, which is either a waste of space or an error, so i got rid of that too.

this:

$query="INSERT INTO mymovies (filmID, title, genre, year, stars, country, link, director, plot, image, trailer, rating) VALUES ('NULL','".$title."', '"$genre."', '"$year."', '"$stars."', '"$country."', '"$link."', '"$director."', '"$plot."', '"$image."', '"$trailer."', '"rating."')";

should be this:

$query="INSERT INTO mymovies (title, genre, year, stars, country, link, director, plot, image, trailer, rating) VALUES ('".$title."', '".$genre."', '".$year."', '".$stars."', '".$country."', '".$link."', '".$director."', '".$plot."', '".$image."', '".$trailer."', '".$rating."')";

you were missing a ***t load of periods (.) to successfully concatenate variables to the sql string, and $rating was missing the dollar sign.

i also assume that filmID auto-increments, since you try to set it to null, which is either a waste of space or an error, so i got rid of that too.

 

Thank you :)

 

works great, I'll have to be a lot more careful in coding (guess it comes with exerience)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.