Creating drop down menu using php, sql connected to database table - php

I'm creating currently database with tables and trying to do it using second normalization form for my school.
I have list of institutional partners which has to be displayed in two tables.
I got this far that I have created database tables:
The first one called "as_partners_id" which contain columns: "Partner_ID" (this is primary key INT) and "Partner" (just a VARCHAR).
The other table called "as_partners" has some more columns, like: "Partner_ID, Country, EU terms, NON-EU terms, More info".
What I want to do is create drop down menu above first table from which you can choose ID from e.g. 1 to 30. After picking it will display the second table with more information about this particular ID. But I actually don't know how to do it properly. Here is the code that I have so far:
<?php
// Create connection
$con=mysqli_connect("localhost","easj_admin","","easj");
// Check connection
if (mysqli_connect_errno())
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else {
echo "We are connected.";
}
$result = mysqli_query($con,"SELECT Partner_ID, Partner FROM as_partners_id");
?>
BODY PART:
<table>
<tr>
<?php
// Get field information for all fields
while ($fieldinfo=mysqli_fetch_field($result))
{
echo "<th>" . $fieldinfo->name ."</th>";
}
echo "</tr>";
// <form>
// <input typ="select">
// </form>
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Partner_ID'] . "</td><td>" . $row['Partner'] . "</td>" ;
echo "</tr>";
}
?>
</table>
How can I add to my code this drop down menu that will display the second table called as_partners?
Please help.

<form name="myform" method="get">
<select name='whatever' onclick="if(this.value != ''){ myform.submit(); }">
<?
while($row = mysqli_fetch_array($result))
{
echo "<option value='" . $row['Partner_ID'] . "'>" . $row['Partner'] . "</option>";
}
?>
</select>
</form>
That will get you the drop down. Then from there you can submit the form using post or get and use the submitted id to generate the second table.
Then you'll need to add php code similar to this:
if(isset($_GET['whatever']))
{
[build 2nd table here]
}

Related

trouble getting info from an array and then inserting it into a db

I have a page that I have been working on. It runs several queries to get existing data from several tables in my DB. There is a table that shows the result of three queries. The first query gets the extension and the secret of phones, the 2nd query gets MAC addresses of phones, and finally the third query gets the names of templates for the phones. The results of the last two queries (with the help of others) are setup as dropdowns in the 3rd and 4th columns of the table created to show the extensions. This way I can select the MAC of the phone I want to assign to the extension and then the template to make the phone work the way I want. The whole page is set as a form and I am using $post to the insert page. My goal here is to take the information (array) that is created by the user making their selections and insert ALL the 4 columns of information into a new table, from there I want to create files using that information to setup the phones. Here is the code I have for now.
<?php
error_reporting(E_ALL);
ini_set('display_errors','On');
$link = mysql_connect("localhost", "root", "cacti") or die ('Error connecting to mysql' . mysql_error());
mysql_select_db("cqadmin");
$sql2 = "SELECT extension, secret from extensions;";
$result2 = mysql_query($sql2) or die(mysql_error());
echo "<table border='3'>
<tr>
<th>Extension #</th>
<th>Secret</th>
<th>MAC Address</th>
<th>Template</th>
</tr>";
while($row = mysql_fetch_array($result2))
{
$sql = "SELECT id , mac FROM phones order by mac;";
$result = mysql_query($sql) or die(mysql_error());
$sql1 = "SELECT id , templatename FROM templates order by templatename;";
$result1 = mysql_query($sql1) or die(mysql_error());
echo "<tr>";
echo "<td>" . $row['extension'] . "</td>";
echo "<td>" . $row['secret'] . "</td>";
echo "<td> <select name='phone'>";
while($rowA = mysql_fetch_array($result)) {
echo '<option value="' . $rowA['id'] . '">' . $rowA['mac'] . '</option>';
}
echo "</select></td>";
echo "<td><select name='template'>";
while($rowB = mysql_fetch_array($result1)) {
echo '<option value="' . $rowB['id'] . '">' . $rowB['templatename'] . '</option>';
}
echo "</select></td>";
echo "</tr>";
}
echo "</table>";
?>
<input type="submit" value="Submit your selections">
</body>
</html>
And my insert page
<?php
echo "You got here";
//***********Get the Assignment information *************
$values = array_values($_POST);
print_r($values);
?>
The resulting print shows this
Array ( [0] => 324 [1] => 24 )
Looking at my db table 324 is the index id of the last phone scanned and in the template table 24 is the last template created, No info on the extension or the secret.
I think I am close but I do not know where to go from here.
PS. I know I need to use mysqli or pdo, not sure how to change over yet.

