Bootstrap modal shows the same result even after SQL query is changed - php

I have a bootstrap modal that will dynamically retrieve sql data based on sql query. What should happen is that an sql query will be executed and the matching records shall be displayed, and if a selected record doesn't match the sql query, then the modal shall be empty.
first record selection
Now, this is the .php code:
<?php
if(isset($_POST["student_id"]))
{
$output = '';
$connect = mysqli_connect("localhost", "root", "root", "tnstudentregistrationdb");
//This is where the problem is, but I just couldn't really figure out what's wrong. Basically, if I select a record from my modal and its studentID is 1 but doesn't have noteTitle record of 'SECOND NOTE', no data should be displayed, however in this case the record with noteTitle 'SECOND NOTE' still shows up.
$query = "SELECT * FROM studentNotes WHERE studentID = '".$_POST["student_id"]."' AND noteTitle = 'SECOND NOTE'";
$result = mysqli_query($connect, $query);
$output .= '
<div class="table-responsive">
<table class="table table-bordered">';
while($row = mysqli_fetch_array($result))
{
$output .= '
<tr>
<td width="30%"><label>Student ID</label></td>
<td width="70%">'.$row["studentID"].'</td>
</tr>
<tr>
<td width="30%"><label>Note Title</label></td>
<td width="70%">'.$row["noteTitle"].'</td>
</tr>
<!--Make note title unique in sql database-->
<tr>
<td width="30%"><label>Note</label></td>
<td width="70%">'.$row["notes"].'</td>
</tr>
';
}
$output .= "</table></div>";
echo $output;
}
?>
This is what happens if I select a record with Student ID 2:
The modal should be empty since in my database, the query 'SELECT * FROM studentNotes WHERE studentID = 2 AND noteTitle = 'SECOND NOTE';' returns an empty set
sql query in mysql shell
I have already tested the bootstrap modal and the html table element that I used in this system, and in conclusion, the problem really is from the above .php code I just really couldn't figure out what's wrong since I have also of course have already manually tried to run sql queries on sql shell directly and data retrieval there is completely just fine, please help and give beneficial feedbacks, thank you so much.

Related

Retrieving multiple data and inserting multiple data with array and foreach [duplicate]

