PHP INSERT It Show only one - php

<table border="1" width="500">
<tbody>
<tr>
<th>ProFileID</th>
<th>CogID</th>
<th>FileValue</th>
<th> Count </th>
<?php
$sql="Select * From idtable"; // Read idtable
$result=mysql_query($sql); // Query sql
$num=mysql_num_rows($result); // Check record
if($num>0){ // If record > 0
$count=1; //
while($recordset=mysql_fetch_assoc($result)){ // loop
?>
<tr>
<td align="center"><?php echo $count; ?></td>
<td><?php echo $cogid = $recordset['CogID']; ?></td>
<td><?php echo $FileValue = $recordset['FileValue']; ?></td>
<td><? $url = "https://s3-ap-southeast-1.amazonaws.com/cloudpoc2/".$cogid."/Contacts/contactsversion3/".$FileValue."";
//example "https://s3-ap-southeast-1.amazonaws.com/cloudpoc2/us-east-1%3Ab9b09e99-5921-494a-b5a7-54f289131eaa/Contacts/contactsversion3/Contacts_1437450598237.txt";
$getText = file_get_contents("$url", true);
$Contact = substr_count($getText ,"FIRSTNAME");
echo $countupdate = $Contact; ?>
</tr>
<?
$count+=1; // Increase count +1
}
}
?>
</tbody>
<?php // UPDATE TO DATABASE
$updatecount = "INSERT INTO counttable (ProfileID,Count,Timestamp)
VALUES('','$countupdate','')";
mysql_query($updatecount);
?>
**I want to update count to phpmyadmin Table name is Countable I have 2 data but it show only one data i don't know what happen ** Some on kind please help me
This my pic

You do an update after while loop is over. After loop is over $updatecount stores the last value of $Contact. If you want to update table on each iteration of while loop, move your update query in the loop:
while($recordset=mysql_fetch_assoc($result)) { // loop
// do something here
$updatecount = "INSERT INTO counttable (ProfileID,Count,Timestamp) VALUES('','$countupdate','')";
mysql_query($updatecount);
}

Related

i want to calculate subtraction of row2 & row1

I display mysql table data using php.
I search but find solution for column but not for row.
Below I try to show what I want...
<?php
$i=0;
while($row = mysqli_fetch_array($result)) {
?>
<table>
<tr>
<td id="<?php echo $row["id"]; ?>"><?php echo $row["total"]; ?></td>
<td id="difference"> Difference from previous row. </td>
</tr>
<?php
$i++;
}
?>
</table>
<?php
$i=0;
$oldval = 0;
while($row = mysqli_fetch_array($result)) {
?>
<table>
<tr>
<td id="<?php echo $row["id"]; ?>"><?php echo $row["total"]; ?></td>
<td id="difference"> <?php echo ($i==0) ? $oldval : $row['total']-$oldval; ?> </td>
</tr>
<?php
$oldval = $row["total"];
$i++;
}
?>
</table>
$oldval variable is used to store current row's total field data so when you go to the next row, you can get the difference for current row because you have previous row's data stored in the $oldval
And the ternary condition I put there is because if $i==0 means the first row so you don't have any data of the previous row so by default difference is 0 and you can notice that I stored the current record's total field data after printing the difference

php mysql update 3rd column value if 1st column value > 2nd column value

