this is my search.php code. i am trying to search wherein the user will input the subscriber id and it will show the FirstName, LastName and address of the accepted SubscriberID
<!doctype html>
<html>
<body>
<?php
$conn=mysqli_connect("localhost","root","kuyahajji","steve");
if (isset($POST['submit-SubscriberID'])) {
$choice = mysqli_real_escape_string($conn,$_POST['$SubscriberID']);
$sql = "SELECT * FROM property WHERE SubscriberID='$choice'";
$result = mysqli_query($conn,$sql);
$queryResult = mysqli_num-rows($result);
if ($queryResult > 0) {
while($row = mysqli_fetch_assoc($result))
{
echo $result;
}
}
else {
echo "0 results";
}
}
?>
</body>
</html>
$row is array. To output required key use [] notation:
echo $row['FirstName'] . $row['LastName'];
Also correct name for a function is mysqli_num_rows (all underscores).
Related
I do not understand PHP and SQL. We are just barely scraping it at the end of the semester, and its frustrating me. I am trying to get my results page to show the correct info, but for the life of me, it won't grab anything. I clearly have something wrong and was wondering if I could get some help.
Initial page
(normal top of basic webpage here)
<form id="ClubForm" action="ClubMembersResults.php" method="get">
<?php
require_once ('dbtest.php');
$query= "SELECT * FROM tblMembers ORDER BY LastName, FirstName, MiddleName;";
$r = mysqli_query($dbc, $query);
if (mysqli_num_rows($r) > 0) {
echo '<select id="memberid" name="memberid">';
while ($row = mysqli_fetch_array($r)) {
echo '<option value="'.$row['LastName'].'">'
.$row['LastName'].", ".$row['FirstName']." ".$row['MiddleName']. '</option>';
}
echo '</select>';
} else {
echo "<p>No Members found!</p>";
}
?>
<input type="submit" name="go" id="go" value="Go" />
</form>
<div id="results"></div>
</body>
results page currently written as:
<?php
$memid = 0;
$memid = (int)$_GET['memberid'];
if ($memid > 0) {
require_once ('dbtest.php');
$query = "SELECT * FROM tblMembers WHERE MemID = $memid;";
$r = mysqli_query($dbc, $query);`enter code here`
if (mysqli_num_rows($r) > 0) {
$row = mysqli_fetch_array($r);
echo "<p>Member ID: ".$row['MemID']."<br>";
echo "Member Name: ".$row['LastName'].", ".$row['FirstName']." ".$row['MiddleName']."<br>";
echo "Member Joined: ".$row['MemDt']."<br>";
echo "Member Status: ".$row['Status']."<br></p>";
}else {
echo "<p>Member not on file.</p>";
}
//table for inverntory
echo "<table border='1'>";
echo "<caption>Transaction History</caption>";
echo "<tr>";
echo "<th>Purchase Dt</th>";
echo "<th>Trans Cd</th>";
echo "<th>Trans Desc</th>";
echo "<th>Trans Type</th>";
echo "<th>Amount</th>";
echo "</tr>";
$query2 = "SELECT p.Memid, p.PurchaseDt, p.TransCd, c.TransDesc, p.TransType, p.Amount
FROM tblpurchases p, tblcodes c
WHERE p.TransCd = c.TransCd AND p.MemId = 'member id'
ORDER BY p.MemId, p.PurchaseDt, p.TransCd
";
$r2 = mysqli_query($dbc, $query2);
while ($row = mysqli_fetch_array($r2)) {
echo "<tr>";
echo "<td>".$row['PurchaseDt']."</td>;";
echo "<td>".$row['TransCd']."</td>";
echo "<td>".$row['TransDesc']."</td>";
echo "<td>".$row['TransType']."</td>";
echo "<td>".$row['Amount']."</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo '<p>No Member ID from form.</p>';
}
?>
the results page should be showing tables with the info in the TH and TR/TD areas. Both those areas are coming from a separate SQL table, and teh the only similar field between the tblmembers and tblpurchases is MemID.
You need to use a join in your sql request to show purchases by members.
SELECT m.Memid, p.PurchaseDt, p.TransCd,
FROM tblpurchases p join
tblmembers m on p.MemId=m.MemId
This is an example of a join
How can I echo inside an html tag? I read some articles here and I understood that I have to put the all HTML tag inside the PHP, but when I do, it won't show the correct layout. I mean, it puts it in the top of the page instead of showing it where it belong. How can I make this work? Attached two pictures to explain my broken english better
<?php
include("config.php");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT id FROM users";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<div class='inner'> <h2 class='major'>How many accounts</h2> <p>We currently have ". $row["id"] . " accounts!</p></div>";
}
} else {
echo "0 results";
}
$con->close();
?>
And I would like to put only row id inside HTML tag because, in the HTML inside PHP it shows the first on the page, it doesn't show where it should belong.
<section id="four" class="wrapper alt style1">
<div class="inner">
<h2 class="major">How many accounts</h2>
<p>We currently have <?php echo $row["id"] ?> accounts!</p>
How it shows now:
How it should look
Why don't you echo $result->num_rows instead of id? What happen if someone close their account or you delete some rows in the middle of table?
I have reached to an answer.
in HTML section I added <?php echo $ID ?> and in the PHP section I modified it with
while($row = $result->fetch_assoc()) {
$ID = $row['id'];
}
Now script looks like this
<?php
include("config.php");
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT id FROM users";
$result = $con->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
$ID = $row['id'];
}
} else {
echo "0 results";
}
$con->close();
?>
and the HTML part
<h2 class="major">How many Accounts?</h2>
<p>Currently we have <?php echo $ID ?> accounts.</p>
Assign a variable to your result.
while($row = $result->fetch_assoc()) {
$id = $row["id"]; }
Then anywhere in your html all you will have to do is the following.
<?php echo $id; ?>
I hope this helps. Have a great day.
I have created dynamic buttons in php but i want to fetch value from databse and set it to the buttons but it didnt fetch and set.give any solution for this. In databse there is 5 values and that value i want to set to the buttons
following code i have tried.
<?php
function dash()
{
include 'config.php';
$sql = "SELECT roomno FROM roombook";
if($result = mysqli_query($db, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
$str='';
$roomno=array($row['roomno']) ;
// $aa=array($roomno);
//echo "$arr";
while(list($k,$v)=each($roomno)) {
$str.='<input type="submit" name="btn_'.$k.'" value="'.$v.'" id="btn_'.$k.'"/>';
}
return $str;
}
// Free result set
mysqli_free_result($result);
} else{
echo "No records matching your query were found.";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
// Close connection
mysqli_close($db);
//$btn=array(1=>'hjck',2=>'102',3=>'104');
}
?>
<!Doctype html>
<html>
<body>
<div id="bpanel" >
<?php echo dash();?>
</div>
</body>
</html>
As roonno is a comma delimited list an explode() is what you need to do here
$sql = "SELECT roomno FROM roombook";
if($result = mysqli_query($db, $sql)){
$str = '';
while($row = mysqli_fetch_array($result)){
// generate array from comma delimited list
$rooms = explode(',', $row['roomno']);
foreach ( $rooms as $k=>$v ) {
$str .= '<input type="submit" name="btn_'.$k.'" value="'.$v.'" id="btn_'.$k.'"/>';
}
}
return $str;
} else {
//echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
// fixed? $link shoudl be $db
echo "ERROR: Could not able to execute $sql. " . mysqli_error($db);
}
I want to access while array $row2 outside the loop so that I can compare in if else condition, because $row2 array contains many number. Is there any solution make $row array accessible outside the loop or any other method?
<?php
$sql = (" SELECT * FROM result ");
$query = mysqli_query($db, $sql);
while($row = mysqli_fetch_array($query)) {
$row2 $row['id'];
}
for($i=1;$i<=10;$i++) {
if ($i<= $row2) {
echo "<font color='red'><font size='50px'>".$i."</font></font>"."<br>";
continue;
} else {
echo $i.'<br>';
}
}
?>
If you want to loop over two dependent things you need to nest them or else $row2 will always contain the last value in the while loop:
<?php
$sql = (" SELECT * FROM result ");
$query = mysqli_query($db, $sql);
while ($row = mysqli_fetch_array($query)) {
$row2 = $row['id']; // I added an = sign here.
for($i=1;$i<=10;$i++) {
if ($i<= $row2) {
echo "<font color='red'><font size='50px'>".$i."</font></font>"."<br>";
continue;
} else {
echo $i.'<br>';
}
}
}
If this isn't what you want please clarify your question.
Pass your variable to a separate function that performs whatever task you want.
<?php
while($row = mysqli_fetch_array($query))
{
MyFunction($row2);
}
function MyFunction($passed_row2_value){
// perform whatever task you want here.
}
?>
This shows with some format the ids retrieved by the query and fills the blanks without format.
<?php
$sql = (" SELECT * FROM result ");
$query = mysqli_query($db, $sql);
$row2 = [];
while($row = mysqli_fetch_array($query)) {
$row2[$row['id']] = true;
}
for($i=1;$i<=10;$i++) {
if ($row2[$i]) {
echo "<font color='red'><font size='50px'>".$i."</font></font>"."<br>";
} else {
echo $i.'<br>';
}
}
?>
I am trying to display a list of persons. On clicking the list , more details on the person must be shown.
members.php: -a page to list all members
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<h1>Our members are</h1>
<?php
include 'config.php';
session_start();
$sql = "SELECT name from users";
$result = $conn->query($sql);
echo '<table>';
if($result->num_rows > 0)
{
while($row = $result->fetch_assoc())
{
echo '<tr>';
$_SESSION['selection'] = $row['name'];
echo '<td>'.$row['name'].'</td>';
echo '</tr>';
}
}
echo '</table>';
$conn->close();
?>
</body>
view.php -the page to show more details:
<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
include 'config.php';
session_start();
$pkey = $_SESSION['selection'];
$new = "SELECT * FROM users where name = '$pkey'";
$display = $conn->query($new);
$result = $display->fetch_assoc();
echo 'Name:'.$result['name'];
echo 'Rollno:'.$result['rool_no'];
echo 'Description:'.$result['description'];
$conn->close();
?>
</body>
on using session , always the details of last person in the list is shown.
How to display the corresonding details??
finally you are saving last name in the session variable $_SESSION['selection'] = $row['name']; so you getting the last name .while loop end with last name and you saving it in session.you are not using session as array know that.
my advice is not use session. session is not required here.
while($row = $result->fetch_assoc())
{
echo '<tr>';
$_SESSION['selection'] = $row['name'];
echo '<td>'.$row['name'].'</td>';
echo '</tr>';
}
change these lines in members.php
while($row = $result->fetch_assoc())
{
echo '<tr>';
echo '<td>'.$row['name'].'</td>';
echo '</tr>';
}
and change these line in view.php
<body>
<?php
include 'config.php';
session_start();
$pkey = $_REQUEST['name'];//change this line alone
$new = "SELECT * FROM users where name = '$pkey'";
$display = $conn->query($new);
$result = $display->fetch_assoc();
echo 'Name:'.$result['name'];
echo 'Rollno:'.$result['rool_no'];
echo 'Description:'.$result['description'];
$conn->close();
?>