Jump to content

Recommended Posts

Hi Everyone

 

I'm currently trying to learn PHP whilst writing my own custom control panel

 

I had a working import csv page but I got told its not really good to use mysql_* anymore, so I've started again with trying to do it with pdo,

 

but i think I'm well over my head with this one, my page says is successfully uploaded but there is nothing in the Leads table I created so its not putting the data into the table.

 

here's my code so far 

<?php

$used=0;

error_reporting(E_ALL);
ini_set('display_errors', '1');

if (isset($_POST['submit'])) {
	
	require "database/pdo_connection.php";
	
	//upload file
	if (is_uploaded_file($_FILES['fileName']['tmp_name'])) {
	echo "<h1>" . "File " . $_FILES['fileName']['name'] . " uploaded Successfully." . "</h1>";
	
	}
	
	$filename = $_FILES['fileName']['name'];
	//Now Lets import the file to the database/connection
	$handle = fopen($_FILES['fileName']['tmp_name'],"r");
	
	$import=$pdo_connection->prepare(
	"INSERT IGNORE into Leads(number,date,used,fileName) values(?,?,?,?)"
	);
	
	while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
		$import->bindParam(1, $data[0], PDO::PARAM_STR);
		$import->bindParam(2, $data[1], PDO::PARAM_STR);
		$import->bindParam(3, $used, PDO::PARAM_INT);
		$import->bindParam(4, $filename, PDO::PARAM_INT);
		$import->execute();
	}
	fclose($handle);
}	
else {
	
	print "<div id='box'>";
	
	print "Upload new csv by browsing to file and clicking on Upload<br/>\n";

	print "<form enctype='multipart/form-data' action='Leads2.php' method='post'>";

	print "File name to import:<br/>\n";

	print "<input id='test' size='10' type='file' name='fileName' ><br/>\n";

	print "<input type='submit' name='submit' value='Upload'></form>";

	print "</div>";
}
?>

Thanks in advance for any help given

Link to comment
https://forums.phpfreaks.com/topic/296657-pdo-import-to-table/
Share on other sites

As a side note, if your database interaction is limited to one to two queries, or if you're comfortable with procedural(or Object Oriented) style, mysqli_ is good for that.  If you prefer Object Oriented; or need to use multiple database drivers (mysql, prostgresql, etc) in one application; or need to use transactions, PDO is your choice.  Pick the best API (mysqli or PDO) for the right job.

 

Both mysqli and PDO have optimization when used with prepared statements.

PDO does offer more flexibility with results and transactions are a nice feature when you need to do more than one process during your script execution. 

Link to comment
https://forums.phpfreaks.com/topic/296657-pdo-import-to-table/#findComment-1513262
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.