Retrieve data from mysql through checkbox - php

I want to search question from mysql using ComboBox. If I select chapter number one in ComboBox, I want to display that question which only chapter one have.
In this suppose my chapter 1 contains 2 questions, chapter 2 contains some questions and so on. When I select chapter number 1 then it doesn't display question that chapter 1 have. It will only print the last question from the last chapter. How can I solve this problem?
<?php
$sql= "select distinct chapter from math";
$q= mysql_query($sql);
echo "<select name='fname'>";
while($info=mysql_fetch_array($q)){
$d1 = $info['chapter'];
echo "<option> ".$info['chapter']."</option>";
}
echo "</select>";
$sql1 = "select question from math where chapter=$d1";
$sql1_res = mysql_query($sql1) or die(mysql_error());
while($row = mysql_fetch_array($sql1_res)){
$question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
echo $question;
}
?>

Your mistake is that you are selecting questions from last iteration of query where you select chapters.
...
while($info=mysql_fetch_array($q)){
$d1 = $info['chapter'];
echo "<option> ".$info['chapter']."</option>";
}
echo "</select>";
$sql1 = "select question from math where chapter=$d1";
...
will always select last chapter. But you have another problem. Assuming you want to show questions when user select some value from dropdown, you have to send that selection using POST/GET/AJAX back to PHP and generate results based on that selection. Something like this:
if(!isset($_POST['fname']))
{
$sql= "select distinct chapter from math";
$q= mysql_query($sql);
echo "<select name='fname'>";
while($info=mysql_fetch_array($q)){
echo "<option> ".$info['chapter']."</option>";
}
echo "</select>";
}
else
{
$sql1 = "select question from math where chapter = " . $_POST['fname'];
$sql1_res = mysql_query($sql1) or die(mysql_error());
while($row = mysql_fetch_array($sql1_res)){
$question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
echo $question;
}
}

In your question query change the while loop according to below code:
<?php
$sql= "select distinct chapter from math";
$q= mysql_query($sql);
echo "<select name='fname'>";
$d1 = array();
while($info=mysql_fetch_array($q)){
$d1[] = $info['chapter'];
echo "<option> ".$info['chapter']."</option>";
}
echo "</select>";
$sql1 = "select question from math where chapter IN ('".implode("','",$d1)."')";
$sql1_res = mysql_query($sql1) or die(mysql_error());
while($row = mysql_fetch_array($sql1_res)){
$question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
echo $question;
}
?>
make the $d1 variable as an array and change the select query of question.

<?php
$sql= "select distinct chapter from math";
$q= mysql_query($sql);
echo "<select name='fname'>";
while($info=mysql_fetch_array($q))
{
$d1 = $info['chapter'];
echo "<option> ".$info['chapter']."</option>";
echo "</select>";
$sql1 = "select question from math where chapter=$d1";
$sql1_res = mysql_query($sql1) or die(mysql_error());
while($row = mysql_fetch_array($sql1_res))
{
$question=htmlspecialchars_decode($row['question'], ENT_QUOTES); // It gives only last question.
echo $question;
}
}
?>

$sql1 = "select question from math where chapter=".$d1;

Related

$_POST Insert into many to many, wrong PHP Syntax?

