display post from html in following php script - php

I have a html page that posts to mysql database through a php script. How do I get the information that was entered in the html page to display after 1 record created?
<?php
$link = mysqli_connect('****', '****', '****', 'orders');
$sequence = $_POST['sequence'];
$items_count = $_POST['items_count'];
$total = $_POST['total'];
$payment_type = $_POST['payment_type'];
$sql="INSERT INTO orders (sequence,items_count,total,payment_type)
VALUES
('$sequence','$items_count','$total','$payment_type')";
if (!mysqli_query($link,$sql)) {
die('Error: ' . mysqli_error($link));
}
echo '1 record created';
mysqli_close($link);
?>

In the page that is supposed to display the posts, have another query
$sql = "SELECT sequence,items_count,total,payment_type FROM orders";
and I'm not sure if it's the right mysqli function, but after you retrieve the data from mysql as an array (mysqli_fetch_array ?) - you have to iterate through the returned results.
foreach($fetched_row as $row){
echo "<pre>"; print_r($row); echo "</pre>"
}
Which, you'll obviously want to use your own HTML - and the $row will be an associative array with the column names as the keys --

You need to add this code on the page where your fields will be displayed.
<?php
$last_record_id = mysql_insert_id();
$query = "SELECT * FROM <tablename> WHERE <$table_primary_key_id> = '$last_record_id'";
$result = mysqli_query($dbcon, $query);
$row = mysqli_fetch_array($result)
?>
Then fetch each field in separate variable like $price = $row['price'] and display them. You don't need looping because there is only one record to fetch and display.

Related

I want to fetch first five rows and display same 5 five rows at different places on single page using single query

I want to fetch first five rows and display same five rows at different places on single page, using single query
$cel_bir1=$db->query("SELECT * FROM tbl_birthday WHERE MONTH(Birthday_Date)=$month AND DAY(Birthday_Date)=$day LIMIT 4");
$get_bir=$cel_bir1->fetch(); $count=0;
while($get_bir1=$cel_bir1->fetch())
{
if($count++) echo ',';
echo $get_bir1['Name'];
}
So to get the first 5 rows, you can use Mysql Limit eg: Select * FROM mytable LIMIT 5.
Ok, typically what happens is, When using a PDO,your result-set is stored in an array.
$query = "Select * FROM mytable";
$statement = $db->prepare($query);
$statement->execute();
$productsinfo = $statement->fetchAll();//$productsinfo is the array in question
$statement->closeCursor();
Now to get the values in the array, you can loop through the array using a foreach loop like:
foreach($productsinfo as $productsinfomation):
$productid=$productsinfomation['productid'];
//you can echo $productid or anything you want to do with it here
endforeach;
There is no harm in repeating the foreach loop at different positions on your page.
Le'me know how it works for you.
Have fun coding.
$sql ="SELECT * FROM table_name LIMIT 0,5";
// $conn = your connection to database
$result = $conn->query($sql) or die($conn->error);
$html = '';
while($row = $result->fetch_assoc()){
$html.= $row['colum1'].'<br>'.$row['colum2'];
}
echo $html;
?>
// some more html code
<?php echo $html?>
// some more html code
<?php echo $html?>
// some more html code
<?php echo $html?>

incorrect result display from database

I have a database table that has 4 records with a column _id that auto increments. When I run a query to get all records, it works but it doesn't echo out all the ids, it only points to the first rows and echos it four times. I am using PHP and MySQLi. Here is my code
Code for querying
$sql = "SELECT * FROM att_table";
$query = $conn->query($sql);
$result = $query->fetch_assoc();
Code for display
do{
echo result['_id'];
}while($query->fetch_assoc());
It outputs 1111 instead of 1234. Please what is wrong?
You're fetching each of the 4 results, so it loops the appropriate number of times; but you're only assigning the fetched result to $result once, so that's the only _id value that gets echoed
do{
echo $result['_id'];
}while($result = $query->fetch_assoc())
You also can use a foreach loop :
$sql = "SELECT * FROM att_table";
$query = $conn->query($sql);
$result = $query->fetch_assoc();
foreach($result as $data){
echo $data['_id'];
}

PHP GET Rows based on URL ID [duplicate]

This question already has answers here:
How can I get an unknown username given an ID?
(2 answers)
Closed 12 months ago.
I have a search function on my site which displays results from a MySQL db. Each search result is linked to a dynamic php page with the URL, i.e. /details.php?id=123.
I need the details.php page to get the ID (e.g 123) from the URL and then fetch all rows from the database with this ID, storing them in a variable for use later. I then need to be able to echo the rows at various points throughout the page to populate the content.
The code I have so far is:
<?php
$db = mysql_connect("","","") or die("Database Error");
mysql_select_db("items",$db);
$id = $_GET['id'];
$id = mysql_real_escape_string($id);
$query = "SELECT * FROM `items- table` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
?>
I’m fairly new to PHP so not sure if the code above will get all rows based on the ID, and then how to echo the rows within divs on the page?
You can use mysql_fetch_array() OR mysql_fetch_assoc() function with while loop (if multiple rows there) OR without while loop if there is only one row.
$query = "SELECT * FROM `items- table` WHERE `id`='" . $id . "'";
$result = mysql_query($query);
while($fetch = mysql_fetch_array($result))
{
echo "<div>".$fetch['column_name']."</div>";
}
Almost correct. Just add something like this:
while ($row = mysql_fetch_assoc($result)) {
echo '<div>';
foreach ($row as $key => $val) {
echo $key.' = '.$value.'<br/>';
}
echo '</div>';
}