I am comparing 1st [ Sent ] & 2nd column [ Last Update Date ] values. if 1st column value is greater than 2nd column value , than i want to save the value "attempted" in 3rd column [ Attempted ].
<table>
<tr>
<th class="table-header">Sent</th>
<th class="table-header">Last Update Date</th>
<th class="table-header">Attempted</th>
</tr>
<?php
if(!empty($orderrecords))
{
foreach($orderrecords as $k=>$v)
{
?>
<tr>
<td><?php echo $orderrecords[$k]["sent_date"]; ?></td>
<td><?php echo $orderrecords[$k]["lud"]; ?></td>
<td>
<?php
$orderrecords[$k]["sent_date"]= strtotime($orderrecords[$k]["sent_date"]);
$orderrecords[$k]["lud"]= strtotime($orderrecords[$k]["lud"]);
$sqlecom = 'UPDATE table SET attempted = "attempted" WHERE $orderrecords[$k]["lud"] < $orderrecords[$k]["sent_date"]';
$db_handleecom = new DBController();
$resultecom = $db_handleecom->executeUpdate($sqlecom);
echo $orderrecords[$k]["attempted"];
?>
</td>
</tr>
<?php
}
}
?>
</table>
update : I tried below code, but still 3rd column values are not saving in database.
<?php
$orderrecords[$k]["sent_date"]= strtotime($orderrecords[$k]["sent_date"]);
$orderrecords[$k]["lud"]= strtotime($orderrecords[$k]["lud"]);
if ($orderrecords[$k]["lud"] < $orderrecords[$k]["sent_date"])
{
$sqlecom = 'UPDATE table SET attempted = "attempted"
WHERE id = ' . $orderrecords[$k]['id'];
$db_handleecom = new DBController();
$resultecom = $db_handleecom->executeUpdate($sqlecom);
//echo "attempted";
}
echo $orderrecords[$k]["attempted"];
?>
Update 2 :
when i add below code on top of the file, than complete table disappears!!!! :
mysqli_report(MYSQLI_REPORT_ALL & ~MYSQLI_REPORT_INDEX);
here is full code : https://pastebin.com/VwQSjgQ5
Try this (I used same code structure you required) :
<table>
<tr>
<th class="table-header">Sent</th>
<th class="table-header">Last Update Date</th>
<th class="table-header">Attempted</th>
</tr>
<?php
if(!empty($orderrecords)){
foreach($orderrecords as $k=>$v){
?>
<tr>
<td><?php echo $v["sent_date"]; ?></td>
<td><?php echo $v["lud"]; ?></td>
<?php
if ($v["lud"] < $v["sent_date"]) {
$attempted= "attempted";
$sqlecom = 'UPDATE table SET attempted = "attempted" WHERE ORDERID = '.$v["order_id"];
$db_handleecom = new DBController();
$resultecom = $db_handleecom->executeUpdate($sqlecom);
}
else {
$attempted = "";
}
?>
<td><?php echo $attempted; ?></td>
</tr>
<?php
}
}
?>
</table>

How to go through an array and get values which I want to use in an sql query and store the retrieve value in an another array?

I want to store three variable values in an array. These are HomeResult, Draw and AwayResult. Which I want to use in an another php file. In the image u can se the final website. Problem is I can get the names of the teams and also the value of rows but result is not success.
Or is there another way to achieve this goal ?
I am not sure if it is the goed way to use B,E,F and O variable form the include php.
code of final site;
<table class="table">
<thead>
<tr>
<th>Home Team</th>
<th>Away Team</th>
<th>Result</th>
<th>B</th>
<th>E</th>
<th>F</th>
<th>O</th>
</tr>
</thead>
<tbody>
<?php
include 'ff.php';
include 'result.php';
foreach ($champs as $champ ) :
?>
<tr>
<td><?php echo $champ['HOME'];?></td>
<td><?php echo $champ['AWAY'];?></td>
<td></td>
<td><?php echo $champ['B'];?></td>
<td><?php echo $champ['E'];?></td>
<td><?php echo $champ['F'];?></td>
<td><?php echo $champ['O'];?></td>
</tr>
<?php
endforeach;
?>
file result.php
include 'champs.php';
$HomeWins=0;
$Draw=0;
$AwayWins=0;
foreach ($champs as $champ ) :
$B =$champ['B'];
$E=$champ['E'];
$F=$champ['F'];
$O=$champ['O'];
$sql = "SELECT * FROM England where B = '$B' AND E = '$E' AND F = '$F' AND O ='$O' ";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
$rowcount=mysqli_num_rows($result);
// output data of each row
while($row = $result->fetch_assoc()) {
if($row['Q'] == 1){
$HomeWins++;
}
elseif($row['Q'] == 0){
$Draw++;
}else{
$AwayWins++;
}
}
$HomeResult =round(($HomeWins/$rowcount )*100);
$DrawResult = round(($Draw/$rowcount )*100);
$AwayResult =round( ($AwayWins/$rowcount )*100);
} else {
echo "0 results";
}
endforeach;
$conn->close();
?>

Creating arrays from columns in database that I can echo in html

