1

I'm doing a final project similar to Mashup in which I want to implement an upload function on the webpage so certain data can be uploaded to mySQL. ( It's hosted locally using MAMP currently).

So far I've written a program to parse the CSV format specific to my needs if given the file.

so in pseudo code this looks like

***1. get file from user (I've been testing with a sample file in the same directory, so this is what I'm having trouble implementing)
2. parse data from CSV
3. for all lines in file:
    4.send relevant data to google geocoder api
    5.parse returned JSON
    6.create and style variables to be sent to mySQL
    7.put these variables into a query
    ***8.send query and update table

I've implemented all but the starred steps. And for those I've been trying the following code:

for (1) I input this on my index page:

<form enctype="multipart/form-data" action="geocoder.php" method="POST">
<!-- MAX_FILE_SIZE must precede the file input field -->
<input type="hidden" name="MAX_FILE_SIZE" value="30000" />
<!-- Name of input element determines name in $_FILES array -->
Send this file: <input name="userfile" type="file" />
<input type="submit" value="Send File" />
</form>

which I found on stackexchange. It, on the UI side of things, does great. It opens a window from which you choose the relevant file and press submit.

also for (1) I used the following code in my geocoder.php file to define the file:

if($_FILES['userfile']['error'] == 0){
    $file = $_FILES['userfile']['name'];

//open file
$fp = fopen($file, "r");
if ( !$fp ) {
  trigger_error('fopen failed', E_USER_ERROR);
} 

for (8) I'm using the mysqli method from php.net:

  $sql = "MY FORMATTED QUERY";
  $mysqli = new mysqli("localhost", "root", "root", "data");
   $result = $mysqli->query($sql);
        $row = $result->fetch_assoc();
        echo htmlentities($row['_message']);

And it's probably just due to my lack of understanding about how the file uploads to the server. Reading up on it in the general case doesn't seem to help me make it actually run, though. I can tell it's not inputting the data because I can just refresh phpmyadmin.

It's really difficult, I've found, to debug something when the only info you have about the process is that it's not working. Until now I've just been taking shots in the dark and hoping it works eventually. Do you have any ideas about what is wrong? Or is there a way (similar to dump in pset7) that I can use to help see what's going wrong behind the scenes?

0

This may seem like a very oblique answer, but here's some great general advice about debugging from a book I'm currently reading. In case I don't get an opportunity to look more deeply into your issue tonight, I hope this will give you some fresh ideas on how to approach the debugging process.

Having said that, it does occur to me that you could comment out the mysqli part at the end, and instead just print the formatted query to the screen instead. And if you can do that, you can also test that query using the interactive mysql50 shell interface.

Good luck!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .