- 1). Create a blank text file and name it "insert.php." Open it in a plain text editor, adding an opening line reading "<?php" and a closing line reading "?>." All other lines of PHP code need to go between these two statements.
- 2). Write a command to connect to the MySQL server on the second line using the "mysql_connect" syntax: "$link = mysql_connect(SERVER,USERNAME,PASSWORD);." Replace "server" with the location of your MySQL database and "username" and "password" with your login details. If the server was located at "mysql.example.com," and your username was "johndoe" with the password "12345," you would type:
"$link = mysql_connect(mysql.example.com,"johndoe","12345");" - 3). Add a line to select the database using the $link variable. The line should read "mysql_select_db(DATABASE,$link);," where "database" is replaced by the name of the actual database. For instance, if the database was called "2011_graduates," your code would read:
"mysql_select_db("2011_graduates",$link);." - 4). Create a MySQL query with the "INSERT INTO" syntax (you will need to know the name of the table and the name of the columns inside it.) If the table was called "english_students" and contained the columns "lastname," "firstname" and "gpa," the query would look like this:
"INSERT INTO english_students (lastname, firstname, gpa) VALUES ('doe', 'john', '4.0')". - 5). Insert the MySQL query between the parentheses of a "mysql_query" function on a new line:
"mysql_query("INSERT INTO english_students (lastname, firstname, gpa) VALUES ('doe', 'john', '4.0')");." - 6). Close the active MySQL link with a fifth line containing the code "mysql_close($link);." Save the PHP file and close the text editor.
previous post