Note that there are some explanatory texts on larger screens.

plurals
  1. POhow do I make CSV Uploader select certain values if CSV file is put in different order
    primarykey
    data
    text
    <p>I am currently working on an CSV Uploader project and I have it working correctly but my question is - How would I make it select (firstname, lastname, email, phone, etc) if clients CSV file is put together differently. Or would the client who is uploading the file have to have it in the order the program is made?</p> <p>Here is php code ----------------------------------------------</p> <pre><code>try { # MySQL with PDO_MYSQL $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password); $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); } catch(PDOException $e) { echo "I'm sorry, I'm afraid I can't do that."; file_put_contents('PDOErrors.txt', $e-&gt;getMessage(), FILE_APPEND); } if (isset($_POST['submit'])){ $file = $_FILES['files']['tmp_name']; $handle = fopen($file , "r"); while (($fileop = fgetcsv($handle,1000,",")) !== false) { $firstname = $fileop[0]; $lastname = $fileop[1]; $email = $fileop[2]; $phone = $fileop[3]; $insertdata = $DBH-&gt;prepare("INSERT INTO csv (firtname, lastname, email, phone) VALUES ('$firstname','$lastname','$email','$phone')"); $insertdata-&gt;execute(); } if ($insetdata){ echo "Successfully Uploaded. Thank you."; } } </code></pre> <p>Thank you for your time!</p> <p>UPDATED----------------------------------------------</p> <p>New code</p> <pre><code> try { # MySQL with PDO_MYSQL $DBH = new PDO("mysql:host=$hostname;dbname=$database", $username, $password); $DBH-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); } catch(PDOException $e) { echo "I'm sorry, I'm afraid I can't do that."; file_put_contents('PDOErrors.txt', $e-&gt;getMessage(), FILE_APPEND); } function returnBack(){ header("Location:http://pcgprotects.com/PCG/csvuploader/csvuploaderform.php?seterror=1"); exit; } if (isset($_POST['submit'])){ /*******************************GET NAME OF COLUMNS IN CSV FILE *********************************************************/ if (empty($_POST['files'])){ returnBack(); } if (empty($_POST['firstname'])){ returnBack(); } if (empty($_POST['lastname'])){ returnBack(); }if (empty($_POST['email'])){ returnBack(); } if (empty($_POST['phone'])){ returnBack(); } $file = $_FILES['files']['tmp_name']; $handle = fopen($file , "r"); $fileop = fgetcsv($handle,1000,","); $firstname_index = array_search($_POST['firstname'],$fileop); if (empty($firstname_index)){ returnBack(); } $lastname_index = array_search($_POST['lastname'],$fileop); if (empty($lastname_index)){ returnBack(); } $email_index = array_search($_POST['email'],$fileop); if (empty($email_index)){ returnBack(); } $phone_index = array_search($_POST['phone'],$fileop); if (empty($phone_index)){ returnBack(); } /***********************ASSIGN COLUMN VALUES TO ACCORDING VARIABLES AND INSERT THEN INTO CSV TABLE IN DB *************************************/ while (($fileop = fgetcsv($handle,1000,",")) !== false) { $firstname = $fileop[$firstname_index]; $lastname = $fileop[$lastname_index]; $email = $fileop[$email_index]; $phone = $fileop[$phone_index]; $insertdata = $DBH-&gt;prepare("INSERT INTO csv (firtname, lastname, email, phone) VALUES ('$firstname','$lastname','$email','$phone')"); $insertdata-&gt;execute(); } if ($insetdata){ echo "Successfully Uploaded. Thank you."; } } </code></pre> <p>In my csv trial file i used columns named - First name, Last name, Phone, Email. Then in my form I entered those values but I got kicked back - here is my Form code -----</p> <pre><code> &lt;table width="300" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"&gt; &lt;tr&gt; &lt;form method="post" action="csvuploader.php" enctype="multipart/form-data"&gt; &lt;td&gt; &lt;table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"&gt; &lt;tr&gt; &lt;td colspan="3"&gt;&lt;strong&gt;CSV Uploader &lt;/strong&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Enter Name of First Name Column &lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;input name="fisrtname" type="text" &gt;&lt;?php $setError=$_GET['seterror']; if($setError == 1){ echo "&lt;span class='errorMsg'&gt;Need Exact Name&lt;/span&gt;"; } ?&gt; &lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Enter Name of Last Name Column &lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;input name="lastname" type="text" &gt;&lt;?php $setError=$_GET['seterror']; if($setError == 1){ echo "&lt;span class='errorMsg'&gt;Need Exact Name&lt;/span&gt;"; } ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Enter Name of Email Column &lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;input name="email" type="text" &gt;&lt;?php $setError=$_GET['seterror']; if($setError == 1){ echo "&lt;span class='errorMsg'&gt;Need Exact Name&lt;/span&gt;"; } ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td&gt;Enter Name of Phone Number Column &lt;/td&gt; &lt;td&gt;:&lt;/td&gt; &lt;td&gt;&lt;input name="phone" type="text" &gt;&lt;?php $setError=$_GET['seterror']; if($setError == 1){ echo "&lt;span class='errorMsg'&gt;Need Exact Name&lt;/span&gt;"; } ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;tr&gt; &lt;td width="294"&gt;&lt;input type="file" name="files" /&gt;&lt;?php $setError=$_GET['seterror']; if($setError == 1){ echo "&lt;span class='errorMsg'&gt;Select a File&lt;/span&gt;"; } ?&gt;&lt;/td&gt; &lt;/tr&gt; &lt;br /&gt; &lt;tr&gt; &lt;td&gt;&lt;input type="submit" name="submit" value="Submit" /&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/td&gt; &lt;/form&gt; &lt;/tr&gt; &lt;/table&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload