Php post to a mysql database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tombstone
Forum Newbie
Posts: 5
Joined: Thu Feb 02, 2006 1:11 am

Php post to a mysql database

Post by tombstone »

Ok so first off i have regester_globals vales to on (i know its unsecure)
Here is the PHP code add.php all in CASE Sensitive:

Code: Select all

<?php
$user="root";
$pass="password";
$db="movielist";
$movie_name=$movie_name;
$web_address=$web_address;
$type_Action=$type_Action;
$type_Adventure=$type_Adventure;
$type_Animation=$type_Animation;
$type_Family=$type_Family;
$type_Comedy=$type_Comedy;
$type_Crime=$type_Crime;
$type_Documentary=$type_Documentary;
$type_Drama=$type_Drama;
$type_Fantasy=$type_Fantasy;
$type_Horror=$type_Horror;
$type_Independent=$type_Independent;
$type_Musical=$type_Musical;
$type_Mystery=$type_Mystery;
$type_Romance=$type_Romance;
$type_ScienceFiction=$type_ScienceFiction;
$type_Thriller=$type_Thriller;
$type_War=$type_War;
$type_Western=$type_Western;
$type_Holiday=$type_Holiday;
$rating=$rating;
$book=$book;



mysql_connect(localhost,$user,$pass);
mysql_select_db($db) or die( "Unable to select database, Please contact your administrator");
$query="INSERT INTO movie_list VALUES ('','$movie_name','$web_address','$type_Action',
'$type_Adventure','$type_Animation','$type_Family','$type_Comedy','$type_Crime',
'$type_Documentary','$type_Drama','$type_Fantasy','$type_Horror','$type_Independent',
'$type_Musical','$type_Mystery','$type_Romance','$type_ScienceFiction','$type_Thriller',
'$type_War','$type_Western','$type_Holiday','$rating','$book')";
echo "done";
?>

Here is the code i used to create the table:

Code: Select all

<?php
$user="root";
$pass="password";
$db="movielist";
mysql_connect(localhost,$user,$pass);
mysql_select_db($db) or die( "Unable to select database Please contact the administrator.");
$query="CREATE TABLE movie_list (id int(7) NOT NULL auto_increment,movie_name varchar(100) NOT NULL,web_address varchar(150) NOT NULL,
type_Action int(1) NOT NULL,type_Adventure int(1) NOT NULL,type_Animation int(1) NOT NULL,type_Family int(1) NOT NULL,type_Comedy int(1) NOT NULL,
type_Crime int(1) NOT NULL,type_Documentary int(1) NOT NULL,type_Drama int(1) NOT NULL,type_Fantasy int(1) NOT NULL,type_Horror int(1) NOT NULL,
type_Independent int(1) NOT NULL,type_Musical int(1) NOT NULL,type_Mystery int(1) NOT NULL,type_Romance int(1) NOT NULL,
type_ScienceFiction int(1) NOT NULL,type_Thriller int(1) NOT NULL,type_War int(1) NOT NULL,type_Western int(1) NOT NULL,type_Holiday int(1) NOT NULL,
rating varchar(2) NOT NULL,book varchar(50) NOT NULL,Add_Date varchar(15) NOT NULL,PRIMARY KEY (id),UNIQUE id (id))";
mysql_query($query);
mysql_close();
print("<center><h1>Done Creating the Table</h1></center>");
?>
Not sure what is going on. I have also tried $movie_name=$_POST['movie_name'];
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Php post to a mysql database

Post by josh »

tombstone wrote:Not sure what is going on
Me neither, what was the question?


Also this code is redundant:

Code: Select all

$movie_name=$movie_name;
$web_address=$web_address;
$type_Action=$type_Action;
$type_Adventure=$type_Adventure;
$type_Animation=$type_Animation;
$type_Family=$type_Family;
$type_Comedy=$type_Comedy;
$type_Crime=$type_Crime;
$type_Documentary=$type_Documentary;
$type_Drama=$type_Drama;
$type_Fantasy=$type_Fantasy;
$type_Horror=$type_Horror;
$type_Independent=$type_Independent;
$type_Musical=$type_Musical;
$type_Mystery=$type_Mystery;
$type_Romance=$type_Romance;
$type_ScienceFiction=$type_ScienceFiction;
$type_Thriller=$type_Thriller;
$type_War=$type_War;
$type_Western=$type_Western;
$type_Holiday=$type_Holiday;
$rating=$rating;
$book=$book;
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

A great function for MySQL debugging is mysql_error(). Adding it to your die() error handler will output the exact MySQL error from the server, it's similar to the PHP error reporting system so you'll will know exactly what to do and where to fix the issue.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

$query is created in the first snippet, but never used against mysql.
tombstone
Forum Newbie
Posts: 5
Joined: Thu Feb 02, 2006 1:11 am

Re: Php post to a mysql database

Post by tombstone »

Basicly it is not updating the table at all.
Yeah i thought th code was pointless but i tried it with and without

Code: Select all

<?php
$user="root";
$pass="password";
$db="movielist";
mysql_connect(localhost,$user,$pass);
mysql_select_db($db) or die( "Unable to select database, Please contact your administrator");
$query="INSERT INTO movie_list VALUES ('','$movie_name','$web_address','$type_Action','$type_Adventure','$type_Animation','$type_Family','$type_Comedy','$type_Crime','$type_Documentary','$type_Drama','$type_Fantasy','$type_Horror','$type_Independent','$type_Musical','$type_Mystery','$type_Romance','$type_ScienceFiction','$type_Thriller','$type_War','$type_Western','$type_Holiday','$rating','$book')";

echo mysql_error();

echo "<p>done";
?>

After i ran it with this it just displays 'Done'


[quote="jshpro2"]

Me neither, what was the question?


[/quote]
tombstone
Forum Newbie
Posts: 5
Joined: Thu Feb 02, 2006 1:11 am

Post by tombstone »

feyd wrote:$query is created in the first snippet, but never used against mysql.

Code: Select all

<?php
$user="root";
$pass="password";
$db="movielist";
mysql_connect(localhost,$user,$pass);
mysql_select_db($db) or die( "Unable to select database, Please contact your administrator");
$query="INSERT INTO movie_list VALUES ('','$movie_name','$web_address','$type_Action','$type_Adventure','$type_Animation','$type_Family','$type_Comedy','$type_Crime','$type_Documentary','$type_Drama','$type_Fantasy','$type_Horror','$type_Independent','$type_Musical','$type_Mystery','$type_Romance','$type_ScienceFiction','$type_Thriller','$type_War','$type_Western','$type_Holiday','$rating','$book')";
mysql_query($query); 
echo mysql_error();

echo "<p>done";
?>
Now i get this:

Column count doesn't match value count at row 1
done
tombstone
Forum Newbie
Posts: 5
Joined: Thu Feb 02, 2006 1:11 am

Post by tombstone »

sorry guys im looking at old code. I forgot i added a date integer into the mix which means i am missing one...
tombstone
Forum Newbie
Posts: 5
Joined: Thu Feb 02, 2006 1:11 am

Post by tombstone »

Oh and thanxs for pointing out that i had done nothign with the $query i totally missed that *slaps head*
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

Here is a couple example usages of it:

Code: Select all

<?php
mysql_select_db($db) or die( "Unable to select database, Please contact your administrator<br />". mysql_error()); 
?>
and without any text:

Code: Select all

<?php
mysql_select_db($db) or die(mysql_error()); 
?>
Post Reply