Jump to content

Using Php to insert data into MYSQL


connexion2005

Recommended Posts

Hey guys I just whipped up this script for part of our project. Do the insert statements and script look legit in order to make php insert data into my mysql tables which I already created in previous scripts?

 

Josh

 

<?php

 

//The directions said to connect to the db using number one that Wylene created.

define('SQL_HOST','localhost');

define('SQL_USER','Robert');

define('SQL_PASS','ITnw2459');

define('SQL_DB','test');

 

//Making sure we select the right database here.

mysql_select_db(SQL_DB,$conn);

 

//Here are the insert statements that I used to populate our tables.

$incourseinfo = "INSERT INTO course_info (course_num, course_name, course_type, credit_hours) " .

                "VALUES (101, 'Geometry', 'Math', 1), " .

"(102, 'Spanish', 'Langauages', 1), " .

"(103, 'American History', 'History', 1), " .

"(104, 'TechWriting', 'English', 1), " .

"(105, 'Algebra1', 'Math', 1), " .

"(106, 'Algebra2', 'Math', 1), " .

"(107, 'ITsupport', 'Networking', 1), " .

"(108, 'Webserver', 'Networking', 1), " .

"(109, 'Bowling', 'Athletics', 1), " .

"(110, 'Archery', 'Athletics', 1), " .

"(111, 'World History', 'History', 1), " .

"(112, 'Texas History', 'History', 1), " .

"(113, 'Chemistry', 'Science', 1), " ;

$results = mysql_query($incourseinfo)

or die(mysql_error());

 

$instudentdata = "INSERT INTO student_data (stud_id, lname, fname, mname, campus enum) " .

"VALUES (1001, 'Doe', 'John', 'Anderson', 'Bryan'), " .

(1001, 'Dane', 'Jane', 'Bates', 'Bryan'), " .

(1002, 'Davids', 'Joe', 'Davis', 'Bryan'), " .

(1003, 'Berds', 'Jason', 'Jonathon', 'Bryan'), " .

(1004, 'Crawford', 'Martha', 'Cindy', 'Bryan'), " .

(1005, 'Fuqua', 'Amy', 'Jay', 'Bryan'), " .

(1006, 'Pierce, 'Paul', 'Anthony', 'Bryan'), " .

(1007, 'Cook', 'Dane', 'Damon', 'Bryan'), " .

(1008, 'Bagwell', 'Jeff', 'Lucas', 'Bryan'), " .

(1009, 'Wagner', 'Billy', 'Dean', 'Bryan'), " .

(1010, 'Griffey', 'Ken', 'Jr', 'Bryan'), " .

(1011, 'Jordan', 'Michael', 'James', 'Bryan'), " .

(1012, 'Olajuwon', 'Hakeem', 'Dequa', 'Brenham'), " .

(1013, 'Drexler', 'Clyde', 'Donvan', 'Brenham'), " .

(1014, 'James', 'Lebron', 'Donovan', 'Brenham'), " .

(1015, 'Groce', 'Jared', 'Roger', 'Brenham'), " .

(1016, 'Groce', 'Chad', 'Allen', 'Brenham'), " .

(1017, 'Faulk', 'Abby', 'Lynn', 'Brenham'), " .

(1018, 'Thomas', 'Frank', 'Junior', 'Brenham'), " .

(1019, 'Vaughn', 'Moe', 'Williams', 'Brenham'), " .

(1020, 'Davis', 'Paul', 'Wayne', 'Brenham'), " .

(1021, 'Jacobs', 'Anthony', 'John', 'Brenham'), " .

(1022, 'Joseph', 'Paul', 'Raymond', 'Sealy'), " .

(1023, 'Jameson', 'Joe', 'Jenna', 'Sealy'), " .

(1024, 'Wilkerson', 'Katelynn', 'Lou', 'Sealy'), " .

(1025, 'Hickson', 'Meredith', 'Shae', 'Sealy'), " .

(1026, 'Homeyer', 'Catie', 'Lynn', 'Sealy'), " .

(1027, 'Prewit', 'Brittany', 'Sarah', 'Sealy'), " .

(1028, 'Legravier', 'Amber', 'Denise', 'Sealy'), " .

(1029, 'Williams', 'Molly', 'Amber', 'Sealy'), " .

(1030, 'Crnkovic', 'Nicole', 'Bae', 'Sealy'), " .

(1031, 'Smith', 'Catherine', 'Vannah', 'Sealy'), " .

(1032, 'Sowell', 'Savannah', 'Bobbi', 'Schulenburg'), " .

(1034, 'Demson', 'Dannika', 'Didi', 'Schulenburg'), " .

(1035, 'Werzer', 'Jennfier', 'Jade', 'Schulenburg'), " .

(1036, 'Poehl', 'Jana', 'Grace', 'Schulenburg'), " .

(1037, 'Groce', 'Josh', 'Holland', 'Schulenburg'), " .

(1038, 'Alford', 'Steven', 'Bradley', 'Schulenburg'), " .

(1039, 'Landolt', 'Keith', 'William', 'Schulenburg'), " .

(1040, 'Hibbeler', 'James', 'Allen', 'Schulenburg'), " .

(1041, 'Gold', 'Gatlin', 'Wayne', 'Schulenburg'), " .

(1042, 'See', 'Stephen', 'Brooks', 'Schulenburg'), " ;

$results = mysql_query($instudentdata)

or die(mysql_error());

 

$ininstructors = "INSERT INTO instructors (inst_id, lname, fname, mname, office_loc, phone, email) " .

"VALUES (2001, 'Thomas', 'Dale', 'NA', 'West', 9792558001, 'dale@edu'), " .

(2002, 'Williams', 'Dale', 'John', 'West', 9792558002, 'dale2@edu'), " .

(2003, 'Wexelman', 'Trent', 'Davis', 'West', 9792558003, 'trent@edu'), " .

(2004, 'Jordan', 'Hawkin', 'Glenn', 'East', 9792558004, 'jordan@edu'), " ;

$results = mysql_query($ininstructors)

or die(mysql_error());

 

//Im not sure how to insert the semester tables? So I just did a random 10 to guess. The inst_id's repeat because there are only 4 instructors. Adan HELP!!!...LOL.

$inenrollment - "INSERT INTO enrollment (course_num, inst_id, sud_id, semester enum, year) " .

"VALUES (101, 2001, 1001, 'Fall', 2007), " .

(102, 2002, 1002, 'Fall', 2007), " .

(103, 2003, 1003, 'Fall', 2007), " .

(104, 2004, 1004, 'Fall', 2007), " .

(105, 2001, 1005, 'Fall', 2007), " .

(106, 2002, 1006, 'Spring', 2008), " .

(107, 2003, 1007, 'Spring', 2008), " .

(108, 2004, 1008, 'Spring', 2008), " .

(109, 2001, 1009, 'Spring', 2008), " .

(110, 2002, 1010, 'Spring', 2008), " ;

$results = mysql_query($inenrollment)

or die(mysql_error());

 

echo "Data has been successfully inserted!"

 

?>

Link to comment
https://forums.phpfreaks.com/topic/44236-using-php-to-insert-data-into-mysql/
Share on other sites

Why not run it and see? If there's any error messages or things you don't understand, but askin to look through the whole script and see if it's ok is a bit too general.

 

That said it looks fine, but there could easily be some small syntax error that could be overlooked.

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.