With MySQL and PHP, get rows from table, input a letter grade for rows, then update table

So far, I have the following code...
<?php
// Connection information to database omitted....
<form method='get' action='submit.php'>
$counter=0;
echo "<table>"
while ($row= mysqli_fetch_row($result))
{
echo "<tr><td>" . $row[0] . "</td><td>" . $row[1] . </td>
<input name='student" . $counter . "'></td></tr>";
$counter++;
}
echo "</table>
<button name="submitGrades" type="submit">Submit</button>
</form>"
?>
row[0] is a student ID number and row[1] is their grade.
I would like to enter letter grades for each row, then hit submit to update the table. But I can't seem to figure out how to retrieve each input field.
I was able to get it working by forcing the user to enter a specific student ID, then the grade. It's not optimal, so I would appreciate any assistance. Thank you in advance.
I done this way,
fill the student ID column in the table as a input,name from id0,id1.... like student0,1..
Now, after submit
for($i=0;$i<;$counter;$i++)
{
$g="student".$i;
$sid="id".$i;
$grade=$row[$g];
$id=$row[$sid];
$query="update table set grade=".$grade."where sid=".$id;
mysqli_query($query);
}

Passing selected variables in PHP?

I want to send some information from one php file to another.
I've read about the use of $_SESSION and $_POST, but they're giving me some problems.
My code looks something like this:
<form action="booking.php" method="post">
<select name="bookingflight">
<?php
$query = "SELECT Flight, Name
FROM Airport";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($result)) {
echo "<option value=\"". $row['Flight'] . ',' . $row['name'] . "\">" . $row['Flight'] . ' ' . $row['name'] . "</option>";
}
?>
</select></p>
<input type="submit" value="book flight"/>
</form>
This gives me a dropbox list of all the flights, along with the name of the flights.
If I select an element it's stored in $_POST["bookingflight"] which I can access in booking.php, which is fine.
However, it's given as a string, while I should be able to handle flight and name separately.
Ideally, I'd have two variables, one for flight and one for name, which I can access in booking.php.
How should I do this? With $_SESSION I don't even know how to assign a selected item from the list to a variable.
Alternate Solution: If Flight is unique data
while($row = mysql_fetch_array($result)) {
$flight=$row['Flight'];
$name=$row['Name'];
echo "<option value='$flight'>$flight $name</option>";
}
PHP : booking.php
<?php
$flight=$_POST['bookingflight'];
$con=mysqli_connect("example.com","peter","abc123","my_db");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT Flight,Name FROM Airport WHERE Flight='$flight'");
$row = mysqli_fetch_array($result);
$name= $row['Name'] ; // Here you get Name of selected Flight
mysqli_close($con);
?>
Or
If your Airport table contains any unique data/id , you can pass that data as option value too
You started this the wrong way...
Give your Airport table a unique primary key (named id, INT, auto-increment) and pass that as a value in generated options:
echo "<option value=\"". $row['id'] ."\">" . $row['Flight'] . ' ' . $row['name'] . "</option>";
Now when you POST your form you get that ID value in booking.php.
Because every flight has a unique ID you can just issue another query and you get your result as an array there:
$query = "SELECT Flight, Name FROM Airport WHERE id = $id";
Put this in your booking.php...
if($_POST)
{
$values = $_POST['bookingflight'];
$val= explode(',',$values);
$_SESSION['flightnumber'] = $val[0];
$_SESSION['flightname'] = $val[1];
}

php - using a hyper link to send variable used in data base to use get command to echoe results

