last day (15 days later) » 

11:47 AM
-1
Q: Get data from MySQL database right after submiting it

Rafael LopesSo guys, I'm bulding a website that allows users to upload a picture and then get the EXIF info shown in a user-friendly way. I also want to make available an option for users to be able to share their picture online. At the moment I already have the JavaScript for the image preview fully worki...

 
cjc
Are you doing the submission via AJAX?
 
How do you have confirmation that the picture has been correctly uploaded? The PHP script could return a JSON object that contains the path which you could then parse in javascript. No need to do an extra select call!
 
To get solid suggestions in comments or possible a answer i would suggest that you add table structures and the application code which might be server side or client side.. As without the question is unclear and will be most likely to voteclosed.i would suggest you to read how to ask
 
@cjc I'm using PHP to do it. PS: Added the code of the upload script
 
No, cjc means how are sending the request from the browser? You use PHP to process it when the server receives it. But in the browser you could be sending it via AJAX, or via a normal form postback. The way you're doing that part might make a difference to what a suitable solution should look like. It sounds like you're probably just using a normal form submit, without involving JavaScript or AJAX, but it's better if we're certain.
P.S. you've shown us the process up to the point where you generate a SQL query. But you haven't shown us how you execute that query or what happens afterwards. Presumably you continue to generate some HTML for subsequent display, within the same PHP script? If so, then you don't need to get any information from the database, because your script already knows it - the image path you need is http://localhost/geoPic/photos/".$newfilename
 
11:47 AM
@ADyson I have a separate index page where the info would be shown, yes. I'd like to have an empy textarea that would then receive the path after the upload is done. <textarea id="link_foto"></textarea> I would then add a button below that textarea that would "trigger" a function to copy the path from the textarea to the clipboard. Also, yes, you are right in saying I don't actually need to get the path from the database as I already have it. The thing is... this project has to have some sort of database interaction. By doing it the way you sugested, the database would be redundant
 
"this project has to have some sort of database interaction" ...why, is it a student project where you're supposed to show off your database skills?
 
@ADyson Nah. More like a nonsense requirement from the teacher... If the idea was to test my database skills, a simple table with 3 columns, an insert statement and a simple select would be too simple :P
 
Warning: You are wide open to SQL Injections and should really use parameterized prepared statements instead of manually building your queries. They are provided by PDO or by MySQLi. Never trust any kind of input, especially that which comes from the client side. Even when your queries are executed only by trusted users, you are still in risk of corrupting your data.
 
Ok so you have a separate index page. What happens when your script above completes? Are you wanting to redirect the user to the index page, and show the recently inserted photograph?
 
@ADyson I want the user to stay on the index page for the whole process... He loads the image. The image appears alongside a button to share it if he wants... If he presses that button the image gets uploaded and he should get a link to then share it where he wants... all without leaving the page because the user may also want to load the EXIF info.
 
11:47 AM
Right so all of the PHP and HTML code above is also from your index.php script, is that correct? The structure is not fully clear
 
@ADyson The HTML is a file called index.php. The PHP is a file called upload_2.php. then I have a few files for Javascript and EXIF manipulation
 
I see. So it seems you are uploading the file via AJAX, so that means you won't leave the index.php page. So you just need to make the PHP script echo the path of the image. Then you need to receive that in the response to the ajax request, and use JavaScript to display it on the page. All pretty standard AJAX usage patterns. You don't need the database for that little bit of functionality, but of course you do need to store the image in your database so you can use it another time in your site.
 
@ADyson I don't need to use the images on my site again... But can I do the path bit using the database? I really can't make it look redundant, even if it is... I know it sounds stupid, but I only came to the conclusion that the database is kinda useless for my needs a bit too late. Yet, I still need to make it look like its needed :/ And it is a bit too late to add some kind of functionality to the site that would justify the use of the database...
 
Sure you can, although it's ridiculous. Does your photo table have an auto-increment ID field? If so then after you've run the INSERT, use mysqli_last_id() to get the ID of the newly created row. Then you can use that ID in a SELECT query to get the row back out again, and fetch the path from the field, and return it. Anyone reviewing that in any detail though will pretty quickly spot that it's a pointless step
The only other thing you could maybe do to make the DB look useful is associate the photo with the user who uploaded it (assuming your site has a feature allowing people to register and log in, that is). And then a user can manage the photos they've uploaded, adding/deleting/renaming them or whatever. Just an idea.
 
@ADyson It does have an auto-increment ID, yes. Yes, I know, the user idea would make it useful, but was never intended for this website. The website will probably never go online as I'll just make the presentation using XAMPP but the idea is for it to be a quick and simple exif viewer that also allows the user to upload and share his picture...
@ADyson Also, 1 more question, that is actually the main one. I want to return the path, not in the form of an alert or something but to a text-area element. What would be the best way to implement that?
 
11:47 AM
Since you're using AJAX, just echo the value in PHP. This will be returned in the response to the AJAX request. The browser receives this value and passes it back to the JavaScript. Normally you can declare a "callback" function in the JavaScript, which is a function to be run at a later time when the response from the server is received. In that function you can read the response value coming back from the server, and set it as the content of your textarea. This is the standard thing to do in an AJAX scenario.
Since it appears you're using the ajaxForm plugin, you can see on the documentation page at jquery.malsup.com/form/#options-object that you can define a "success" option to be passed to the ajaxForm function, and that option can contain a callback function. or you can event set the "target" option containing info about the page element you want to update with the server response, and the ajaxForm code will handle it automatically for you
 
@ADyson Thanks. I'll also need to modify the ajaxForm bit of code to only show the "Image Uploaded" when it is actually uploaded... as at the moment it shows it even if I modify my PHP in a way to not make it work.
 
Yes...you can use the "success" and "error" callback options to differentiate between a HTTP success code (200 OK) and any other HTTP error status (e.g. a 500 Internal Server Error which might occur if PHP crashes). If PHP encounters a predictable problem (e.g. invalid file type) it should probably change the HTTP status to 400 (Bad Request) and return a suitable error message, which you can display on-screen using JavaScript.
 
 
7 hours later…
7:07 PM
Ok... so, the Ajax bit is working.. it verifies the submission as it should and also already targets the textarea as I wanted.

Now, if I just use the $filepath variable I created to send the path to the textarea it works but I'm not able to figure how to do it via a DB select...
$link_foto = mysqli_query($link,"SELECT path FROM carro WHERE name = '".$newfilename."'");

$result = mysqli_query($link, $link_foto);



Shouldn't this work?
 
7:28 PM
Alright... forget it...


I fixed it using this code:


$result = mysqli_query($link, "SELECT path FROM photo WHERE name = '".$newfilename."'");

while ($row = $result->fetch_assoc()) {
echo $row['path'];
}
 

last day (15 days later) »