Im sorry if this has been answered before but I am new to PHP and MySQL and I can't figure this out.
Pretty much every time I alter my code to include an array I get a fatal error. What I am trying to do is display all the data in 3 columns from my table.
I have my site set up where you log in and I store that user's name as a "code" in a session. I have a table that has multiple user form entries that are differentiated by the user's code because in my form, I grab the code as a hidden field and add it to the entry in the table.
So far I have been able to isolate those entries by the users code, in one column I have the sum of all of the user's numerical data and I am able to echo this as a total.
I want the other 3 columns to display all the values in their columns and for each value have a line break in between them. And I am trying to print or echo these results in specific parts on a confirmation page.
I have seen examples with PDO using fetch_all and other examples of storing arrays but I can't seem to figure it out with my existing code.
Here is my existing code:
<?php
$user = *****;
$pass = *****;
$dbh = new PDO('mysql:host=localhost;dbname=*****', $user, $pass);
$stmt = $dbh->prepare("SELECT sum(price),part_number,location,price FROM products WHERE code = :usercode");
$stmt->bindParam(':usercode', $_SESSION['MM_Username']);
if ($stmt->execute()) {
$user = $stmt->fetch(PDO::FETCH_ASSOC);
}
?>
And here is where I want to display the results:
<table style="margin:0 auto;" cellspacing="7" width="100%">
<tbody>
<tr>
<td><?php echo $user['part_number']; ?></td><!--all column values-->
<td><?php echo $user['location']; ?></td><!--all column values-->
<td><?php echo $user['price']; ?></td><!--all column values-->
<td><?php echo "Total:", $user['sum(price)']; ?><br></td><!--this is ok-->
</tr>
</tbody>
</table>
Try like this:
<table style="margin:0 auto;" cellspacing="7" width="100%">
<tbody>
if ($stmt->execute()) {
while($user = $stmt->fetch( PDO::FETCH_ASSOC )){
<tr>
<td><? echo $user['part_number']; ?></td><!--all column values-->
<td><? echo $user['location']; ?></td><!--all column values-->
<td><? echo $user['price']; ?></td><!--all column values-->
<td><? echo "Total:", $user['sum(price)']; ?><br></td><!--this is ok-->
</tr>
}
}
</tbody>
</table>
There are a few things in your question that jumped out at me.
It looks like you're attempting to display both raw data (each row) and aggregate data (the sum of prices). It can be simpler to fetch the information separately instead of in the same request.
You had mentioned fetch_all in PDO, but the method is fetchAll.
Instead of working with PDO within the HTML (like iterating through while calling fetch), write code so that you're simply iterating over an array.
Based on your description of the problem, it sounds like you want to separate the total price from the raw data, so you can reduce your table down to three columns and use the table footer to show the total price.
Based on those, I have the following solution that
Separates the calls to get data into descriptive functions
Use money_format to better display prices
Removes any database-specific manipulation from the view itself.
<?php
function getTotalPriceForUser(PDO $database_handler, $user_code)
{
// If no rows are returned, COALESCE is used so that we can specify a default
// value. In this particular case, if there aren't any products that would
// match, we'd still get a result with a value of 0.
$sql = 'SELECT COALESCE(SUM(price), 0) FROM products WHERE code = ?';
$stmt = $database_handler->prepare($sql);
$stmt->execute(array($user_code));
// This fetches the first row of the result; the result is given as an array with numerical keys.
$result = $stmt->fetch(PDO::FETCH_NUM);
// [0] refers to the first column
return $result[0];
}
function getProductsForUser(PDO $database_handler, $user_code)
{
$sql = 'SELECT part_number, location, price FROM products WHERE code = ?';
$stmt = $database_handler->prepare($sql);
$stmt->execute(array($user_code));
// fetchAll returns all rows, with each row being an associative array (where part_number, location and price are the keys)
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}
// Set up the database information
$user = '*****';
$pass = '*****';
$dbh = new PDO('mysql:host=localhost;dbname=*****', $user, $pass);
// money_format to use the below money formatting; this makes sure there's a dollar sign to represent USD, for example
setlocale(LC_MONETARY, 'en_US.UTF-8');
// Store $_SESSION['MM_Username'] in a local variable
$user_code = $_SESSION['MM_Username'];
// Get the list of products associated with this user code
$products = getProductsForUser($dbh, $user_code);
// Get the total cost of the products
$total_cost = getTotalPriceForUser($dbh, $user_code);
?>
<table style="margin:0 auto;" cellspacing="7" width="100%">
<thead>
<tr>
<th>Part Number</th>
<th>Location</th>
<th>Cost</th>
</tr>
</thead>
<tfoot>
<tr>
<td style="text-align: right" colspan="2">Total:</td>
<td style="text-align: right; border-top: 1px solid #999"><?= money_format('%.2n', $total_cost) ?></td>
</tr>
</tfoot>
<tbody>
<?php foreach($products as $product): ?>
<tr>
<td><?= $product['part_number'] ?></td>
<td><?= $product['location'] ?></td>
<td style="text-align: right"><?= money_format('%.2n', $product['price']) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Change to this
<? echo
to
<?php echo
Try this:
...
$keys = array_keys($user);
foreach ($keys as $k) :
?>
<td><?= $user[$k]?></td>
<?php endforeach?>
<table>
<tbody>
if ($stmt->execute()) {
while($user = $stmt->fetch( PDO::FETCH_ASSOC )){
<tr>
<td><?php echo $user['part_number']; ?></td><!--all column values-->
<td><?php echo $user['location']; ?></td><!--all column values-->
<td><?php echo $user['price']; ?></td><!--all column values-->
<td><?php echo "Total:", $user['sum(price)']; ?><br></td><!--this is ok-->
</tr>
}
}
</tbody>
</table>

