How to display both old and new values of particular column in sql on html using php?

php mysql fetch array multiple rows
how to fetch single column from database in php
store mysql query results in php array
mysql_fetch_assoc all rows
how to store values to array from mysql database in php
php mysql fetch single value
call to undefined function mysql_fetch_assoc()
how to fetch data from database and store in variable in php

Let us assume there is a form which has a text field and a submit button.

Every time I type a text and submit it should be stored in the database and displayed in a table.

Additionally, when I again submit the form the old text in SQL column value should be overwritten with this new value and both values should be displayed in table rows.

So every time I do this I want old data and new data to be appended to table rows.

How to achieve this??

Instead of overwriting the data, why not create a new entry and mark it as active, such that to get the latest data using sql, you order by primary key desc limit 1. If you need the old data you ust get the previous entry.

mysql_fetch_field - Manual, mysql_fetch_field — Get column information from a result and return as an object If field or tablenames are aliased in the SQL query the aliased name will be both columns; var_dump() will show you that you get the following for both fields: on live server (unless user is restricted or replace the $_GET with fixed value.) In this article, we’re going to show you how to use PHP to Connect to and Retrieve Data from MySQL. Step 1. Create our SQL Query to grab all comments. In order to display comments on a page, we first need to know what comments to show. When we setup our site we created two pages, and each page was assigned a unique id number.

We have very few information, but here is some idea to achieve it :

1/ Add an last field in your table and update it each time you add a new data, then when you display it just get all data and check the last field to see which one is the last :

So imagine this table :

Table data
===============================
id_data | id_user | data | last

When you display it for your user :

select * from data where id_user = :id_user order by last desc;

This way you will get the last one with last = 1 (true) first then the other.

And when you submit a new data :

// you update all old data as "old"
update data set last = 0 where id_user = :id_user; 
// you create a new data
insert into data (id_user, data, last) values (:id_user, :data, 1); 

2/ Add a field date so you know wich one are older than other

Table data
==================================
id_data | id_user | data | created

When you display it for your user :

select * from data where id_user = :id_user order by created desc;

This way you will get the data order by the created date with the last one first.

And when you submit a new data :

// you create a new data with the current date
insert into data (id_user, data, created) values (:id_user, :data, NOW()); 

This solution is better I think so you can have some "historic" of each data.

Is it what you are looking for?

mysql_fetch_array - Manual, By using MYSQL_BOTH (default), you'll get an array with both associative and $resultset = new mysql_resultset(mysql_query($sql)); ?> for the problem with fields containing null values in an associated array, feel free to use my main purpose was to show the fetched array into a table, showing the results side by side  I've assumed the User and Genre tables both have a column Name - join to these tables to look up the rating. The GROUP and MAX will eliminate any cases where the same user has more than one rating in the same Genre (switch the MAX to AVG or MIN if you need otherwise). Top 8 will restrict the genres. So adjust the Sql like so:

why not just append it with a pipe character like so... "|entry_data" then just explode it when you need it.

var data_array = explode("|",data);
print_r(data_array);

mysql_fetch_assoc - Manual, echo "Could not successfully run query ($sql) from DB: " . mysql_error(); exit; } Note: If you're expecting just one row, no need to use a loop Note: This function sets NULL fields to the PHP NULL value. Fetch a result row as an associative array, a numeric array, or both; mysql_data_seek() - Move internal result pointer  COLUMN column_name OLD_VALUE variable_name. SQL*Plus prints the bottom title as part of the process of breaking to a new page--after finding the new value for the master column. Therefore, if you simply referenced the NEW_VALUE of the master column, you would get the value for the next set of details.

If you want to show all changes only for some users then better to save you last value in main table and create new history table for changes.

table_history
id | id_row | field | before | after 

In this case you can without additional query show last value for some users and all data for others.

In this structure you also can save multiple fields data if you need. And believe me, saving previous value in your table will make you life is easy in future.

