moosey_man1988 Posted June 5, 2015 Share Posted June 5, 2015 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 Quote Link to comment https://forums.phpfreaks.com/topic/296657-pdo-import-to-table/ Share on other sites More sharing options...
moosey_man1988 Posted June 5, 2015 Author Share Posted June 5, 2015 (edited) nevermind, I done it with mysqli instead, was MUCH easier. Edited June 5, 2015 by moosey_man1988 Quote Link to comment https://forums.phpfreaks.com/topic/296657-pdo-import-to-table/#findComment-1513247 Share on other sites More sharing options...
mac_gyver Posted June 5, 2015 Share Posted June 5, 2015 the bind statements (pdo or mysqli) don't belong inside the loop. the loop only populates the variables that were bound and calls the execute method. Quote Link to comment https://forums.phpfreaks.com/topic/296657-pdo-import-to-table/#findComment-1513255 Share on other sites More sharing options...
rwhite35 Posted June 5, 2015 Share Posted June 5, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/296657-pdo-import-to-table/#findComment-1513262 Share on other sites More sharing options...
Barand Posted June 6, 2015 Share Posted June 6, 2015 @rwhite35, Transactions are not limited to PDO, they are a function of the dbms, not the software used to access it Quote Link to comment https://forums.phpfreaks.com/topic/296657-pdo-import-to-table/#findComment-1513326 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.