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.
Related
I need some idea of how is the best way to spellcheck brands. I have a table in MYSQL with the name of my products and a form that leads to php search in brands table.
But the some customer are input the name of products wrong and the search bring nothing in the results example:
The product is Heineken
The customer put Heikenem , hekeinem , heken
Another case too is Jack Daniel's
The customer input jack daniels, jack Danis
Sometimes we think only by one side, but if we really think in the roles of the subject, we can see another ways to reach the goal, in this case the goal is make the user avoid wrong inputs of specifics words in the search, for this case, the best way is use autocomplete because, you really don't want words that not exists in your database and the user really want result for things that you have in your database.
This must be something trivial but long to explain. I cannot form good question for google. I dont know if I should get this result in SQL query or I need to change PHP part which includes sql query. (My knowledge is small, I am making my own very simple planner website but im stuck on creating button for filtering options.)
Imagine I have table named “Records” with 3 columns “ID, Item, Category”.
In categories I write all data in CAPITAL LETTERS and categories can be repeating.
So example table Records looks like this(in columns layout like this: id-item-CATEGORY):
1-sleep-HEALTH 2-ate breakfast-FOOD&DRINK 3-drank
coffee-FOOD&DRINK 4-took eyedrops-HEALTH 5-tram to
work-TRAVEL
I know how to list all data from column Category, but I want to avoid repeating categories. I need to get list of categories like this (Health and Food&drink does not duplicate):
HEALTH FOOD&DRINK TRAVEL
So with this list I can than via php generate html select tag options.
In php part I would use something like this:
$query1=mysqli_query($db,"select * from Records asc”);
while($query2=mysqli_fetch_array($query1)){
echo “<option value=“.$query2[‘category_option’].”>”.$query2[‘category_option’].”</option>”
};
So the result HTML will look something like this.
<select>
<option value="HEALTH">HEALTH</option>
<option value="FOOD&DRINK">FOOD&DRINK</option>
<option value="TRAVEL">TRAVEL</option>
</select>
Can you please point me right direction? Or mention some best practice to reach this kind of functionality. Thanks to all which try to help.
Use DISTINCT in your SQL:
SELECT DISTINCT Category FROM Records
No duplicates will be returned.
Have a play with it on this link https://www.w3schools.com/Sql/sql_distinct.asp
Note that now we are only fetching one column here instead of *.
You can also use GROUP BY:
SELECT * FROM Records GROUP BY Category
https://www.w3schools.com/Sql/sql_groupby.asp
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.
Is there a way to call field rows in a URL without using the column name??
So I currently have a posting site where users can select category or subcategories of choice from drop downs, how it's currently setup my site outputs links to the categories chosen such as..
topics.php?category=Food&sub_cat=Pies
topics.php?sub_cat=Pies
This allows users to go to either one of the links, or both
topics.php?category=Food&sub_cat=Pies
To give more functionality I am looking at adding textboxes instead of drop downs, the problem is users will more than likely enter the data in different boxes than other users, ie.
User 1. catbox: Food subcatbox: Pies
User 2. catbox: Pies subcatbox: Food
So in this case my current URL system won't return accurate results, so my question is would there be a way where "category" or "subcategory" could be replaced and just put the results together without them being listed in 2-5 different fields therefore not returning all the results that = to that value? "food" or "pie" in this example.
topics.php?xxx=Food&xxx=Pies
or
topics.php?xxx=Pies&xxx=Food
Looking at So homepage if you click "php" it will put php in the URL, click mysql and it will put "php+mysql" that sort of thing.
you can use parent child method in your database.your table would be like this
id - parent_id - category_name - depth
when you want to insert a data to your table it's depth will be one plus it's parent depth
when someone post to your page you first take query witch of the inputs has most depth then that will be your subcategory.
Calling field rows via parameters in your URL may be a very bad idea. It's a perfect way to allow a massive SQL injection attack. So, the answer is probably "yes, but HOLY MOLY PLEASE DON'T!"
Now it may be that your code is parsing these out on the back end and protecting them via any of a variety of methods, I can't tell from the amount of code posted.
I have read other answers on this (or at least near to this) subject but I couldn't get a clear view of it so I'm asking for help again.
I have a complex dynamic HTML form that I would like to submit to database using PHP. The form is split into multiple tabs and in each tab I got checkboxes that trigger other parts of the form. Example: at a point in my form I got a checkbox group that has options of: "hotel" and "restaurant". If I check hotels, I get another part of the form displayed, specific for "hotels". Same thing for "restaurant". So it's very dynamic here and I don't know which would be the best approach for storing every form field in database. Because it could contain 15 fields or 20, depending on the selection. Any example would be appreciated as I'm not that advanced with database design.
Thank you!
So it's very dynamic here and I don't
know which would be the best approach
for storing every form field in
database.
I apologise if I have misunderstood you here but I believe that you should design the database according to the data and not the form. It is difficult to comment without knowing the exact details of your situation so here is an example:
If you usually dump all the data from a form into a single table, but because sometimes this will involve submitting 5 values and other times this will involve submitting 10 and so you are unsure how many columns your table should have, then I think the problem is in the database design.
Work out what pieces of data are dependent on other pieces of data. For example, you mention checking "hotel" might open up more fields specific to that choice. Let's assume this involves things like "en-suite", "bed type" etc. Then you should have 3 tables, a registration table (assuming the user is using the form to buy these services), a hotel table and a registration_hotel table. The registration table will record a number of details specific to the registration only such as the customer's name and a unique id number. The hotel table will hold information specific to the hotel only, such as how many rooms have en-suite. The registration_hotel table will hold details specific to that registration at that hotel. You might want a column of type bool to record whether the user requested "en-suite".
When submitting the form, check which pieces the user entered with if(isset($_POST['hotel']) && !empty($_POST['hotel'])). Then only send stuff to the registration_hotel table if that condition is true.
If this design results in making too many separate calls to the database, you might want to look into transactions which will help you to manage the speed and security of these calls.
If you can post in a specific example of something you don't know how to do, that would be useful.
You didn't specify how you can manage this dynamic form. Can you edit it's PHP/HTML source? One great thing would be if you can label your different variables like hotel[], restaurant[], etc.
If your submitted form is clear enough (i mean semantically correctly structured) you can store the whole submitted form serialized.
Note: this method only working when you don't need to search for specific items in your database.
Edit: maybe i'm misunderstood your problem.
You can create a 'metadata' table like this:
form_id | option_name | option_value
---------------------------------------
1 | hotel | true
1 | restaurant | false