i have a table in mysql that contain courseid and coursename. I manage to fill the dropdown menu with courseid from table using php. Now i would like to display coursename each time user select courseid in droplist for example when they select cd123 system will display "multimedia" (information from db based on courseid). Thanx
<body>
<select id="coursecodeID">
<?php
include ('config.inc');
$sql = "SELECT * FROM coursetest";
$rs = mysql_query($sql);
echo "<option>-Please select-</option>";
while($row = mysql_fetch_array($rs))
{
echo "<option value=\"".$row['courseid']."\">".$row['courseid']."</option>\n ";
}
?>
</select>
</body>
Well you just need to do a SELECT Statement that gets the coursename by courseID
Example: select coursename from coursetest where courseid=(Here you put the selected index)
thats its
Feel free to ask me any questions
Simply use $row['coursename'] like this
echo "<option value=\"".$row['courseid']."\">".$row['coursename']."</option>\n";
This is a very basic question so I suggest you read on more tutorials/books/whatever you find useful.
You should know that the mysql_* functions are deprecated, meaning that they won't be developed any further. So in new code it is suggested to use PDO or MySQLi instead.
Do you want the text to show somewhere outside of the dropdown?
Related
I have SQL command, which will get list of all categories that I used within my table. (writting categories into "categories" column)
SQL:
SELECT category FROM `events` GROUP BY category
In PHPMYADMIN the result is correct, so I know my SQL command is good (each of them is separate category):
MEDICINE
SLEEP
SPORT
I need to get these values and wrap each of them in OPTION html tag, so I can use them on page in SELECTION element. I have a strong feeling I need to use PHP to generate something like this. Can you please inspire me with code?
Here is my attempt to at least see the list via PHP, but I have failed
<html>
<body>
<table>
<?php
include_once('config.php');
$query1 = mysqli_query($query, "SELECT category FROM `events` GROUP BY category");
//nefunkcny
echo $query1;
?>
</table>
</body>
</html>
This is what I would like to get, the ability to select the category on page
I would appreciate any advice on this, thank you in advance.
If further clarification needed, please ask.
Have a nice day.
use a while to fetch the results.
// Perform queries
$query1 = mysqli_query($query, "SELECT category FROM `events` GROUP BY category");
if (mysqli_num_rows($query1) > 0) {
// output data of each row
echo "<select>";
while($row = mysqli_fetch_assoc($query1)) {
echo '<option value="' .$row['category'] . '">' . $row['category'] . "</option>";
}
echo "</select>";
}
I have a database that stores data input from a website. I have designed a form to retrieve some of the data from the database. My issue is, I want some of the form fileds to auto populate with data from certain table columns. i don't know the PHP code to insert into the form fileds to do this.
The form looks like this:
http://onlinestudentsadmission.com/schooldemo/studentsdataform.php
The table has the following columns:
userid last_name first_name year friendly_url has_pic Gender Orphan Admmission School County
I want the form to pull the data in year, County, and School columns and populate the field options. What code do I insert into those form fields?
The form submits to studentdatadisplay.php which am yet to code since I dont know how it should look like so as to get the data from the data retrieval form and format it to a table depending on the search criteria.
Any help will be appreciated alot.
If your HTML form inputs have the name attributes set, and your form action is actually posting to the right URL, you don't have to do anything else to your form.
Have a read through this guide for PHP form handling. Basically, the name attributes for each of your inputs will be an element in the $_POST object available to your in your future studentdatadisplay.php script. You'll do any database queries in that script file.
Are you talking about drop-down list that is automatically populated with the different values from a column (such as year, County or School)?
If so, the PHP at the top of your page should look something like this:
<?php
$con=mysqli_connect("localhost","username","password","myDatabase");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$lookupYear = mysqli_query($con,"SELECT DISTINCT year AS value FROM tblStudents ORDER BY year ASC");
$lookupCounty = mysqli_query($con,"SELECT DISTINCT County AS value FROM tblStudents ORDER BY year ASC");
$lookupSchool = mysqli_query($con,"SELECT DISTINCT School AS value FROM tblStudents ORDER BY year ASC");
?>
Then when you're ready to list each one in the body HTML of your page, generate a dropdown option for each one using this example:
<select name="year">
<?php
while($row = mysqli_fetch_array($lookupYear))
{
echo "<option value=\"" . $row['value'] . "\">" . $row['value'] . "</option>";
echo "\n";
}
?>
</select>
I hope that helps.
I am very new to using PHP so I am asking for some help. I have this code which works fine (I got it online from somewhere).
$data = mysql_query("SELECT * FROM products")
or die(mysql_error());
print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
print "<tr>";
print "<th>title:</th> <td>".$info['title'] . "</td> ";
print "<th>info:</th> <td>".$info['information'] . " </td></tr>";
}
print "</table>";
But what PHP would I use to pull just one cell? For example, I have 9 items in my db with productIDs how could I call just the name or price or description for that particular product?
Thanks in advance.
It sounds like you want a particular row, in which, case you would just changes your query to filter for some specific value that identifies the row. THis might look something like this:
SELECT * FROM products WHERE product_id = ?
Where ? is the known product_id value that you want to receive the full record for.
Also, since you are just learning, I should point out that you should not learn to use mysql_* functions. They are deprecated. Learn mysqli or PDO for interacting with MySQL.
To obtain just one cell try the following:
SELECT CellName FROM TableName Where productId = x
So to get the price you would do:
SELECT price FROM products WHERE productid=x
Where productid equals the specific product you talked about.
if you want to understand a bit more mysql I advise you to go to PhpMyAdmin and try those and think about the result:
SELECT * FROM products;
SELECT title, information AS lol FROM products;
SELECT title, count(*) AS count FROM products;
SELECT * FROM products WHERE title = 'XXXX';
SELECT *, count(*), CHAR_LENGTH(title) FROM products;
SELECT title AS lol FROM products;
you will understand better the query language i thinks.
please read this chapter http://dev.mysql.com/doc/refman/5.0/en/examples.html. it'll really help you.
I just want to say, you can retrieve any columns(s) in any condition column
SELECT column_name(s) FROM products WHERE colmnm_name = ?
I am trying to use a dynamic select form to send information to a MySQL database. The user will be able to choose their school, and then select their major from within that school's list (all retrieved from a MySQL table). I then want to send that information to a different table in the database to be stored.
This is what I have for the code thus far:
<select name="school">
<php
$sql = "SELECT school_name, school_id FROM school_table ORDER BY school_name";
$query = mysql_query($sql,$conn);
while($row = mysql_fetch_array($states))
{
echo ("<option value=$row[school_id]>$row[school_name]</option>");
}
?>
</select>
I don't know how to make the second select, which would ideally recognize the school_id from the first table and match it with the corresponding school_id on the second table, which also lists the majors at that school. Also, I don't know how to send the form when it is finally done to a MySQL table.
You could either use a simple form to submit the value from the combobox to the server (as HTTP POST or HTTP GET) and use the value as a variable in you SQL statement or you could use a simple AJAX request to send the necessary information to your php script. Anyway, your serverside code should look like this:
//process.php
$myRetrievedValue = $_POST["school"];
$mySqlStm = "SELECT * FROM foo WHERE bar = '".mysql_escape_string($myRetrivedValue)."'";
On the client side you code could look like this (using a simple form and no AJAX stuff):
<form action="process.php" method="post">
<select name="school">
<php $sql = "SELECT school_name, school_id FROM school_table ORDER BY school_name";
$query = mysql_query($sql,$conn); while($row = mysql_fetch_array($states)) {
echo ("<option value=$row[school_id]>$row[school_name]</option>"); } ?>
</select>
<input name="" type="submit" />
</form>
Please remember: Whenever you use a user input in you query use prepared statements (or at least escape methods as above) to avoid SQL injections.
answer is to select from both tables in one SELECT using joins:
http://dev.mysql.com/doc/refman/5.0/en/join.html
INNER JOIN
SELECT `school_table`.`school_name`,
`school_table`.`school_id`,
`2ndTable`.`school_id`,
`2ndTable`.`major`,
FROM school_table,2ndTable
WHERE `school_table`.`school_id`=`2ndtable`.`school_id`
ORDER BY school_name
or a
LEFT JOIN (returning all columns in the left)
SELECT `school_table`.`school_name`,
`school_table`.`school_id`,
`2ndTable`.`major`,
`2ndTable`.`school_id`
FROM school_table
LEFT JOIN on `school_table`.`school_id`=`2ndtable`.`school_id`
ORDER BY school_name
I have some trouble with this.
I have one database with the following tables:
Countries -> All countries of the world are added
Cities -> The cities are also added
user_profile -> The profile of the user with the fields "country" & "city".
Everything works fine, even the populating of the boxes is working. But I don't know how to get the SELECTED value of the user for both country & city.
I have the following code:
Select Country: <br />
<select id="countries">
<option value="">--</option>
<?php
$sql = "SELECT country, title FROM countries ORDER BY title ASC";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
echo "<option value=\"".$row['country']."\">".$row['title']."\n ";
}
?>
</select>
Select City: <br />
<select id="cities">
<option value="">Select one</option>
<?php
$sql = "SELECT country, title FROM cities ".
"ORDER BY title ASC";
$rs = mysql_query($sql);
while($row = mysql_fetch_array($rs))
{
echo "<option class=\"".$row['country']."\" value=\"".$row['title']."\">".$row['title']."\n ";
}
?>
</select>
Now I need to check in the table user_profile which country & city he chose and show it to him.
I also use jchained (jquery plugin) to make both select boxes working together.
How can I achieve this? If I need to post anything else, please let me know.
Thanks in advance.
You have a lot of missing code here but I will try to help.
I am going to start by assuming that you have already stored the users selections in the user_profile table and that they are foreign keys to to correct relations.
To get the selected id's for repopulating the select boxes with the selected flag use:
SELECT country, city FROM user_profile where id = $user_id;
To get the text values you would do something like:
SELECT country.title as country, city.title FROM user_profile LEFT JOIN countries ON user_profile.country = country.id LEFT JOIN cities ON user_profile.city = cities.id WHERE user_profile.id = $user_id;
If you are not storing them as foreign key relations but instead by string title in the user_profile table you will need to do this:
SELECT country.title as country, city.title FROM user_profile LEFT JOIN countries ON user_profile.country = country.title LEFT JOIN cities ON user_profile.city = cities.title WHERE user_profile.id = $user_id;
You can then use these results to either display to the user or set the "SELECTED" flag in the options box.
Do you just mean you don't know what to do after they select the values? If so then you would just use the form to post the values to the new page. On the receiving page you can run a query to Update the user's profile.
Sorry if my response isn't detailed enough ><
edit Oh, I may see what you mean now. You're able to store it correctly but you want the correct option to be flagged as selected if it's what the user has previously set? If that's it then you can query for the value, then inside of the while loop just add a check to see if $user_country == $row['country'] and if so echo ' SELECTED '. Hope that helps!