Upload issue involving two MySQL tables - php

I have an upload photos script and before confirming the upload you'd need to select an photo album (different table) that was echoed out in a drop-down.
What kind of query do I need to write for it to send (what I think would be the best) the ID of the photo as it were to be uploaded to some kind of third table where it would hold the ID of the album and photo, so I can echo out that it will only show those pictures in the selected album name.
I've been stuck with this for days and I don't know how to proceed, because I know it's an issue if you want to use multiple tables in MySQL but for me it's too late to since I made everything in MySQL instead of MySQLi.

I don't think you need a third table if there is not any other reason to what you said.
You can basically carry out the album id from the value of select dropdown.
Upon submit capture this album id along with your photo.
<select name"album_id">
<option value="1">album one</option>
<option value="2">album two</option>
<option value="3">album three</option>
</select>
//Upon submit
capture it like this:
$album_id=$_POST/REQUEST['album_id'];
//Place this variable value into your SQL code.

Related

how a website user interacts with a database?

I'm new to all this, and I can't seem to find the right search on google to get the answer I'm looking for. I have a front end that a user will use to create a new transaction. There is a drop down menu tied to a "categories" table that has a 1:M relationship with the "transaction" (see image). My question is, if a user submits this form to save a new transaction, they don't know the ID from the categories table, all they'll be doing is selecting one from a drop down that's pulling from the categories table. All I know how to do at this point is take their input from the drop down and store it into a field in the transaction table. I'm trying to understand how to use relationships, but in my limited understanding of how keys work, it seems that unless you knew what the ID number was, then the relationship does no good. If all you're doing is taking the submission from the drop down and storing it in the transaction table, then you've still just got the category name stored in 2 different tables, so what's the point? I'm missing a step in here somewhere and I'm hoping someone can help make sense out of this.
The user wouldn't know the Category ID. (Though they could if they wanted to. It's in the markup.) More importantly, your code would know the Category ID. When creating that drop down list (the select element), your code would be populating it with both the ID and the Name. The ID is used for the value, the Name is used for the display text.
The resulting markup might look like this:
<select name="category">
<option value="1">First Category</option>
<option value="2">Second Category</option>
<option value="3">Third Category</option>
</select>
And so on.
When the user selects one, they're choosing based on the text. But the actual form will post the value of the selected option back to the server. So if the user selects Second Category then in your server-side code $_POST["category"] will contain the value 2, which is the ID you need to insert into your Transaction record.

How to post values from multiple drop down menus in the same form

I have a form with a unique identifier for each row. I also have a drop down menu with user names.
basically each row has a task and next to each task a drop down menu with name of the task owner. what I need to do is to be able to POST the users name along with their task id so I can update the owner of the task in one submit.
let say the following is what I have in my form
ID Title MENU
1 Task 1 (please select)
2 Task 2 (please select)
3 Task 3 (please select)
make_change
if the user selected "Mike" in row 1 then I want to update the database with Mike's ID which is Mike's option value in the drop down menu.
The thing that I am having a difficulty with in trying to link each menu to the correct row id "task id".
this is how my menu code is displayed
<select name="user_id[]">
<option value="200">Mike</option>
<option value="205">Jaylen</option>
<option value="220"> Jack</option>
</select>
I want to be able to link each menu to the correct task id so when I post the form I can link each menu to the correct row.
How can I include the task id in each menu?
Thanks
So, my understanding is that you need to know which Task ID is related to which select?
Assuming that you create the selects in PHP you could simply change
<select name="user_id[]">
to
<select name="user_id[TASK_ID]">
where TASK_ID is the actual id for the task... (e.g. 1, 2, or 3 etc).
Then when you submit to PHP you could loop through your $_POST[user_id] with for example a foreach loop...
foreach($_POST['user_id'] as $taskid => $userid){
//Do something with the information....
}
UPDATE
If you know the TASK_ID you don't need to iterate through the entire post you can simply access the required field using: $_POST['user_id'][TASK_ID]
For example...
echo $_POST['user_id'][1]; //Echo's the user id for task 1
echo $_POST['user_id'][8]; //Echo's the user id for task 8
Instead of making the name for the select tag an array you can make them unique names. This way each row can be uniquely identified. Example:
<select name="task1">
<option value="200">Mike</option>
<option value="205">Jaylen</option>
<option value="220"> Jack</option>
</select>
<select name="task2">
<option value="200">Mike</option>
<option value="205">Jaylen</option>
<option value="220"> Jack</option>
</select>
This way task1 will map to whatever is selected in the dropdown for task1 and the same for the task2 dropdown.

PHP/SQL - Using a Select element to display/link to rows in a database

