Jump to content

submit form and: upload CSV and form fields into mysql db


azukah

Recommended Posts

Hi-

the code below lets me upload a CSV file to my database if I have 1 field in my database and 1 column in my CSV.

I need to add to my db "player_id" from the CVS file and "event_name" and "event_type" from the form... any ideas???

 

here's the code:

<?php
$hoststring =""; 
$database = "";
$username = "";
$password = "";
$makeconnection = mysql_pconnect($hoststring, $username, $password);
?>
<?php
ob_start();
mysql_select_db($database, $makeconnection);
$sql_get_players="
SELECT * 
FROM tabel 
ORDER BY player_id ASC";
//
$get_players = mysql_query($sql_get_players, $makeconnection) or die(mysql_error());
$row_get_players = mysql_fetch_assoc($get_players);
//
$message = null;
$allowed_extensions = array('csv');
$upload_path = '.'; //same directory

if (!empty($_FILES['file'])) {

if ($_FILES['file']['error'] == 0) {
// check extension
$file = explode(".", $_FILES['file']['name']);
$extension = array_pop($file);

if (in_array($extension, $allowed_extensions)) {

	if (move_uploaded_file($_FILES['file']['tmp_name'], $upload_path.'/'.$_FILES['file']['name'])) {

		if (($handle = fopen($upload_path.'/'.$_FILES['file']['name'], "r")) !== false) {
			$keys = array();
			$out = array();
			$insert = array();
			$line = 1;

			while (($row = fgetcsv($handle, 0, ',', '"')) !== FALSE) {

				foreach($row as $key => $value) {    

					if ($line === 1) {
						$keys[$key] = $value;       
					} else {       
						$out[$line][$key] = $value;              
					}       
				}                
			$line++;          
				}        

			fclose($handle);            

			if (!empty($keys) && !empty($out)) {        
				$db = new PDO(
				'mysql:host=host;dbname=db', 
				'user', 
				'pw');   

				$db->exec("SET CHARACTER SET utf8");        

				foreach($out as $key => $value) {        
					$sql  = "INSERT INTO `table` (`";
					$sql .= implode("`player_id`", $keys);    
					$sql .= "`) VALUES (";    
					$sql .= implode(", ", array_fill(0, count($keys), "?"));    
					$sql .= ")";    
					$statement = $db->prepare($sql);    
					$statement->execute($value);       
				}      

				$message = '<span>File has been uploaded successfully</span>';      
			}
		}
	}
} else {
	$message = '<span>Only .csv file format is allowed</span>';
}
} else {
	$message = '<span>There was a problem with your file</span>';
}
}
ob_flush();?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>CSV File Upload</title>
</head>
<body>

<form class="form" action="" method="post" enctype="multipart/form-data">
<h3>Select Your File</h3>
<p><?php echo $message; ?></p>
<input type="file" name="file" id="file" size="30" />
<br/>
<label>Event Name:</label><input name="event_name" type="text" value="" />
<br/>
<label>Event Type:</label><input name="event_type" type="text" value="" />
<br/>
<input type="submit" id="btn" class="button" value="Submit" />
</form>

<br/>
<h3>Results:</h3>
<?php do { ?>
<p><?php echo $row_get_players['player_id'];?></p>
<?php } while ($row_get_players = mysql_fetch_assoc($get_players)); ?>
</body>
</html>

You wont like what I have to say but here goes.

bugs we can help with. How tos we can help with, but teaching programming takes a little longer than most people have time to spend helping out.

I have to say that this looks like you have cobbled together pieces of code that you found around on the internet and dont really understand what you are doing. If you did, then you could add any number of fields to your database. The long term solution is learning how to do something so that you can do it in other situations.

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.