php to my SQL insert [closed] - php

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
i have a small problem which i cannot find a way to overcome..
i am making a website for car's dealer, i made an insert page which inserts the cars details, i want the car's picture to be saved in a different directory for each car with the name of the folder's name being the car's ID.. i had no problem setting it up this way.
the problem is:
If i take the last ID in the database on the insert page first load then add one to it and put it in the insert statement there is no guarantee that when i upload the data the ID will be free anymore.. even if the last ID is taken at the update time as after the query comes back there could have been an insertion before the DB receives the data for insertion... the problem here is it will not be possible to insert the record anymore... the question here is: is it possible to reserve and ID somehow?
If i made it insert the record first then go to another page for image insertion then how will i be able to track it down, i wont have a way to correspond the uploaded images to the ID since there might be an insertion after my insertion and the last ID will no longer be for the car i have uploaded, so if i get the last ID and upload the images then the images will be uploaded to the wrong directory... the question here is: how can i track it?
or is there another way of doing this?

Use MySQL AutoIncrement feature in the DB table, for ID column
http://dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html
And then use mysqli last insert ID, to get the last ID from DB
http://php.net/manual/en/mysqli.insert-id.php
When you have that last (latest) ID, you can reuse it in your PHP in all places you need

Related

PHP Create temporary link to page/file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 months ago.
Improve this question
I am working on a website project, where you can login.
I want to make a system where you can request a file, but the link to the file should only be valid for a certain amount of time. After the certain amount of time the link should expire and stop working (maybe a show a 404 error).
-I am using php and MySQL database
Thank you!
BTW: I am new on this platform
Start with these 2 tables.
Whenever you upload file or manually add it, add an entry in the files table.
Whenever someone request a file, generate a link for it in links table like https://host/download.php?id=the_random_string which will point to the real file_id, with an expiry date.
files
id
name
physical_path
links
id
file_id
random_string
expiry
So now you should have a file download.php (pseudo code)
$str = $_REQUEST['id'];
$sql = "select file_id from links where random_string=$str AND expiry hasn't passed";
$file_id = $row->file_id;
$sql = "select physical_path from files where id = $file_id";
$physical_path = $row->physical_path;
do_download($physical_path);
You can accomplish that using two ways...
First Way
You can add timestamps on the URL where this URL is valid for a specific time (You must take care of changing this timestamp by the user) so, I recommend adding the timestamp as an arbitrary/inapprehensible string where the user can not change.
Second Way
You can add the timestamp in the database with each file and before accessing the file you must check the stored timestamp if was exceed the current timestamp or not.

PHP - how to generate a URL on save like JSFiddle does? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm trying to recreate something you might know from imgur or jsfiddle - you just type something or upload images, then you click "save" or "upload" and then you get a original link in form of imgur.com/smth or jsfiddle.net/smth. How is this done? I've created simple website where I can write notes and save them and it works perfectly well for myself, but I wanted to share it with some of my friends and I have absolutely no idea how can the "save" button attach their changes to new URL every time they press it? I know this is pretty basic but I couldn't find anything on the matter online and I really have no idea how to handle this issue, excepting I'll be writing their changes to different SQL rows.
Thanks for any clues :)
If you're trying to create something and let people edit it and keep the same link you could do it with PHP and mySQL.
I think the easiest way would be to have one page to create the initial note and one to update/view it. Once you create it the save button would add it to a database with a unique id of some kind like an auto increment number. To send the page you would use a link like www.somepage.com/info.php?id=1. The "id=1" part would be the unique id within the database. In info.php, you would need to use $get_["id"] and set that to equal the database id row and load the info from there. Then you could allow edits to that row from that page. You would have to set the save button to update the row.
To get something like somepage.com/smith, if you're on a linux server, you would use htaccess and mod_rewrite.
This is a really down and dirty gist of how you could do this, but it should send you in the right direction.

Codeigniter linking pages to each other [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
This may be a bit confusing to you but I'll try to make it as simple as possible.
I am working on a simple project where I have only one type of content coming from my database. When a user lands on my website they will be seeing one of random entries as home page view and then they will be able to click on next and previous to see other available pages.
It may work like pagination, but the difference is that pagination pulls all entries from database and lists them based on number of listing per page. My approach is the exact article page to have only next article and previous article links on every page.
Would you please let me know how would you approach this if it was your application? I'm looking forward to hearing your thoughts.
Thank you.
you have to build a query which returns max three rows - one for prev data, one for current data and another one for next data.
Now you have to check if prev data not found in the query result then do not show prev link in the page
Also you have to check if next data not found in the query result then do not show next link in the page.
Query for first page will return two rows and for next page onwards will return three rows. for last page query will return two rows.
Depending on the which links getting clicked you have to build the query.
For first page you return the query with first and next records from the db. from next page onwards you have to get the clicked link's id and determine the prev and next data for next page onwards.

Save client-side changes to server [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I have a very simple site (PHP) that allows user changes (via jQuery) that modify elements' classes, and add/delete rows from a table.
I want to write these changes to the actual PHP file in question on the server every time a user makes a change, so that when the page is refreshed or revisited, the changes remain intact.
What's the simplest / best way to accomplish this? Thank you!
For addition/deletion of rows, fire ajax call and update records associated with the user. This is in addition to client-side (jQuery/JavaScript) changes to page. Whenever they revisit/refresh the page, data will be fetched from db, and as such will always be latest.
When you say that users modify elements' classes, I assume you mean their page's looks and styling (like some sites have on-page options to change background color, font-sizes). If that is the case, I suggest you:
Create a list of all possible states and store them in a new table in db, say display_options. You could store details like option_type (e.g. background color, font-size etc) and option_back_value (e.g. #FFF, #000 etc).
Create a new table say user_display_options where you store things like user_id, option_type and their chosen option_id.
While loading page, do a join on these tables using user_id. Then while creating your page, conditionally add classes etc to the page.
When user edits page, fire ajax call sending required data like user_id, option_type, option_id. As mentioned for other task, these changes to db will be in addition to client-side changes.
Also read #David 's comment to your question - it clears an important concept.

Displaying questions in a questionnaire using PHP?

I am building an online questionnaire. In this questionnaire, the user should see only one question at a time per page. When he sees the question, he will provide an answer and after that he will press on next to go to the second question and so on until the end of the questionnaire. I have already built the questionnaire but I am stuck now on the part" how i can display only one question from the database?". Also, when the user press on next, should i save the answer immediately to the database or I should wait until he completes answering all the questions and save them at the end to the database?
If anyone can help in that, please provide me with the appropriate technique to do that in PHP.
Use a counter variable that matches all the question ids (hope you have unique ids for each of your question).
And then loop through that variable and for each counter value fetch the data from the database using where id = ? clause where ? will be replaced by the counter variable value.

Categories