Can I use MySQL COUNT() and DISTINCT together?, How do I find duplicate values in a column in SQL query? Combines the result set of two or more SELECT statements (only distinct values) UNION ALL: Combines the result set of two or more SELECT statements (allows duplicate values) UNIQUE: A constraint that ensures that all values in a column are unique: UPDATE: Updates existing rows in a table: VALUES: Specifies the values of an INSERT INTO statement: VIEW

In given below code i update field with new one insert data and also append your new data with old I hope this help you

Form from which you insert and update data
if(isset($_POST['submit']) && $_POST['submit']){
    $text_field = $_POST['text_field'];
    $query ="SELECT * FROM `table_name`"; //replace with your table name
    $run=mysqli_query($conn,$query);
    $result = mysqli_fetch_row($run);
    $data = $result[1];  
    if(count($result)){
        $last_string = $data.','.$text_field;
        $update= "UPDATE `table_name` SET `text`='".$last_string."'";
        mysqli_query($conn,$update);
    }else{
        $query = "INSERT INTO `table_name`(`text`) VALUES ('".$text_field."')";
        mysqli_query($conn,$query);
    }
}
 ?>

 <form action="" method="POST">
    <input type="text" name="text_field">
    <input type="submit" name="submit">
 </form>
view of data which is inserted
<?php 
$query ="SELECT * FROM `table_name`";
$run=mysqli_query($conn,$query);
$result = mysqli_fetch_row($run);
if(isset($result) && count($result)){
    echo "<p>$result[1]</p>"; 
}

PHP mysqli fetch_field() Function, How do I use count and distinct together in SQL? Additionally, when I again submit the form the old text in SQL column value should be overwritten with this new value and both values should be displayed in table rows. So every time I do this I want old data and new data to be appended to table rows .

SQL Keywords Reference, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  In other words, it prevents various rows from having similar values or sequences of columns. It doesn’t allow null values. The following SQL query lists all the fields in a table’s primary key. SELECT * from Sys.Objects WHERE Type='PK' 16. Query for Displaying Unique Keys. A Unique Key allows a column to ensure that all of its values are different.

SQL SELECT DISTINCT Statement, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  Update Records In MYSQL Database Using PHP In this tutorial I created one index.php to display all the records. on the update button of particular records the ID and new values of that records

SQL Joins, Well organized and easy to understand Web building tutorials with lots of examples of how to use HTML, CSS, JavaScript, SQL, PHP, Python, Bootstrap, Java  There isn't another way to insert a column in a SQL Server table "in between" existing columns - you need to build a temp table and rebuild the old table. What Enterprise Manager does when you commit a reordering is to create a new table, move the data and then delete the old table and rename the new table to the existing name.

Comments
  • You need only the last one and the new one? So if you have data A, I submit with data B ==> you want data A + data B. If I submit data C, you only want data B + data C, that's it? And what if user reload page, should he still saw data B + data C or only the last one (data C)?
  • No, if u submit data c, i want data a, data b, data c. This goes on for more data.
  • And when user reload the page with the form, should he see ALL the data or only the last one? Those data are link to the user or if user A change somehting, user B will see the "old data" too?
  • only for a particular user, when the user reloads the page he should see all the data.
  • if suppose there is a user, text in a particular row, for that particular user how can i create a new entry
  • In the database table, you need to add a foreign key where you put the id of the user, so for every entry, you save the user_id in the database table. when querying, you include select ... where user_id = 1 assuming user id is 1
  • Can u clearly explain with some code, Im not understanding.
  • Put up you code so that I can have a clue how to help you.
  • can we insert into HTML table every time after I submit?. Like if i submit 50 i want 50
  • If the logic after each reload is the same (the last one first, then the old), you will have nothing to do : just reload your page after each submit after you add the last data and you will have what you want ==> data A --> SUB data B ==> data B / data A --> SUB data C ==> data C / data B / data A, etc.
  • You mean every time i submit a text in the textbox add it into a array?