This question already has an answer here:
Insert multiple rows into a MySQL database from a table
(1 answer)
Closed 5 years ago.
I am listing product list from products table which i select product_group on previous page. In this list, i show product names and empty input in every row of table. Into the empty inputs, manually i write number of products which i count in my shop. After filling all the products numbers (rows in this html table) i want to insert them into another mysql table row by row for each product.
Here is my html table codes:
<table class="table table-responsive" width="100%" border="1">
<tbody>
<tr style="background-color:#C2E17C">
<td align="center" width="80%"> Stok İsmi</td>
<td align="center" width="20%"> Sayım Miktarı</td>
</tr>
<?php
$personelcek = mysql_query("SELECT * FROM stoklar WHERE sto_durum='acik' AND sto_altgrup_kod = '".$_REQUEST['sta_RECno']."' ORDER BY sto_isim ASC");
while ($a= mysql_fetch_array($personelcek)){
$sto_RECno = $a['sto_RECno'];
$sto_isim = $a['sto_isim'];
echo'
<tr>
<td align="left" width="80%"><input class="input1" type="hidden" name="stok_kodu[]" value="'.$sto_RECno.'" readonly>'.$sto_isim.'</td>
<td width="20%"><input style="text-align:right" class="input1" type="text" name="sayim_miktari[]" value=""></td>
</tr>';}?>
</table>
And also there is one more input outside of the table which i retrieve the product_group_id as this:
<input type="hidden" name="alt_grup_kod[]" value="<?php echo $_REQUEST['sta_RECno']; ?>">
And the last thing, i want to retrieve existing count numbers from mysql products table for each product.
As a summary, i want to insert every product one by one into another mysql table including product_group_id, product_id, existing_count_numbers and manually written numbers.
I tried like this but everytime i get errors:
$stok_kodu = $_POST['stok_kodu'];
$stoklar = array($stok_kodu);
$sayim_miktari = $_POST['sayim_miktari'];
$sayim_miktarlari = array($sayim_miktari);
$alt_grup_kod = $_POST['alt_grup_kod'];
$alt_gruplar = array($alt_grup_kod);
$stok_miktarlari = array();
$miktar_cek = mysql_query("SELECT * FROM stoklar WHERE sto_RECno = '".$stok_kodu."' ORDER BY sto_RECno");
while ($miktar_al = mysql_fetch_assoc($miktar_cek)){
$stok_miktari[] = $miktar_al['sto_miktar'];
foreach ($stok_kodu as $stoklar){
foreach ($sayim_miktari as $sayim_miktarlari){
foreach ($alt_grup_kod as $alt_gruplar){
foreach ($stok_miktari as $stok_miktarlari){
$sayim_kaydet = mysql_query("INSERT INTO sayim_sonucu (alt_grup_kod, stok_kodu, sayim_miktari, stok_miktari) VALUES ('$alt_gruplar', '$stoklar', '$sayim_miktarlari', '$stok_miktarlari')");
}}}}}
Any idea how i can tidy all these codes and insert every product into another table of mysql database?
Your foreach makes the trouble.Change your foreach like this. hope this will help.And also an added note 'you definitely needed to write a function to escape mysql injection for every post variable.
$count=0;
foreach ($stok_kodu as $stoklar){
$sayim_kaydet = mysql_query("INSERT INTO sayim_sonucu
(alt_grup_kod, stok_kodu, sayim_miktari, stok_miktari) VALUES
('$alt_grup_kod[$count]', '$stoklar',
'$sayim_miktari[$count]', '$stok_miktari[$count]')");
$count++;
}

MySQL, PHP: Display MySQL table in HTML table

sorry, I'm still very novice with server-side (back-end) development, thank you for helping in advance.
Recently, I've been developing a web page which you can choose which table you want to display from the mySQL server, then it will create an editable table in HTML using the mySQL table data.
So far, I'm only able to fetch which table to get from mySQL and display it using in the form of an array, which doesn't really look that great.
I wonder if you can display the array in a form of an editable table without knowing the how many columns, column names as it is different for every different table.
Later on, I have to upload the cell that is updated back to mySQL database and I've no idea how to do that as well.
Sorry for the trouble, this website had been a great help for me and the community is great. Thanks a lot!
Image of what I have so far:
Code I have so far:
SelectTable:
<div class="table-responsive">
<b>List of Tables</b>
<table class="table table-condensed selection_table">
<tbody>
<tr>
<?php
if ($tableResult = mysqli_query($conn,"show tables")){
while($table = mysqli_fetch_array($tableResult)) {
echo("<tr> <td>". "<a class = 'list_tables' href = ?clickedTable=$table[0]>". $table[0] . "</a>" ."</td> </tr>");
}
}else{
die("<b>"."No Table in Database!"."</b>");
}
if (isset($_GET['clickedTable'])){
$selectedTable = $_GET["clickedTable"];
}
?>
</tr>
</tbody>
</table>
</div>
Fetch array from selected table:
<?php
if (isset($_GET['clickedTable'])){
echo("<b> Current Table is: </b> ".$selectedTable. "<br/>");
$query = "SELECT * FROM $selectedTable";
if ($result = mysqli_query($conn , $query)) {
while ($row = mysqli_fetch_array($result)){
print_r($row);
}
}
}else{
echo("Please select a table");
}
?>

PHP/MYSQL - Fetch DB values from dropdown menu, then into table in same page

I'm building an exam management website and one of the pages I'm working on is for adding students to a course. I have a dropdown menu for the student number (which fetches values from a table), however I'd like to make it so that when the teacher selects the student number from the dropdown menu, that student's name and major appear on a table below. I have pretty much all the code for it however I can't seem to make it work. The way it is right now it shows the head of the table but it doesn't show any lines.
The errors are always in the lines where I declare $sql1 and $sql2 and vary according to how I define the condition in the statement.
Code for my dropdown menu : (works fine)
<label class="control-label" for="number">Student Number</label>
<?php
$sql = "SELECT number FROM students";
$result = $conn->query($sql);
echo "<select class=".'"form-control"'.' id="number" name="number" for="number">';
while ($row = $result->fetch_assoc()) {
echo '<option value="' . $row['number'] . '">' . $row['number'] . "</option>";
}
echo "</select>";
?>
Code for my table : (shows only head of table, which is the best I got after moving around the code and getting conversion errors and such)
The errors are always in the lines where I declare $sql1 and $sql2 and vary according to how I define the condition in the statement.
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Major</th>
</tr>
</thead>
<tbody>
<?php
$sql1 = "SELECT name FROM students WHERE number='$row'";
$result1 = $conn->query($sql1);
$value = $result1->fetch_object();
$sql2 = "SELECT major FROM students where number='$row'";
$result2 = $conn->query($sql2);
$value1 = $result2->fetch_object();
echo "<tr>
<td>".$value."</td>
<td>".$value1."</td>
</tr>";
?>
</tbody>
</table>
Thank you for all your help!!
Before I can formulate a complete answer, I must advise you that there are a few logical errors in your code.
How does your page "know" that a user selected an option from the select? You should perhaps intercept the event and respond to that using an asynchronoys mechanism, e.g. via AJAX.
Anyhow, there's no need to run two queries when you can make it with just one:
SELECT name, major FROM students WHERE number = ...
Once you have described how you mean to address issue #1 we can continue discussing the complete solution.
Well, I think there will be no $row in the the second snippet.
It seems that you didn't pass your $row from 1st snippet to 2nd snippet.
You can read this:
PHP Pass variable to next page
You can use session, cookie, get and post.
Or can just simply use "include", then the variables you defined can be used in the second page.
<?php
include "page1.php";
?>
<table class="table">
<thead>
<tr>
<th>Name</th>
<th>Major</th>
</tr>
</thead>
<tbody>
<?php
$number = $row['number'];
$sql1 = "SELECT name, major FROM students WHERE number='$number'";
$result1 = $conn->query($sql1);
$value = $result1->fetch_object();
echo "<tr>
<td>".$value['name']."</td>
<td>".$value['major']."</td>
</tr>";
?>
</tbody>
</table>
According to godzillante's answer below, the mysql query should be like this:
Anyhow, there's no need to run two queries when you can make it with just one:
SELECT name, major FROM students WHERE number = ...
I notice that you use $row as the key of your second query.
But in the first snippet, the data you fetch is "$row" (it is an array, see PHP - fetch_assoc)
You should use $row['number'] instead.

a redirection to the profile

Ive got a code running smoothly which shows the list of people who are level 8 as shown below
I want when a person click on the username of the people it redirects them to their profile, actually i've got no clue or ideas of how could that be done. So i need a little bit of help to get some points on this.
Here is the code to show the above output
<?php
$lvl8 = 0;
$content = "";
$query = $koneksi->prepare("SELECT `user`, `level`, `LastOnlineDate` FROM `playerdata` WHERE `banned`=0 AND `level`=8");
$query->execute();
while($data = $query->fetch())
{
$lvl8++;
$content .= "<tr><td>".$lvl8."</td>";
$content .= "<td>".$data['user']."</td>";
$content .= "<td>".$data['LastOnlineDate']."</td></tr>";
}
?>
<table class="table table-bordered">
<thead>
<tr>
<td colspan='6'><h4><small>Level 8 - Trusted Admin (Total <?php echo $lvl8 ?>)</small></h4></td>
</tr>
<td><h5>Number</h5></td>
<td><h5>Username</h5></td>
<td><h5>Last Login</h5></td>
</thead>
<?php
if($query->rowCount() == 0)
{
echo "<tr><td colspan='6'><small>No rows found</small></td></tr>";
}
echo $content;
?>
</table>
first thing you should do is, have an a tag for name, like this
$content .= "<td><a href='link to a new file?id=userid from database'>".$data['user']."</a></td>";
In the new page, you can capture the id of the user, run a query to fetch the details of obtained id and then show the details obtained from database.
Create a http query or post form for your action.
like this in your foreach loop.
GET
<?php echo $username ?>
in your ending script for example a controller in an mvc architecture:
$id = $_GET['user_id'];
// do database stuff and view
Do your query using $id and fetch the result then display your view.
Basically you iterate over user id's to create links that carry the id of the clicked user in order to fetch the selected id.

Hide php table unless it is called through search

I currently have a page where it displays data from my database in tables and it also searchable using some key terms. What I want to do is for the table to be hidden unless it is being searched. I have rephrased my codes because this is an assignment and i dont want it to be picked up as plagiarism. I really hope someone knows a way to do this
<?php
include_once('connection.php');
$SQL = "SELECT * FROM `players`";
if (isset($_POST['search'])) {
$search_word = mysql_real_escape_string($_POST['search_bar']);
$SQL .= " WHERE player_id LIKE '%{$search_word}%'";
$SQL .= " or player_name LIKE '%{$search_word}%'";
}
$SQL .= 'group by player_id, player_name order by player_id ASC';
if( !( $SQLRes = mysql_query( $SQL ) ) ){
echo 'Retrieval of data from Database Failed - #'.mysql_errno().': '.mysql_error();
}else{
if( mysql_num_rows( $SQLRes )==0 ){
echo '<tr><td colspan="1">No results </td></tr>';
}else{
$current_player_id = false;
while( $row = mysql_fetch_assoc( $SQLRes ) ){
if ($row['player_id'] !=$current_player_id) {
if ($current_player_id !== false)
echo '</table>';
echo '
<h4>'.$row['player_id'].'</h4>
<table style = "width: 100%" class="listing" cellpadding="0" cellspacing="0">
<tr>
<th style="width: 50%">Player Name</th>
</tr>';
$current_player_id = $row['player_id'];
}
echo '<tr>
<td>'.$row['player_name'].'</td>
<td><a href =delete.php?del='.$row['player_id'].'><strong>DELETE<strong/></a></td>
</tr> ';
}
}
echo '</table>';
?>
</table>
At the start you open with an if statement:
if (isset($_POST['search'])) {
$search_word = mysql_real_escape_string($_POST['search_bar']);
$SQL .= " WHERE player_id LIKE '%{$search_word}%'";
$SQL .= " or player_name LIKE '%{$search_word}%'";
}
It looks to me like the check for the $_POST variable is telling you if you have a search.
That if just needs to wrap the whole of this body of code and it should do what you're looking for.
That said, there are a few niggles elsewhere in your code - please take these the right way - in an effort to help and guide...
1 - Rather than build your "like" statements in your SQL, take a look at bind variables. Also, take a look at mysqli usage examples.
2 - Rather than echoing through your code, consider building an $html string up and then echoing at the end.
3 - It looks like you're coding for your SQL select to return multiple instances of the same player. That suggests either a flaw in the datamodel (the table is called player after all), or in the SELECT - why not get back distinct rows.
4 - Echoing SQL errors back to the console is a great way to give external people access to protected information about your system. Consider logging to a file instead - at least in production.
5 - I've corrected your indentation. Learn how to use it, it will save your brain many times over if you indent code properly.

Categories