I have a table with arrays pulling information from a database, I have linked the fix to be a hyperlink "click me for fix" I have entered the link to send the variable to a php that will use $GET to echoe the information.
code below , i am new to php and been racking brains . the only out put i get is Welcome . (done welcome to test if information was being passed)
<div id=list>
<?php
// Create connection
$con=mysqli_connect('172.16.254.111',"user","password","Faults"); //(connection location , username to sql, password to sql, name of db)
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
//where statement in the sql syntax will select where in db to get infor, use AND to add another condition
$result = mysqli_query($con,"SELECT * FROM Fixes WHERE Product='Serv1U' AND Fault_type='Broadcast Manager'"); //this creates a variable that selects the database
//below is the echo statment to create the results in a table format, list collumn titles
echo "<table id=tables border='1'>
<tr>
<th>Products</th>
<th>Fault_type</th>
<th>Fault_Description</th>
<th>Fix</th>
</tr>";
//below is script to list reults in a table format, $row [row name on table]
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['Product'] . "</td>";
echo "<td>" . $row['Fault_type'] . "</td>";
echo "<td>" . $row['Fault_Description'] . "</td>";
echo "<td>Click for Fix</td>"; //this is how you link into an echo, alsothe id=" hopefully means i can send ID information.
}
echo "</tr>";
echo "</table>";
// below closes the coonection to mysql
mysqli_close($con);
index.php:
Welcome <?php echo $_GET["Fix"]; ?>.
I'm lost. Any help is appreciated.
Thanks
?>
Is it just a typo here? $GET must be $_GET.
And it should be $row['Fix'] not $rows['Fix']! Note the 's'!

dynamically populate mysql array into drop down menu

I have a database that I want to get data out onto a website. It contains states listed by name and id. Counties listed by id, namne , and state that contains thems ID and then clubs that exist , with a reference to the county id's that they exist in and columns for their actual data.
What I've got :
A drop down menu that populates itself with state id and name.
What I'd like to accomplish:
On selection of state , let's say ny , take it's id and use this in gathering another mysql array for the county drop down. I'd like it to dynamically occur on selection of state , maybe even giving a count of results next to the drop down.
$resstate = mysql_query("SELECT * FROM state ORDER by longstate;") or die("Note: " . mysql_error());
State:
<select name="State" size=1>
<?
while( $rs = mysql_fetch_array( $resstate ) ) {
echo "<option value=" .$rs['id'] . ">" . $rs['longstate'] . "</option>";
}
echo "</select>";
?>
I know I could use a JavaScript onChange="this.form.submit()" on the first drop down, but it's my understanding that I'd then be making a new page at that point and don't know if I could keep the functionality of the state drop down, say if you accidentally chose new Hampshire when you wanted New York.
here's an example of the current array filling the drop down :
http://snowmobileamerica.com/countytest.php
----EDIT---
Using Dagons Advice , I looked into Ajax.
I made a php file that's supposed to query the database based on a reference to getcounty.php?q=
The file is created as follows :
<?php
$q=$_GET["q"];
$cn=mysql_connect("localhost","user","password") or die("Note: " . mysql_error());
mysql_select_db("snowusa_clubs", $cn);
$sql="SELECT * FROM county WHERE state_id = '".$q."' ORDER by name";
$result = mysql_query($sql);
echo "<select name="County" size=1>";
while($rc = mysql_fetch_array($result))
{
echo "<option value=" .$rc['id'] . ">" . $rc['name'] . "</option>";
}
echo "</select>";
mysql_close($cn);
?>
If i try to run it manually http://www.snowmobileamerica.com/getcounty.php?q=33 I get a 500 internal server error...
Any ideas where I went wrong?
try adding an id to the element, then make an ajax call to a handler with jquery:
$("#State").change(function() {
$.post("path/to/request handler/" , { "State" : $(this).val() },
function(data){
if (data == "OK"){
//add some elements here
} else {
//handle an error here
}
});
});
not able to comment yet.
but for the second question try:
<?php
$q=$_GET["q"];
$cn=mysql_connect("localhost","user","password") or die("Note: " . mysql_error());
echo "Conn ok<br>";
mysql_select_db("snowusa_clubs", $cn);
echo " Database opened<br>";
$sql="SELECT * FROM county WHERE state_id = '$q' ORDER by name";
$result = mysql_query($sql);
echo " Database queried <br>";
echo "<select name='County' size=1>";
while($rc = mysql_fetch_array($result))
{
echo "<option value='" .$rc['id'] . "'>" . $rc['name'] . "</option>";//added single quotes in the value
}
echo "</select> ";
mysql_close($cn);
?>

Categories