I'm building a CRUD app with PHP, and I need some help trying to simplify the user experience. Basically, the user creates a new "Project" with a pre-assigned ID number. This ID number acts as the primary key. Upon submission of the form, a row is created in a "Projects" table in my database.
After the project is created, I want to use another form to add storage transactions to the project (for contextual purposes, I'm tracking warehouse storage). Initially, was going to have the user re-add the ID number (the primary key entered when the project was created), but there are lots of opportunities for human error if the ID number is entered incorrectly.
Current Task: I want to create a "Select" element on my form that lists all ID numbers that have been entered. Then, I can just select one of the projects from that drop down and have the transaction automatically applied. How can I do this?
Thank you in advance for your help!
First, you'll have to do a query (using PHP) to pull a list of all the project records in the project table from that database. When you do the query for that, you'll have a result set as an array of each project in the projects table that you can iterate through using a loop to populate a drop-down box.
First get the projects (assuming a column name of id that you're using for the drop-down selection):
<php $projects = mysqli_query($db, "SELECT id FROM PROJECTS_TABLE"); ?>
Then, to display the drop down selection box:
<form name="formWhatever">
...
<select name="project_id">
<?php foreach ($projects as $project) {
$result = mysqli_fetch_array($projects, MYSQLI_ASSOC)?>
<option value="<?php echo $result[id]; ?>">Project #<?php echo $result[id]; ?></option>
<?php } ?>
</select>
...
</form
I think I also like #pajaja's idea for using an index page to eliminate a long drop-down box as well.
Well all you need to do is what you described, so I'm not sure if i understood your question exactly. Create an select menu in the form and populate it with all projects. You need to get all project IDs and their names from the database first and generate HTML options for each project ID.
<select name="project_id">
<option value="1">Project 1</option>
<option value="2">Project 2</option>
<option value="3">Project 3</option>
<option value="4">Project 4</option>
...
<option value="n">Project n</option>
</select>
in PHP you will have something like this for each project
echo "<option value='{$project['project_id']}'>{$project['name']}</option>";
and then use the $_POST['project_id'] to get the project_id of project to which you want add storage transaction.
Hints:
Why are you preassigning project IDs? How are you assuring that you don't issue the same preassigned key twice? You can set the database column to be auto incremented on each insert. Also I think that it is better to first display a list of all project, like index page, and then when the user clicks on desired project you present him the option to add storage info (or whatever else you have planned). Having a general form and then selecting the project within will work but can get complicated if there are many projects, and i don't really see the advantage of that approach.

Output content of a form on the same page for public view

I'm trying to build a simple form with only dropdown option like
<select>
<option value="volvo">Volvo</option>
<option value="saab">Saab</option>
<option value="opel">Opel</option>
<option value="audi">Audi</option>
</select>
All I need is when the user selects one of the options, the option to be automatically submitted (without having to click submit) and posted on the same page for public display.
The selection of different users should be viewable as well and just appended to previous users' choices.
For example:
First user selects Volvo, then the page will display
Volvo
When second user selects Audi, the page will now append his result to the previous user's result
Audi, Volvo
Thus every visitor will see what the previous user chose.
And so forth. There should be a limit to how many choice are displayed by users, say 100, and beyond that the original choices will be truncated.
Thanks.
Try like this.. in database create table, and store there users choices.
And on every submit insert the choice into that table. When you show the page select from Your table with limitations ordering by inserted date desc, and filter Your data by that choices before printing or query.
You should be able to set the
onchange()
event in your select tag to a submission (directing to the same page) and then using passed parameters you can determine that the submission went through.

Autopopulate drop down from mysql

When trying to search for this specific thing I'd like to do, I end up only finding how to create the drop down menu from a database, which I have already done. I'm building a list to track completed tasks of a predefined number of people that are in their own table. The record will save the name of the person that completed the task What I'm looking for is as follows:
Employee Table:
--- (nothing selected)
Sara Johnson
John Doe
Mark O'Brien
Task Table:
Clean counters
Unlock doors
Turn on lights
So right now there is the task, then a drop down that puts the employees names in it. What I want is, if insert a record, then come back to the page, to automatically see if the task has someone that has completed it and automatically select it, but leaves the possibility to be changed from the same drop down.
Right now, it will delete the current record with whatever the user puts in, so if they didn't know if someone turned on the lights, it will erase it and replace it with "---." This makes the user have to look up the record, see if anyone put anything in, then, basically recreate the record as it was, then add whatever changes they needed.
I hope that makes any kind of sense.
I have some trouble understanding your problem but what I assume you are looking for is a way to set the default selection of a drop down field. In HTML you can do that by adding the attribute selected="selected" to the <option> tag. It would look like this:
<select name="foo" size="1">
<option value="1">Value 1</option>
<option value="2" selected="selected">Value 2</option>
</select>
If that is not what you meant, please explain further. :)

Categories