Showing posts with label mysql. Show all posts
Showing posts with label mysql. Show all posts

CSV2MySQL convert from CSV file into MySQL database

How to convert from CSV file into MySQL database using PHP?
Plus how to create a table and fields into database with PHP?
Here it is..
We need 3 files php, and 2 file jpg (the jpg is just for menu).

index.php
http://jawaad.pastebin.com/f6399baf4

upload_csv.php
http://jawaad.pastebin.com/f2c7bdbee

create_table.php
http://jawaad.pastebin.com/f64b250cc


And image files:

PHP insert blob and update blob MySQL

Here is how to insert file BLOB MySQL.
PHP CODE:

if($_POST[submit]){
$file_name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$fp = fopen($tmp_name, 'r');
$file_content = fread($fp, $file_size) or die("Error: cannot read file");
$file_content = mysql_real_escape_string($file_content) or die("Error: cannot read file");
fclose($fp);
// INSERT
$qu = "INSERT INTO `file`
(`file_content`,`file_name`,`file_type`,`file_size`)
VALUES
('$content','$file_name','$file_type','$file_size')";
$re = mysql_query($qu) or die ("Sorry Cant insert db!");
echo $file_name." inserted succesfully to database";
}
echo "< form method=\"post\" name=\"form1\" enctype=\"multipart/form-data\">";
echo " < input name=\"file\" type=\"file\">";
echo " < input name=\"submit\" type=\"submit\" value=\"Upload\">";
echo "< /form>";

How to Protect from double posting insert MySQL

Submit once even the visitor is refresh the page from browser.
Here is the way to protect.
Use a SESSION.
1. Set a session in the form page

session_start();
$_SESSION[post_only_once] = 1;
echo "
< form method=\"post\">
Name < input type=\"text\" name=\"name\">
< input type=\"submit\" value=\"submit\" name=\"submit\">
< /form>
";


2. Check the session before doing INSERT into MySQL.
session_start();
if($_SESSION[post_only_once] == 1) {
// here doing insert to mysql
}
unset($_SESSION[post_only_once]); // remove the session after insert

How to connect MySQL from PHP

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
mysql_close($link);

Insert and Select doing in MySQL from SQL Injection

Here is the save way from RSS Attack SQL Injection.
Get all variable $_POST or $_GET or $_REQUEST or $_SESSION convert into mysql_real_escape string filter, you do not need one by one for all type variable.
here is code.
PHP CODE

$_POST  = array_map('mysql_real_escape_string', $_POST);
$_SESSION = array_map('mysql_real_escape_string', $_SESSION);
$_COOKIE = array_map('mysql_real_escape_string', $_COOKIE);


And here is to view back or mysql_unescape_string or mysql_real_unescpae_string or whatever you want to.
PHP CODE
function mysql_real_unescape_string($string){
$string=trim($string);
$string=str_replace("\\","",str_replace("\$","",$string));
return $string;
}

PHP Unlimited sub categories


The best Unlimited sub categories, I choose this one by Shadi Ali (Jan/15/2006).
The coding is simple, easy to understand and ready to use.


Enjoy...
This script use: PHP and MySQL database.
URL: http://www.phpclasses.org/browse/package/2742.html

Pagination Version 2 (ready to use PHP MySQL)

Very easy to use simply add the class, and edit setting:

$page = $_REQUEST["page"];
$alamat_pag = "http://elkaff.com/?m=Alumni"; // Full adress your PHP script
$adjacents = 3; // do not change if you do not know
$tabel="sample_tabel"; // table database form MySQL
$where=""; // where condition, optional
$order="ORDER BY `date` DESC, `name`"; // order condition, optional
$limit = 10; // set limit per page
include "pagination.php"; // include the class
$no=$start+1; // the number, if you want it to diplay, optional
while($ro = mysql_fetch_array($portfolio)) {
echo
$no." ";
echo
$ro[nama]."
"
;
}

echo
"Total: $total_pages
"
;
echo
$pagination;


The code is on: http://www.nusansifor.com/f/showthread.php?t=101