Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing a PHP session variable within a MySQL file run in PHP?
    primarykey
    data
    text
    <p>It might sound a bit confusing, and perhaps what I'm doing isn't right at all, but here goes.</p> <p>Our users can upload .csv files containing their school's data, exported from an external management system called SIMS. Once they upload that file I want to run a .sql script that updates our database with the information they provide in this file. What I've done, on the upload file page (works like a charm), is create a session variable containing the file path and name of the uploaded file, as shown below (file path is blanked out):</p> <pre><code>$target_path = "xxx"; $target_path = $target_path . basename( $_FILES['uploadedfile']['name']); if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) { echo "&lt;p&gt;The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded.&lt;/p&gt;" ; echo "&lt;p&gt;&lt;a href='sqltest.php'&gt;Click here&lt;/a&gt; to upload your files into the database.&lt;/p&gt;" ; $_SESSION['file'] = $target_path ; } </code></pre> <p>Below is a portion of my .sql file operated within PHP containing the session variable. Because most of this populate script uses the same kind of commands throughout it will probably not make sense to show you all 973 lines of code (of course there is a session_start() active at the top of the page).</p> <pre><code>$filename = $_SESSION['file'] ; mysqli_query($dbc, 'SET foreign_key_checks = 0') ; $populate = "CREATE TEMPORARY TABLE IF NOT EXISTS `mldb`.`TempSchool` ( `CentreNo` INT UNSIGNED NOT NULL, `School` VARCHAR(255) NULL, `Street` VARCHAR(255) NULL, `Town` VARCHAR(255) NULL, `County` VARCHAR(255) NULL, `Postcode` VARCHAR(10) NULL, `Tel` VARCHAR(45) NULL, `URL` VARCHAR(512) NULL, `Email` VARCHAR(255) NULL, `Headteacher` VARCHAR(255) NULL, `LEA` VARCHAR(45) NULL, `LEANo` INT UNSIGNED NULL, `EstablishmentNo` INT UNSIGNED NULL, `URN` INT UNSIGNED NULL, `Governance` VARCHAR(45) NULL, `Phase` VARCHAR(45) NULL, PRIMARY KEY (`CentreNo`) ) ENGINE = InnoDB ; LOAD DATA INFILE '$filename' IGNORE INTO TABLE `TempSchool` FIELDS TERMINATED BY ' , ' OPTIONALLY ENCLOSED BY ' \" ' LINES TERMINATED BY ' \r\n ' IGNORE 1 LINES (etc...) </code></pre> <p>I'm getting an error on line 25 of the code (LOAD DATA INFILE '$filename') displaying the following:</p> <pre><code>Invalid query:You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'LOAD DATA INFILE 'C:/Users/Public/Dropbox/mlwebfiles/Trial/uploads/MarksLive Set' at line 25 </code></pre> <p>Presumably it's got something to do with the fact that, even though it's recognizing the file path and file name stored in the $_SESSION['file'] variable, but it's not actually doing anything with it. Is there a way where this .sql file can operate with through PHP set up variables for the file names? Many thanks!</p>
    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.
 

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