php mysql query by link [duplicate] - php

This question already has answers here:
What is the difference between single-quoted and double-quoted strings in PHP?
(7 answers)
Closed 5 years ago.
I'm trying to bring the user details by clicking on "detail" in a table (all the rows in the table are from the db) I bring the info to the table with this (code found it somewhere here),
<?php foreach ($link->query('SELECT * from data') as $row){ ?>
<td><?php echo $row['fullname'] ?></td>
this works perfect, and on the table I add this.
<?php echo 'Details'; ?>
now I'm trying to get the details for each user by using the same code in the query I used before but it doesn't work.
<?php $id = $_GET['AutoID'];
foreach ($link->query('SELECT * from data where AutoID=$id') as $row){ ?>
<td><?php echo $row['fullname'] ?></td>
<td><?php echo $row['email'] ?></td>
<td><?php echo $row['phone'] ?></td>
Thanks a lot, I hope I explain myself well.

Change this
$link->query('SELECT * from data where AutoID=$id')
to that
$link->query('SELECT * from data where AutoID='.$id)
BTW. consider use some filter for $_GET params

Related

how to pass two fields as query string to destination page in href statement

I have this code in php
while($row = mysqli_fetch_array($result)) {
?>
<tr>
<td><?php echo $row["Sec_No"]; ?></td>
<td><?php echo $row["Sec_Sub"]; ?></td>
<td><?php echo $row["Sec_Lec"]; ?></td>
<td><a href="Theo_Entery_Sheet.php?Sec_No=<?php echo $row["Sec_No"]; ?>"> ُEnter Marks/>a></td>
</tr>
<?php
I want to pass the field [Sec_Sub] beside the field [Sec_No] that is exist in href statment as query string to another page (Theo_Entery_Sheet.php)
Theo_Entery_Sheet.php
i want the part of SQL "WHERE" take two conditions Sec_No AND Sec_Sub that is comming from above page
...
...
...
$sql = "SELECT theo_stu_sections.Sec_No, theo_stu_sections.St_No, students.Name, theo_stu_sections.Mid,
. "FROM students INNER JOIN theo_stu_sections ON students.St_ID = theo_stu_sections.St_No\n"
. "WHERE Sec_No='".$_GET['Sec_No']."'\n"
. "ORDER BY students.Name;";
thank You
I want the page to receive two parameters from the page which send query string

MYSQL statement will not recognise _POST variable in PHP

Ive created a page that lists existing hardware transactions ('else' clause below) and filters the transactions based on criteria associated with a specific row ('If' clause). The Else clause displays fine, but when I enter Criteria (e.g. Mouse)and select a Filter (e.g. device) that I know match in the database, no results are shown, when there should be.
The frustrating part is that when I change line 12 to:
WHERE device = '$criteria' //This will work
For some reason the original code wont work even though the variable $filter equals the string "device".
Please someone help, this seems like a bug to me :(
PHP Code:
function display_transactions() {
//Display Transactions table
GLOBAL $transactions;
GLOBAL $db;
try {
if (isset($_POST['submit_criteria'])) {
GLOBAL $transactions_sql;
$filter = $_POST['filter-by'];
$criteria = $_POST['text_criteria'];
$transactions_sql = "SELECT device, quantity, transaction_type, ticket_id, region, username, datetime
FROM transactions
WHERE '$filter' = '$criteria'
";
} else {
$transactions_sql = 'SELECT device, quantity, transaction_type, ticket_id, region, username, datetime
FROM transactions
ORDER BY datetime DESC';
echo "UNFILTERED!";
}
$transactions = $db->query($transactions_sql);
} catch (Exception $e) {
echo $e->getMessage();
}
}
HTML page:
<?php display_transactions() ?>
<?php while($row = $transactions->fetch_assoc()) { ?>
<tr>
<td><?php echo $row['device']; ?></td>
<td><?php echo $row['quantity']; ?></td>
<td><?php echo $row['transaction_type']; ?></td>
<td><?php echo $row['ticket_id']; ?></td>
<td><?php echo $row['region']; ?></td>
<td><?php echo $row['username']; ?></td>
<td><?php echo $row['datetime']; ?></td>
</tr>
<?php } ?>

mysqli_fetch_row doesn't return results, but mysqli_fetch_assoc does

I'm writing a simple php page for an assignment, and one of the criteria is to use both mysqli_fetch_assoc and mysqli_fetch_row. I'm using the same query for both:
<?php
// Perform database query
$query = "SELECT * FROM player JOIN stat ON player.playerId = stat.playerId";
$result = mysqli_query($dbconnection, $query);
if (!$result) {
die("Database query failed");
}
?>
When I run this query in my database, it returns 3 rows as expected. In my webpage, I use mysqli_fetch_assoc($result) first, and it renders an unordered list with the information as expected. I then proceed to use mysqli_fetch_row($result) to display more information, but the second while loop doesn't yield any table data (just the content of the th tags).
<h1>The Players</h1>
<ul>
<?php
// Use return data with mysqli_fetch_assoc
while($row = mysqli_fetch_assoc($result)) {
// output data from each row
?>
<li><?php echo $row["playerId"]; ?></li>
<li><?php echo $row["fname"] . " " . $row["lname"]; ?></li>
<li><?php echo $row["team"]; ?></li>
<li><?php echo $row["position"]; ?></li>
<?php echo "<hr />";
}
?>
</ul>
<h1>The Stats</h1>
<table>
<th>Player</th>
<th>Batting Avg</th>
<th>Homeruns</th>
<th>RBIs</th>
// DATA BELOW THIS POINT IS NOT RENDERED BY THE WEBPAGE
<?php
// Use return data with mysqli_fetch_row
while($row = mysqli_fetch_row($result)) {
?>
<tr>
<td><?php echo $row[2]; ?></td>
<td><?php echo $row[8]; ?></td>
<td><?php echo $row[9]; ?></td>
<td><?php echo $row[10]; ?></td>
</tr>
<?php
}
?>
</table>
<?php
// Release returned data
mysqli_free_result($result);
?>
I'm going to be writing a lot of php for this class in the coming weeks, so I'd really like some tips on how to troubleshoot these kinds of errors myself. Are there methods I can use to easily check the content of $result to see if any resources were actually passed through? It appears to me that $row in the second loop isn't getting assigned to anything, so it just doesn't execute the echo commands.
Ghost is correct. When you iterate over the records at the end it doesnt re-set by itselt and it's not a circular list. Because of that you have to manually set it to 0 with $obj->data_seek(int, ie 0); or in procedural style mysqli_data_seek($result,0);

HTML Clear a Table and Insert Data

Note: I do NOT want to touch any of the data inside of the database. I'm talking about deleting the HTML table.
Also, I have tried using ".html()" and ".remove()" on my table but they end up breaking the formatting and or not allowing any information to show up.
I am going to try to explain my problem to the best of my ability with as much code as I can, so if anything is missing please let me know and I will add it.
Right now I have an HTML page where I have a table.
This HTML table contains several rows of MYSQL data which is fetched from a database.
On page load the initial data is fetched and shown inside the table.
I have several different sorting options on the top of my page that I am trying to make sort the data in the table based on things like date created, is the person paid, and are the verified. (These are just a few examples.
I want to be able to clear the table on click of one of these sorting options and have PHP re-query the database and pull the information with the sorted condition attached.
The query works, and the data is added to the table. HOWEVER, instead of replacing the old data and showing the sorted data, the new (sorted) data, is APPENDED to the top of the table, thus leaving the old data.
How do I fix this? I only want the new sorted data to show.
This if statement checks to see if a drop-down menu called hasBeenSubmitted has had one if it's options clicked, and if so, it does the below based on the option selected.
if(checkValue($_POST['hasBeenSubmitted'])) {
switch($_POST['hasBeenSubmitted']) {
case 'yes':
$query1 = "SELECT *
FROM APPLICATION_Primary
INNER JOIN Application ON Application_primary.applicationId = Application.applicationId AND hasBeenSubmitted = 1
INNER JOIN Applicant ON Applicant.applicantId = Application.applicantId
INNER JOIN APPLICATION_Degree ON Application.applicationId = APPLICATION_Degree.applicationId
INNER JOIN (SELECT DISTINCT (
department_code
), academic_programCode, department_nameFull
FROM AcademicProgram) AS
ac_program ON APPLICATION_Degree.academic_program = ac_program.department_code
INNER JOIN APPLICATION_Transaction ON Application.transactionId = APPLICATION_Transaction.transactionId
ORDER BY Applicant.applicantId ASC";
//SOMEHOW CLEAR THE TABLE HERE?
//QUERY
$updatedapplicants = $db->query($query1);
populateTable($updatedapplicants);
break;
case 'no':
$query1 = "SELECT *
FROM APPLICATION_Primary
INNER JOIN Application ON Application_primary.applicationId = Application.applicationId AND hasBeenSubmitted = 0
INNER JOIN Applicant ON Applicant.applicantId = Application.applicantId
INNER JOIN APPLICATION_Degree ON Application.applicationId = APPLICATION_Degree.applicationId
INNER JOIN (SELECT DISTINCT (
department_code
), academic_programCode, department_nameFull
FROM AcademicProgram) AS
ac_program ON APPLICATION_Degree.academic_program = ac_program.department_code
INNER JOIN APPLICATION_Transaction ON Application.transactionId = APPLICATION_Transaction.transactionId
ORDER BY Applicant.applicantId ASC";
//SOMEHOW CLEAR THE TABLE HERE?
//QUERY
$updatedapplicants = $db->query($query1);
populateTable($updatedapplicants);
break;
}
}
Here is populateTable() which does exactly what you'd think. It takes the information received from the applicants variables and pulls the columns it needs and echo's them inside the table.
function populateTable($updatedapplicants) {
foreach($updatedapplicants as $applicant) {
$color = $color == 'light'? 'dark' : 'light';
?>
<tr
class='<?php echo $color;?>' id='applicant_data'>
<td style="text-align: center;"><?PHP echo '<input type="checkbox" class="processed_check" id="applicant-' . $applicant['applicant_id'] . '" ';
if($applicant['status'] == 1){
echo "checked";
}
echo '>';
?></td>
<td><?php echo $applicant['applicantId']; ?></td>
<td><?php echo $applicant['isPayingOnline']; ?></td>
<td><?php echo ($applicant['hasBeenSubmitted'] == 1) ? 'yes' : 'no'; ?></td>
<!-- applicant info -->
<td><?php
if($applicant['hasBeenSubmitted '] == 1) {
$exDOB = explode("/", $applicant['date_of_birth']);
$newDOB = $exDOB[0].$exDOB[1].$exDOB[2];
$pdftitle = $applicant['applicationId']."_".$applicant['familyName']."_".$applicant['givenName']."_".$newDOB.".p df";
echo "<a href='getFile.php?FileName=" . $pdftitle . "&FileType=application' target='_blank'>" . $applicant['applicant_name'] . "</a>";
} else {
echo $applicant['givenName']." ".$applicant['familyName'];
}
?></td>
<!-- academic info -->
<td><?php echo $applicant['academic_programCode'] . ' - ' . $applicant['department_nameFull']; ?></td>
<td><?php echo $applicant['submittedDate'] ?></td>
<td><?php echo $applicant['startSemester'] ?></td>
<td><?php echo $applicant['startYear'] ?></td>
<td><?php echo ($applicant['isCompleted'] == 'Y') ? "yes" : "no"; ?></td>
<td><?php echo $applicant['studentType'] ?></td>
<td><?php echo $applicant['academic_load'] ?></td>
<td><?php
$email = $applicant['loginEmail'];
echo "<a href='mailto:$email'>$email</a>";
?></td>
<td><?php
if ($applicant['essayOldFileName']) {
$exDOB = explode("/", $applicant['date_of_birth']);
$newDOB = $exDOB[0].$exDOB[1].$exDOB[2];
$ext = end(explode(".", $applicant['essayOldFileName']));
$filename = "essay_".$applicant['applicantId']."_".$applicant['givenName']."_".$applicant['familyName']."_".$new DOB.".".$ext;
echo "<a href='getFile.php?FileName=$filename&FileType=essay' target='_blank'>Essay</a>";
}
?></td>
<td><?php
if($applicant['resumeOldFileName']) {
$exDOB = explode("/", $applicant['date_of_birth']);
$newDOB = $exDOB[0].$exDOB[1].$exDOB[2];
$ext = end(explode(".", $applicant['resume_file_name']));
$filename = "resume_".$applicant['applicant_id']."_".$applicant['given_name']."_".$applicant['family_name']."_". $newDOB.".".$ext;
echo "<a href='getFile.php?FileName=$filename&FileType=resume' target='_blank'>Resume</a>";
}
?></td>
</tr>
<?php }
}
Thank you, I appreciate all help. I am really stuck on this issue.

newbie PDO query display issue

newbie PDO question...
I think (hard to tell - buried in a wrapper class) I am doing a select query using:
PDO::FETCH_ASSOC
wrapper:
return $pdostmt->fetchAll(PDO::FETCH_ASSOC);
I am getting back just 1 record - like to display the results of just current customer....
My query is:
$results = $db->select("mytable", "id = 201"); //just 1 exact record)
then I can loop like:
foreach ($results as $result) {
....
?>
<tr>
<td><?php echo $result["First"]; ?></td>
<td><?php echo $result["Last"]; ?></td>
<td><?php echo $result["id"]; ?></td>
</tr>
This all works fine, but since I only have 1 exact CUSTOMER record - I don't need to LOOP anything.
My question is:: How do I display the columns without a loop?
The following failed:
echo $results["First"];
echo $results["First"][0];
echo $results["First"][1];
So what can I use to make it work?
Use fetch instead of fetchAll
$row = $pdostmt->fetch(PDO::FETCH_ASSOC);
You have to use the fetch function of PDO library.

Categories