how a website user interacts with a database? - php

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.

Related

Insert Multiple Values > One Column from POST Form

I have a form with this select:
<select name="territory[]"><option vaue="None Selected">Please Select</option>
<option value="Argentina">Argentina</option>
<option value="Australia">Australia</option>
// And so on...
</select>
Next to the select option is a button where they can add another country with the same select options. Now when they submit I want them to go into one column in the database called COUNTRIES. So if a person selects 3 countries such as Argentina, Australia and New Zealand, they will display in the database column countries like:
COUNTRIES
------------------
Argentina, Australia, New Zealand
I have been looking and UNION has popped up but I am unsure how to use this as other fields are single entries.
INSERT into b_project (NAME,COUNTRIES, CLIENT)
VALUES ('$_POST[name]','$_POST[countries]','$_POST[client]')
I have done it before where I have inserted them onto new rows in the table using:
foreach ($_POST['name'] as $index => $id)
Then the insert statement with '".$_POST['countries'][$index]."'
But I need all the values in one column. Your advice would be much appreciated.
The simple (but a workaround) answer:
$countries = implode(',', $_POST['countries']);
This will glue the countries with a comma.
The way you should do it:
Use another table in your table, in which you provide all the countries.
Then when a user submits the form, you need another table which map the country_id with the b_project_id. For each country you will have a record in that table.
It little bit more work, but that's the way it should be done.
Last piece of advice:
Please sanitize the inputs. You do not know for sure what a user will send with the POST data. It can be everything...

Populating select list

I have 3 columns in a table in db: ID, TITLE, TEXT and that will populate form select options.
Depending on user selection, script will take title and text and with some changes put all that into a different table.
The question I'm asking is, how efficient is to populate select option like this?
<option value="TEXT"> TITLE </option>
This seems like the most easiest way but text can be quite long (more that 500 characters), and there will be like 10 titles to choose from.
Second thing I'm thinking is to do it like this
<option value="ID"> TITLE </option>
and then when user selects, script will need to again call database to fetch row with corresponding ID.
What is better and efficient with less code? Is there something else that could be done?
I think (IMHO) that the "id" solution is the better EVER!
This because what the user need to see it's different on how you manage this, you think with id, the user think with "description" so, you need the id and the user need the "title".
The best and more efficient is this one for me.

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