PHP Tutorial: MySQL Database Insert function

Status
Not open for further replies.

Markshall

Русский Стандарт
Contributor
Dec 18, 2010
2,637
2,389
This is my first YouTube tutorial video, so cut my some slack lolol

There's not much really that I can say apart from what is in the description of the video:

This video shows you exactly how to create your own function to quickly insert data into an MySQL database without having to keep typing out mysql_query( "INSERT..." etc every time.

My cursor can not be seen in the video and I am not sure why.

I hope it helped.

Video:

If you're too lazy to type down the code, here it is:

PHP:
<?php
/*
This function will enable you to quickly insert values into an MySQL database.

DATABASE STRUCTURE:
-------------------------------------------------------------
- FIELD  -  TYPE          -  AUTO INCREMENT  -    EXTRA    -
-  id    -  int(11)      -      YES        -  PRIMARY KEY -
- user  -  varchar(25)  -      NO        -              -
- pass  -  varchar(32)  -      NO        -              -
- email  -  varchar(150)  -      NO        -              -
-------------------------------------------------------------

Coded by Mark Eriksson
*/

@mysql_connect( "localhost", "root", "markeriksson" ) or die( "Can not connect to MySQL Database." );
@mysql_select_db( "test_insert" ) or die( "Can not find MySQL database." );

function insert( $table, $data )
{
$fields = array_keys( $data );
$values = array_map( "mysql_real_escape_string", array_values( $data ) );
@mysql_query( "INSERT INTO `" . $table . "` (`" . implode( "`,`", $fields ) . "`) VALUES ('" . implode( "','", $values ) . "');" ) or die( "Can not execute MySQL query." );
}

insert( "user", array( "user"  => "Mark Eriksson",
  "pass"  => md5( "markeriksson" ),
  "email" => "[email protected]"
) );
echo "<font color=\"green\">User inserted into database successfully!</font>";
?>
 
Status
Not open for further replies.

Users who are viewing this thread

Top