PHP doesn't add the following information to MYSQL

Hi,

I can’t seem to get the code working.

It does not add the table etc to the MYSQL database.

Please can someone assist me and see what I am doing wrong:

[php]

Untitled Document <?php //connect to mysql $connect = mysql_connect('localhost', 'ash', 'Otorbet52@') or die ("Hello Looser, You dont have access");

//make sure databse is correct
mysql_select_db(“moviesite”);

//insert data into movie database
$insert = "INSERT INTO movie (movie_id, movie_name, movie_type, " .
“movie_year, movie_leadactor, movie_director)” .
"VALUES (1, ‘Bruce Almighty’, 5, 2001, 1, 2), " .
"(2, ‘Office’, 4, 2004, 1, 2), " .
“(3, ‘Need for Speed’, 2, 2005, 2, 1)”;

$results = mysql_query($insert)
or die(mysql_error());

$type = "INSERT INTO movietype (movietype_id, movietype_label) " .
"VALUES (1, ‘Sci-Fi’), " .
"(2, ‘Drama’), " .
"(3, ‘Action’), " .
"(4, ‘Comedy’), " .
"(5, ‘Horror’), " .
“(6, ‘War’)”;

$results = mysql_query($type)
or die(mysql_error());

$people = "INSERT INTO people (people_id, people_fullname, " .
“people_isactor, people_isdirector)” .
"VALUES (1, ‘Jim Carrey’), " .
"(2, ‘Ron Livingston’), " .
"(3, ‘Tom Shady’), " .
"(4, ‘Mary Jane’), " .
"(5, ‘Jimmy Raw’), " .
“(6, ‘Cat Stevens’)”;
$results = mysql_query($people)
or die (mysql_error());

echo “Data inserted successfully”
?>

[/php]

Please put your php code between the php tags.

First you’re using mysql_ statements, you should be using mysqli_ or PDO (My Recommendation). I don’t have time right now to help you further, but I’m sure someone else will if you give them a chance.

To add to Strider’s info…

MySQL is no longer used and will not work at all sometime in the near future, therefore you must upgrade soon.
MySQLi is the “improved” version of MySQL and might be the easiest for you to upgrade to. There are very few
changes to move up to that version.
PDO is the best version, works with more databases than just MySQL and is much more secure and safe!

Now, with that said, here is a link to a page that talks further about it and might give you some future info on it.
http://www.w3schools.com/php/php_mysql_connect.asp
And, here is a link to a list (although not complete) that will give you all you need for MySQLi programming.
http://www.w3schools.com/php/php_ref_mysqli.asp

Lastly, you inserted data into tables, but you did not CREATE the tables first. You mentioned that your code
did not “add the table” to the database. You can not do this with an INSERT query. You need to create it with
a different query. Here is how the query would be set up:
[php]
$query = ‘CREATE TABLE IF NOT EXISTS movie
(
movie_id INT NOT NULL AUTO_INCREMENT,
movie_name VARCHAR(256),
movie_type VARCHAR(256),
movie_year DATETIME,
movie_leadactor VARCHAR(256),
movie_director VARCHAR(256),
PRIMARY KEY(movie_id)
)’;
[/php]
Notes: This query creates a table if it does not already exist. It also sets up the fields based on your INSERT
code you posted. You might need to alter this to fit your uses. The sizes of all of the fields were a guess …
(Capitals are not needed, but, I use them to tell the commands apart for the field names and data.)

Hope this helps…

Sponsor our Newsletter | Privacy Policy | Terms of Service