EDIT: IGNORE ANY SQL INJECTIONS OR VULNERABLE CODE STATEMENTS :D
(School Project).
I wish to create a insert form on my webpage where I can select an artist from a table, including a song from a table and combine them for an insert into a combined foreign key table.
I have managed to do selects and insert with only individual artist and song drop-downs on my web-page, but would wish for combining the two ID's from each table to combine them to a many to many relative table. But when I press the submit button nothing happens, and I'm a beginner and don't know if I'm missing any important bits of actually Posting the information.
For troubleshooting I have tried my code, and tested it. I see if I remove my code theres no problem, so the problem persists on the syntax I believe, as the first dropdown shows, alongside the second dropdown and submit button, but the problem is within the actual processing and SQL query part, where it never goes to the DB..
The problem:
As you can see below I have a the text Song Name appear with a drop-down menu in the bottom left corner including the Artist Name with a submit button. But my problem persists as the select and then insert from the two drop downs into the combined table does not work, it does not actually submit, I want it to post into the DB what can I do. But somethings off? I would appreciate any questions or help, this community is so amazing and wonderful to operate in!
Database
PHP
<form method='POST'>
<?php
include('connect_mysql.php');
if(isset($_POST["mangetilmange"])) {
$song_id = $_POST["song_id"];
$artist_id = $_POST["artist_id"];
$sql ="INSERT INTO artist_has_song (song_id, artist_id) VALUES
('$song_id', '$artist_id')";
if($conn->query($sql)) {
echo "Completed";
} else {
echo "Blablalbablablablablablablabl $sql
($conn->error.";
}
}
?>
Song Name
<?php
$sql = "SELECT * FROM song";
$resultat = $conn->query($sql);
echo "<select name='song_id'>";
while ($rad = $resultat->fetch_assoc()) {
$song_id = $rad["song_id"];
$songname = $rad["songname"];
echo "<option value='$song_id'>$songname</option>";
}
echo "</select>";
?>
Artist Name
<?php
$sql = "SELECT * FROM artist";
$resultat = $conn->query($sql);
echo "<select name='artist_id'>";
while ($rad = $resultat->fetch_assoc()) {
$artist_id = $rad["artist_id"];
$artistname = $rad["artistname"];
echo "<option value='$artist_id'>$artistname</option>";
}
echo "</select>";
?>
</form>
<input type="submit" name="mangetilmange" value ="Submit">
change you code to this:
<form method='POST'>
<?php
include('connect_mysql.php');
if(isset($_POST["mangetilmange"])) {
$song_id = $_POST["song_id"];
$artist_id = $_POST["artist_id"];
$sql ="INSERT INTO artist_has_song (song_id, artist_id) VALUES
('$song_id', '$artist_id')";
if($conn->query($sql)) {
echo "Completed";
} else {
echo "Blablalbablablablablablablabl";
}
}
?>
Song Name
<?php
$sql = "SELECT * FROM song";
$resultat = $conn->query($sql);
echo "<select name='song_id'>";
while ($rad = $resultat->fetch_assoc()) {
$song_id = $rad["song_id"];
$songname = $rad["songname"];
echo "<option value='$song_id'>$songname</option>";
}
echo "</select>";
?>
Artist Name
<?php
$sql = "SELECT * FROM artist";
$resultat = $conn->query($sql);
echo "<select name='artist_id'>";
while ($rad = $resultat->fetch_assoc()) {
$artist_id = $rad["artist_id"];
$artistname = $rad["artistname"];
echo "<option value='$artist_id'>$artistname</option>";
}
echo "</select>";
?>
<input type="submit" name="mangetilmange" value ="Submit">
</form>

How to make a hyperlink to all the rows in a particular column of a table using php

I hope I have asked correctly; I'm a beginning PHP programmer. Here is my part of code...
$con = mysqli_connect("localhost","root","","project");
mysqli_select_db($con,"project");
$result = mysqli_query($con,"select distinct drug from drugs");
while($row = mysqli_fetch_array($result))
{
echo "<a href='review10.php'>";
echo $row['drug'];
echo "</a>";
}
echo "hai";
Here when a list of drugs are displayed, when a user clicks on any drug name, then the details of all the customers who entered the drug name and their uses should be displayed. I am using PHP/MYSQL/WAMP.
Use This Code
$con = mysqli_connect("localhost","root","","project");
mysqli_select_db($con,"project");
$result = mysqli_query($con,"select distinct drug from drugs");
while($row = mysqli_fetch_array($result))
{
echo "<a href='review10.php'> '".$row['drug']."'</a>";
}

Make an SQL query a drop down menu

I have two SQL queries that display different results from my database. I'm using these results as navigation links in my nav bar.
At the moment all the results display as a single line of text, I want to make each SQL query display as a drop down menu, with all the results from the query as an option in the drop down.
Here's the code I'm using:
<?php
$q = "SELECT cat_id, cat_name FROM Category";
$result = mysqli_query($_SESSION['conn'], $q);
while ($row = mysqli_fetch_row($result)) {
echo "<a href='category.php?id=$row[0]'>$row[1]</a> ";
//display product categories
}
mysqli_free_result($result); //free result
$q = "SELECT brand_id, brand_name FROM Brand";
$result = mysqli_query($_SESSION['conn'], $q);
while ($row = mysqli_fetch_row($result)) {
echo "<a href='brand.php?id=$row[0]'>$row[1]</a> ";
//display product Brands
}
?>
use select option
echo "<select name='category'>";
while ($row = mysqli_fetch_row($result)){
echo "<option value='$row[0]'>$row[1]</option> ";
}
echo "</select>";
$q="SELECT cat_id, cat_name FROM Category";
$result = mysqli_query($_SESSION['conn'],$q);
$option1.="<select name='category'>";
while ($row = mysqli_fetch_row($result)){
$option1.="<option value='$row[0]'>$row[1]</option> ";
}
$option1.="</select>";
same for second
print this $option1 value in your view file
<select onchange="document.location.href='category.php?id='+this.value;">
<?php
$result = mysqli_query($_SESSION['conn'],$q);
while ($row = mysqli_fetch_row($result)):?>
<option value="<?=$row[0];?>"><?=$row[1];?></option>
<?php endwhile;?>
</select>

Echo table field values to a dropdown in PHP

I've been consulting stack overflow for quite some time now.
I'm developing a system for article management and I encountered a problem.
How do I echo a field values to a dropdown select from a database table?
I have managed to echo it using this:
$result = mysql_query("SELECT catName FROM tblCat");
while($row = mysql_fetch_array($result)){
echo "$row['catName']";
echo "`<br />`";
}
But when I edited the code to this:
$result = mysql_query("SELECT catName FROM tblCat");
while($row = mysql_fetch_array($result)){
echo "`<select>`";
echo "`<option value='{$row['catName']}'>`";
echo "`</select>`";
}
I got no result.
echo "<select>";
$result = mysql_query("SELECT catName FROM tblCat");
while($row = mysql_fetch_array($result))
echo "<option value='{$row['catName']}'>{$row['catName']}</option>";
echo "</select>";
You need to output it twice, you're only setting value, you need the label too:
echo "<option value='{$row['catName']}'>{$row['catName']}</option>";

Keeping lastly selected value from a dropdownlist after button click

Just like the title says i'm having difficulties in achieving it.
Here's my dropdownlist:
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
echo "<option value=$nt[data] name=\"blabla\">$nt[data]</option>";
}
echo "</select>";
?>
Here's the buttonclick:
<?php
if(isset($_POST['Submit']))
{
$query = "SELECT SUM(suma), paskirtis FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' AND data ='".$_POST['data']."' group by paskirtis";
$result = mysql_query ($query);
echo "<tr><td>Paskirtis:</td><td>Biudzetas:</td><td>Isleista:</td><td>Likutis:</td></tr>";
while($nt=mysql_fetch_array($result)){
if($nt['SUM(suma)'] != null){
$suma = $nt['SUM(suma)'];
}
echo "<tr><td>$nt[paskirtis]</td>
<td><input type=\"text\" name=\"isleista[]\" value=\"Skiriamų pinigų kiekis...\" method=\"post\"></td><td>".$suma." Lt</td><td>--</td></tr> <br>";
}
}
?>
After I press it, it retrieves the data I want from the date I've chosen from the drop down list and also reset whole drop down list showing the first value of the dates from sql database, not the one I selected. If anyone knows how to keep the selected value in the list any help is greatly appriciated!
Try this, you need to place select="selected" in the while loop. See below code how I placed the $selected
<?php
$query = "SELECT data, rel_id FROM $tbl_rel_balansas INNER JOIN $tbl_balansas ON $tbl_rel_balansas.rel_id = $tbl_balansas.id WHERE $tbl_rel_balansas.member_id = '$_SESSION[id]' group by data";
$result = mysql_query ($query);
echo "<select name=data value=''>Data</option>";
while($nt=mysql_fetch_array($result)){
$selected = ($_POST['blabla'] == $nt[data])?'selected="selected"':NULL;
echo "<option value=$nt[data] name=\"blabla\" $selected >$nt[data]</option>";
}
echo "</select>";
?>

Categories