How to create dynamic links for the dynamic posts in MySQL and PHP

I'll try to explain the problem straight away. I have one HTML form which takes input just like a comment form and it saves the xyz data into a MySQL database using PHP. Now, what I want is to create and display links for those comments on a page.
I mean the comments which have been saved including the user's email and name, should be opened by clicking a link.
I don't want to display all the details on a single page from the database for all the users. There should be a page on which links are shown, when a user click a link, the full post should be displayed in next page.
There is not something which I know about this process. Please help me out.
// $rows = set of result from your database query
foreach($rows as $row){
echo '<a'
. ' href="my_link_to_display_comment?id='.$row['id'].'">'
. 'Comment from '.$row['user_name']
. '</a>';
}
First a page to display all the links like the below example -
$result = mysql_query("SELECT * FROM calendar WHERE sort_month='11'");
while($row = mysql_fetch_array($result))
{echo
"".$row['event_name'].""
;}
and then in event.php(the next page after clicking link)
$id = $_GET['id'];
$sql = "select * from calendar where id = $id";
$result = mysql_query($sql, $con);
if ($result){
$row = mysql_fetch_row($result);
$title = $row[12];
$content = $row[7];} ?>
<?php echo $title ?>
<?php echo $content ?>
If you want to show details of a single user just do this.
You can make a search box by using a form.
eg. like if I want to display a details of a student, I will search him by using his roll number and run these queries.
<?php //to search student
require_once './secure.inc.php';
$status = 0;
if(isset($_POST['submit'])){
$roll_number = $_POST['roll_number'];
$query = "select * from students where roll_number=$role_number";
require_once '../includes/db.inc.php';
$result = mysql_query($query);
if(mysql_num_rows($result)==1){
$status = 1;
$row = mysql_fetch_assoc($result); //mysql_fetch_array - both numeric and key index
}else{
$status=2;
}
}
?>
//to display
<?php } else if($status==1) { ?>
<table>
<tbody>
<tr>
<td>Roll Number : </td>
<td><?php echo $row['roll_number']; ?></td>
</tr>
<tr>
<td>Name : </td>
<td><?php echo $row['name']; ?></td>
</tr>
<tr>
<td>Gender : </td>
<td><?php echo $row['gender']; ?></td>
</tr>
<tr>
<td>Email : </td>
<td><?php echo $row['email']; ?></td>
</tr>
<tr>
<td>Mobile Number : </td>
<td><?php echo $row['mobile_number']; ?></td>
</tr>
<tr>
<td>Course : </td>
<td><?php echo $row['course']; ?></td>
</tr>
</tbody>
</table>
<?php } ?>

Categories