Jump to content

Merging data into a MySQL database


michaelk46

Recommended Posts

I am not sure if this is a PHP or MySQL issue so I apologize if it's in the wrong forum.

 

I am trying to merge the data from a csv file to a MySQL Database after the file is uploaded to the server. It uploads and moves the file to the correct directory, but it just won't merge the data...

 

Here's the code

case 'csv':
							$destfile='C:\wamp\www\Test\Upload\Files\csv\\' . basename($_FILES['upload']['name']);
							$ret = move_uploaded_file($_FILES['upload']['tmp_name'], $destfile);
							switch ($ret)
								{
									case false:
										echo htmlspecialchars('Unable to move file', ENT_QUOTES, 'utf-8');
									break;
									default:
										$sql="LOAD DATA LOCAL INFILE '" . ($_FILES['upload']['name']) . "'
							             INTO TABLE page
								         FIELDS TERMINATED BY ','
							             LINES TERMINATED BY '\r\n'
							             IGNORE 1 LINES
							             (id, caption)";
										 echo ($sql); 
										 mysqli_query ($link, $sql);
										 include 'upload.html.php';
										 echo htmlspecialchars('File uploaded successfully', ENT_QUOTES, 'utf-8');
									break;
								}

						break;

 

Any help is greatly appreciated...

Link to comment
https://forums.phpfreaks.com/topic/187322-merging-data-into-a-mysql-database/
Share on other sites

You are correct... I did a test while trying to figure this out and posted the wrong code...

 

Here's the right code

case 'csv':
							$destfile='C:\wamp\www\Test\Upload\Files\csv\\' . basename($_FILES['upload']['name']);
							$ret = move_uploaded_file($_FILES['upload']['tmp_name'], $destfile);
							switch ($ret)
								{
									case false:
										echo htmlspecialchars('Unable to move file', ENT_QUOTES, 'utf-8');
									break;
									default:
										$sql="LOAD DATA LOCAL INFILE '" . $destfile . "'
							             INTO TABLE page
								         FIELDS TERMINATED BY ','
							             LINES TERMINATED BY '\r\n'
							             IGNORE 1 LINES
							             (id, caption)";
										 echo ($sql); 
										 mysqli_query ($link, $sql);
										 include 'upload.html.php';
										 echo htmlspecialchars('File uploaded successfully', ENT_QUOTES, 'utf-8');
									break;
								}

						break;

One thing I was thinking of was... The code for the form is posted below.

 

<form enctype="multipart/form-data" action="index.php" method="post">
<div> 
        <label for="upload">File to Be Uploaded:</label> 
        <input name="upload" type="file" size="45"/>
</div> 
    <p></p>
    <div>
 	<input type="submit" name="file_upload" value="Upload File"/>
</div>
</form>

 

I originally had this script just merging the data with the database and it worked... which is why this is so frustrating.

At that time, the input type was 'text'. I then modified it to move the file to a folder depending on the type of file it was. TO do that I changed the input type to 'file' along with  a couple other changes. I am thinking that's the reason why this is not working currently. Is there a way to assign both text and file to the input type?

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.