Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6352293
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:10:29+00:00 2026-05-24T22:10:29+00:00

I have following PHP loop + SQL Update query. for ($i=0;$i<count($_POST[‘id’]);$i++) { if(!isset($_POST[‘live’][$i])){ $_POST[‘live’][$i]

  • 0

I have following PHP loop + SQL Update query.

for ($i=0;$i<count($_POST['id']);$i++) {

    if(!isset($_POST['live'][$i])){
        $_POST['live'][$i] = "0";
    } else { $_POST['live'][$i] = "1"; }

    $id = ($_POST['id'][$i]);
    $live = ($_POST['live'][$i]);
    $usr2 = $_SESSION['usr'];
    $updated = date("F j, Y, g:i a",time()+60*60);

    $sql = "UPDATE news SET live = '$live', usr2 = '$usr2', updated = '$updated' WHERE id = $id";
    $result = mysql_query($sql);
    //echo $sql."<br />";


}
if($result) {
    header("location: notes.php");
    exit();

}else {
    die("Query failed");
}

How does it work:

  • I’m submitting big form will ALL OF THE table rows.
  • receiving this in different file as an array
  • if $_POST['live'] is ‘not set’ – set it to ‘0’, if ‘set’ set it to 1
  • update array data within for loop

How to UPDATE only the rows which have been actually been changed?

Those which value from $_POST[‘live’] is actually different from this saved in DB, as the condition would be change of our $live row.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:10:31+00:00Added an answer on May 24, 2026 at 10:10 pm

    I guess you’re concerned about the updated field and that this value only changes when something has been altered. (If that’s not the case forget about this answer.)
    You can define an ON UPDATE CURRENT_TIMESTAMP clause for a timestamp field. Each time a record is updated without explicitly setting a value for this field mysql uses the current time as its new value…
    …but only if the record is altered; if you “update” the fields with the same value as are already in that record nothing happens.

    demo script:

    <?php
    $pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly');
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    
    setup($pdo);
    $stmt = $pdo->prepare('UPDATE soNews SET somevalue=:v WHERE id=:id');
    show('original', $pdo);
    
    $stmt->execute( array(':id'=>1, ':v'=>'z') );
    show('update with new=old & id=1', $pdo);
    
    $stmt->execute( array(':id'=>2, ':v'=>'y') ); // new value=old value
    show('update with new!=old & id=2', $pdo);
    
    
    
    function setup($pdo) {
        $pdo->exec('
            CREATE TEMPORARY TABLE soNews ( 
                id int auto_increment,
                somevalue varchar(32),
                updated TIMESTAMP DEFAULT 0 ON UPDATE CURRENT_TIMESTAMP,
                primary key(id)
            )
        ');
    
        $pdo->exec("INSERT INTO soNews (id,somevalue,updated) VALUES (1,'x', Now()-interval 47 hour),(2,'y', Now()-interval 47 hour)");
    }
    
    function show($label, $pdo) {
        echo "------ $label --------\n";
        foreach( $pdo->query('SELECT * FROM soNews', PDO::FETCH_ASSOC) as $row ) {
            echo join(', ', $row), "\n";
        }
    }
    

    prints

    ------ original --------
    1, x, 2011-08-16 14:09:53
    2, y, 2011-08-16 14:09:53
    ------ update with new=old & id=1 --------
    1, z, 2011-08-18 13:09:53
    2, y, 2011-08-16 14:09:53
    ------ update with new!=old & id=2 --------
    1, z, 2011-08-18 13:09:53
    2, y, 2011-08-16 14:09:53
    

    As you can see as a result of the first update query the timestamp field has been updated while the second query setting new=old didn’t affect the updated field.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: <form action= method=POST> <?php $count = isset($_POST['count']) ? $_POST['count']
I have following SQL query: $readNews_SQLselect = SELECT ; $readNews_SQLselect .= live, content, user,
I have the following pages: page1.php <?php if (isset($_GET['link'])) { session_start(); $_session['myvariable'] = 'Hello
I have the following php loop that takes an array that contains a set
I have the following PHP that creates some JSON from a MySQL query. This
I have the following checkbox created by a loop: <input type='checkbox' value=0 name='<?php echo
I have the following code with the if...else statement within a while loop. $colour
I have the following (Symfony 2) PHP Template: <?php $messages = $view['session']->getFlashes(); if (count($messages)
I have following PHP code $val=<div id=user.$row['cid']. userid=.$row['cid']. class=innertxt><img src=images/images.jpg width=50 height=50><strong>.$uname.</strong><ul> <li>Email: .$row['cemail'].</li>
I have the following php code, which has been snipped for brevity. I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.