I am having a problem again with my MySQL query. I am trying to execute my query using my $_GET variable. so here is my code. and many thanks in advance and answer will be very appreciated.
and please dont down vote my question if anyone thinks its not correct, please edit it or tell me to edit it.
many thanks.
here is Code.
$Status = $_GET['status'];
$User = $_GET['user'];
require('./connect.php');
$query = "SELECT * FROM users ORDER BY id ASC";
$result = mysqli_query($con, $query);
$numrows = mysqli_num_rows($result);
if ($numrows > 0) {
echo '<table class="table" border="1">
<tr style="background-color: #0DF; color: #222; font-weight:bold;">
<td>ID:</td>
<td>User Name:</td>
<td>Email:</td>
<td>First Name:</td>
<td>Last Name:</td>
<td>Domain:</td>
<td>Country:</td>
<td>Phone:</td>
<td>Plan:</td>
<td>Duration:</td>
<td>Payable:</td>
<td>Paid Date:</td>
<td>Active Plan:</td>
<td>Active:</td>
<td>Register Date:</td>
</tr>';
while ( $row = mysqli_fetch_assoc($result) ) {
$dbid = $row['id'];
$dbuser = $row['username'];
$dbemail = $row['email'];
$dbfname = $row['first_name'];
$dblname = $row['last_name'];
$dbdomain = $row['domain'];
$dbcountry = $row['country'];
$dbphone = $row['phone'];
$dbplan = $row['plan'];
$dbduration = $row['duration'];
$dbpayable = $row['payable'];
$dbpaid_date = $row['paid_date'];
$dbactive_plan = $row['active_plan'];
$dbactive = $row['active'];
$dbdate = $row['date'];
if ( $dbactive_plan == 0) {
$status = "Activate";
$changeStatus = ''.$status.'';
}
else {
$status = "Deactivate";
$changeStatus = ''.$status.'';
}
echo '
<tr>
<td>'.$dbid.'</td>
<td>'.$dbuser.' Delete</td>
<td>'.$dbemail.'</td>
<td>'.$dbfname.'</td>
<td>'.$dblname.'</td>
<td>'.$dbdomain.'</td>
<td>'.$dbcountry.'</td>
<td>'.$dbphone.'</td>
<td>'.$dbplan.'</td>
<td>'.$dbduration.'</td>
<td>'.$dbpayable.'</td>
<td>'.$dbpaid_date.'</td>
<td>'.$dbactive_plan.' '.$changeStatus.'</td>
<td>'.$dbactive.'</td>
<td>'.$dbdate.'</td>
</tr>';
}//while loop
echo '</table>';
if ($Status == 1 && $User == $dbuser) {
$query = "UPDATE users SET active_plan='$Status' WHERE username='$User'";
mysqli_query($con, $query);
echo "$Status";
}
else if ($Status == 0 && $User == $dbuser) {
$query = "UPDATE users SET active_plan='$Status' WHERE username='$User'";
mysqli_query($con, $query);
echo "$Status";
}
UPDATED
You're defining $dbuser inside the while loop, so it is getting set to the username from the last iteration of the loop. I'm not sure based on your code what $dbuser is supposed to be, judging by your code....
Related
I inserted the data in the table very well and it's showing in phpMyadmin but when i try to display data from the database only one single item is displayed. I need help.
Code and screenshoots below
<?php
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
$count = mysqli_num_rows($result);
if($count > 0){
while($row = mysqli_fetch_assoc($result)){
$incidentId = $row['id'];
$icTypeName = $row['inctype_name'];
$addedBy = $row['added_by'];
$addedOn = $row['added_on'];
}
?>
<tr class="tableData">
<td><?php echo $incidentId;?></td>
<td><?php echo $icTypeName;?></td>
<td><?php echo $addedBy;?></td>
<td><?php echo $addedOn;?></td>
<td><i class="uil uil-edit icon editIcon"></i></td>
</tr>
<?php
} else {
$_SESSION['noData'] = 'No incidents to show!';
}
} else {
die('Failed to Connect!');
}
?>
Images below
I expect to get all the data in the database tables displayed with the "SELECT * FROM nw"
Simply move the } that ends the while loop to after the code that is using the variables. You are currently consuming all the resultset before outputting anything, so you will only see the last rows data.
<?php
$sql = "SELECT * FROM nw";
$result = mysqli_query($conn, $sql);
if($result == TRUE){
$count = mysqli_num_rows($result);
if($count > 0){
while($row = mysqli_fetch_assoc($result)){
$incidentId = $row['id'];
$icTypeName = $row['inctype_name'];
$addedBy = $row['added_by'];
$addedOn = $row['added_on'];
//} REMOVED
?>
<tr class="tableData">
<td><?php echo $incidentId;?></td>
<td><?php echo $icTypeName;?></td>
<td><?php echo $addedBy;?></td>
<td><?php echo $addedOn;?></td>
<td><i class="uil uil-edit icon editIcon"></i></td>
</tr>
<?php
} // END OF WHILE LOOP
} else {
$_SESSION['noData'] = 'No incidents to show!';
}
} else {
die('Failed to Connect!');
}
?>
I created a table which is updated through a form and each row gets assigned a specific number.
When viewing this table, I want to click on that assigned number and get a page where all the details of that row are displayed.
If I do $sql = "SELECT * FROM clients WHERE nif_id='114522';"; - where the nif_id is the assigned number - I get the values for that number, but I need it to change with every number in the table.
Any ideas?
UPDATE
This is the table code:
<div class="card card-body">
<table class="table">
<thead>
<tr>
<th>NIF</th>
<th>Nome</th>
<th>Apelido</th>
<th>Telemóvel</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
<?php
include_once '../includes/db.inc.php';
$sql = "SELECT * FROM clients ORDER BY nif_id ASC;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$first = $row["prm_nome"];
$last = $row["apelido"];
$phone = $row['nmr_tlm'];
$email = $row['mail'];
$nif = $row['nif_id'];
echo '<tr>';
echo '<td>'.$nif.'</td>';
echo '<td>'.$first.'</td>';
echo '<td>'.$last.'</td>';
echo '<td>'.$phone.'</td>';
echo '<td>'.$email.'</td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
</div>
You can use the get request parameters.
ex: www.myapp.com/table?id=3920393
add functionality in your PHP file as follows
if(isset($_GET["id"])){
$id = $_GET["id"];
$sql = "SELECT * FROM clients WHERE nif_id='".$id."';";
//make db call & display HTML
}
This is a very simple implementation and does not implement any security or SQL injection security. This was more of a conceptual answer as to how you can tackle your problem.
This is quite a common scenario for web-based systems.
<div class="card card-body">
<table class="table">
<thead>
<tr>
<th>NIF</th>
<th>Nome</th>
<th>Apelido</th>
<th>Telemóvel</th>
<th>E-mail</th>
</tr>
</thead>
<tbody>
<?php
include_once '../includes/db.inc.php';
$sql = "SELECT * FROM clients ORDER BY nif_id ASC;";
$result = mysqli_query($conn, $sql);
$resultCheck = mysqli_num_rows($result);
if ($resultCheck > 0) {
while ($row = mysqli_fetch_assoc($result)) {
$first = $row["prm_nome"];
$last = $row["apelido"];
$phone = $row['nmr_tlm'];
$email = $row['mail'];
$nif = $row['nif_id'];
echo '<tr>';
echo '<td>'.$nif.'</td>';
echo '<td>'.$first.'</td>';
echo '<td>'.$last.'</td>';
echo '<td>'.$phone.'</td>';
echo '<td>'.$email.'</td>';
echo '</tr>';
}
}
?>
</tbody>
</table>
</div>
where the detail.php is another page to query specific details regarding the query nifid.
As a reminder, if the data type of the column is INT, there is no need to use single quotes to surround the value in the SQL statement.
Sample detail.php:
<?php
if(!isset($_GET['nifid']) || (int)$_GET['nifid'] <= 0) {
// Invalid or missing NIFID
header('Location: table.php');
}
include_once '../includes/db.inc.php';
$id = (int)$_GET['nifid'];
$sql = "SELECT * FROM clients WHERE nif_id=$id";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);
// TODO: display the result in whatever way you like
?>
i should make a "cheque" and there should be (Index,id,....), but index should reset after 100, but id must be auto_incremented.
Here is my code what i did. I didnt get how can i reset index to 0. I cant even show the indexs value, it shows 0 everytime when i add something.
<?php
$host = "localhost";
$user = "root";
$password = "";
$database = "dbcar";
$number = "";
$state_number = "";
$model = "";
$status = "";
$conditions = "";
$date = "";
$number_index = "1";
$connect = mysqli_connect($host,$user,$password,$database);
echo "<h1>Report Log </h1>";
$sqlget = "SELECT * FROM carserv ORDER BY date DESC LIMIT 1";
$sqldata = mysqli_query($connect,$sqlget) or die("error");
echo "<table>";
echo"<tr>
<th>INDEX</th>
<th>ID</th>
<th>State Number</th>
<th>Model</th>
<th>Status</th>
<th>Condition</th>
<th>Date</th>
</tr>";
while($row = mysqli_fetch_array($sqldata,MYSQLI_ASSOC)){
echo" <tr><td>";
echo $row['number_index'];
echo" </td><td>";
echo $row['number'];
echo" </td><td>";
echo $row['state_number'];
echo" </td><td>";
echo $row['model'];
echo" </td><td>";
echo $row['status'];
echo" </td><td>";
echo $row['conditions'];
echo" </td><td>";
echo $row['date'];
echo" </td></tr>";
}
echo "</table>";
function getPosts(){
$posts = array();
$posts[0] = $_POST['state_number'];
$posts[1] = $_POST['number'];
$posts[2] = $_POST['model'];
$posts[3] = $_POST['status'];
$posts[4] = $_POST['conditions'];
$posts[6] = $_POST['number_index'];
return $posts;
}
?>
and here is my output: http://imgur.com/GOuCcBU
Take look in your phpmyadmin page there is auto_increment option you need to just have two field check auto increment for id field and for index you just fetch it and save it to db in another field with name number_index(because index is a reserved word).
Reset your db to O by doing something like this is your counter_update.php
if($_POST['index_reset']) {
$index_reset = $_POST[index_reset];
mysql_connect("server", "username", "password") or die(mysql_error());
mysql_select_db("database") or die(mysql_error());
$sql = 'UPDATE counters SET number_index =\'0\' WHERE Page = \'$index_reset\';';
}
And html side something like this
$page_reset = "<form id='Reset' action='counter_update.php' method='post'>
<button type='submit' name='index_reset' value='$formPage'>RESET</button>
</form>";
I have tried but i could not just get where i am doing it wrong. The table only echo the first row continuously and did not echo the other rows. Kindly help please.
$run = "
SELECT * FROM staff
";
$runquery = mysqli_query($connection, $run);
$runrow = mysqli_num_rows($runquery);
if($runrow < 1){
echo "<p class='errormsg'>You do not have any Staff</p>";
}
else{
$row = mysqli_fetch_array($runquery);
if($row) {
$surname = $row['surname'];
$lastname = $row['lastname'];
$phone = $row['phone'];
$username = $row['username'];
$role = $row['auth'];
}
foreach ($row as $staff) {
$table .= "
<tr>
<td>$surname</td>
<td>$phone</td>
<td>$username</td>
<td>$role</td>
</tr>
";
}
}
You need to change loop like this. also the tabular view of your record require <table> body.
$run = "SELECT * FROM staff";
$runquery = mysqli_query($connection, $run);
$runrow = mysqli_num_rows($runquery);
if( $runrow < 1 ) {
echo "<p class='errormsg'>You do not have any Staff</p>";
}
else {
$table = ""; // create table here
while($row = mysqli_fetch_array($runquery)) {
$surname = $row['surname'];
$lastname = $row['lastname'];
$phone = $row['phone'];
$username = $row['username'];
$role = $row['auth'];
$table .= "<tr>
<td>$surname</td>
<td>$phone</td>
<td>$username</td>
<td>$role</td>
</tr>";
}
echo $table;
}
did you try to move the following code part into the foreach-loop?
$run = "
SELECT * FROM staff
";
$runquery = mysqli_query($connection, $run);
$runrow = mysqli_num_rows($runquery);
if($runrow < 1){
echo "<p class='errormsg'>You do not have any Staff</p>";
}
else{
$row = mysqli_fetch_array($runquery);
foreach ($row as $staff) {
$surname = $staff['surname'];
$lastname = $staff['lastname'];
$phone = $staff['phone'];
$username = $staff['username'];
$role = $staff['auth'];
$table .= "
<tr>
<td>$surname</td>
<td>$phone</td>
<td>$username</td>
<td>$role</td>
</tr>
";
}
}
<?php
$results = mysql_query("SELECT * FROM ".TBL_SUB_RESULTS."
WHERE user_submitted != '$_SESSION[username]' AND home_user = '$_SESSION[username]' OR away_user =
'$_SESSION[username]' ") ;
$num_rows = mysql_num_rows($results);
if ($num_rows > 0)
{
while( $row = mysql_fetch_assoc($results))
{
extract($row);
$q = mysql_query("SELECT name FROM ".TBL_FRIENDLY." WHERE id = '$ccompid'");
while( $row = mysql_fetch_assoc($q))
{
extract($row);
?>
<table cellspacing="10" style='border: 1px dotted' width="300" bgcolor="#eeeeee">
<tr>
<td><b><? echo $name; ?></b></td>
</tr><tr>
<td width="100"><? echo $home_user; ?></td>
<td width="50"><? echo $home_score; ?></td>
<td>-</td>
<td width="50"><? echo $away_score; ?></td>
<td width="100"><? echo $away_user; ?></td>
</tr><tr>
<td colspan="2">Accept / Decline</td>
</tr></table><br>
<?
}
}
}
else
{
echo "<b>You currently have no results awaiting confirmation</b>";
}
?>
I am trying to run two queries as you can see.
But they aren't both working.
Is there a better way to structure this, I am having a brain freeze!
Thanks
OOOH by the way, my SQL wont stay in this form!
I will protect it afterwards
SELECT *
FROM TBL_SUB_RESULTS ts
LEFT JOIN
TBL_FRIENDLY tf
ON tf.id = ts.ccompid
WHERE ts.user_submitted != '$_SESSION[username]'
AND ts.home_user = '$_SESSION[username]' OR ts.away_user = '$_SESSION[username]'
For debuging purposes, try to print your queries to the sceen. Check if you find any errors in your final query. You might just have misspelled a variable name.
<?php
$query = "SELECT * FROM ".TBL_SUB_RESULTS." WHERE user_submitted != '$_SESSION[username]' AND home_user = '$_SESSION[username]' OR away_user = '$_SESSION[username]'";
echo "query: ".$query;
$results = mysql_query($query);
// rest of code...
I think you need brackets in your WHERE clause:
WHERE user_submitted != '$_SESSION[username]'
AND (home_user = '$_SESSION[username]'
OR away_user = '$_SESSION[username]')