MYSQL query using a set of values (values stored from a session array) not working

I'm just a beginner and I'm doing a project (a shopping cart). User can add a product to the cart and the id of the product stores in a session. When I use those ids to echo out PRICE from DB it's not working. I'm using PHP & MYSQL. Here is my code
if(count($_SESSION['cart_items'])>0){
// getting the product ids
$nos = "";
foreach($_SESSION['cart_items'] as $no=>$value){
$nos = $nos . $no . ",";
}
// removing the last comma
$nos = rtrim($nos, ',');
//echo $nos; (will display like this INT VALUES 1,2,3,4)
$nos=mysql_real_escape_string($nos);
$site4->DBlogin();
$qry = "SELECT * FROM vendorproducts WHERE product_no IN('.implode(',',$nos).')";
$result = mysql_query($qry);
$row = mysql_fetch_assoc($result);
echo $row['price'];
}
PHP is not recursively embeddable:
$qry = "SELECT * FROM vendorproducts WHERE product_no IN('.implode(',',$nos).')";
^---start of string end of string ---^
Since you're already in a string, .implode(...) is just plain text, NOT executable code.
This means your query is illegal/invalid SQL, and if you had even basic/minimal error checking, would have been told about this:
$result = mysql_query($qry) or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^^
I have fixed the issue and thanx MARC for your suggestions.
I was making two mistakes:- IMPLODE input field was not an array. and as Marc said the query was not executable.
Changes made :-
$nos = rtrim($nos, ',');
**$narray = array($nos);**
$fgmembersite4->DBlogin();
$qry = **'SELECT * FROM vendorproducts WHERE product_no IN ('.implode(',',$narray).')'**;
$result = mysql_query($qry) or die(mysql_error());
**while (**$row = mysql_fetch_assoc($result)){
echo $row['price'];}

How to display the content of more then one SQL database row

I am trying to retrieve the data stored in an unknown number of mySQL database rows and display them using HTML. At the moment I can display data from one row.
Each row has a unique id number, I was planning to iterate through by comparing this number to the variable counter. But it would then leave me with the issue of displaying the results in HTML. At the moment I am just echoing variables that contain data from the rows. However what I want to create is a HTML list that increases in length depending on how many rows are in the table.
Here is my current PHP code for retrieving a row from the database is:
$sql = "SELECT * FROM project_tasks WHERE project_name='$proj_name' AND task_id='$counter'";
$query = mysqli_query($db_conx, $sql);
$row = $query->fetch_assoc();
$task_id = $row["task_id"];
$proj_name = $row["project_name"];
$task_name = $row["task_name"];
$task_importance = $row["task_importance"];
$task_description = $row["task_description"];
$task_deadline = $row["task_deadline"];
$task_members = $row["task_members"];
$task_budget = $row["task_budget"];
At the moment I am just displaying some of the results in HTML using this code:
<div id="inner_container">
<?php echo "$task_id $proj_name $task_name $task_deadline"; ?>
</div>
$sql = "SELECT * FROM project_tasks WHERE project_name='$proj_name' AND task_id='$counter'";
$query = mysqli_query($db_conx, $sql);
while($row = $query->mysqli_fetch_assoc()) {
$task_id = $row["task_id"];
$proj_name = $row["project_name"];
$task_name = $row["task_name"];
$task_importance = $row["task_importance"];
$task_description = $row["task_description"];
$task_deadline = $row["task_deadline"];
$task_members = $row["task_members"];
$task_budget = $row["task_budget"];
echo "$task_id $proj_name $task_name $task_deadline";
}
Since you have built an associative array using fetch_assoc all you need to do is loop through that array. The OO example on http://php.net/manual/en/mysqli-result.fetch-assoc.php should get you what you need. A quick example:
$sql = "SELECT * FROM project_tasks WHERE project_name='$proj_name' AND task_id='$counter'";
$query = mysqli_query($db_conx, $sql);
echo '<div id="inner_container">';
while ($row = $query->fetch_assoc()) {
$proj_name = $row["project_name"];
$task_name = $row["task_name"];
$task_deadline = $row["task_deadline"];
echo "$task_id $proj_name $task_name $task_deadline";
};
/* free result set */
$row->free();
echo '</div>;

Categories