从php中的表中选择并使用值,然后将其插入到同一个表中的不同列中?

时间:2017-05-12 18:33:28

标签: php mysql sql database mysqli

假设我在表中有4列,我在PHP中选择三列进行计算和转换,并希望使用新的更新值更新最后一列。

从同一个表中选择和插入两个值的最佳方法是什么?我尝试在PHP中的条件内打开一个新查询。没有工作......然后我尝试将它放在同一个查询“INSERT INTO hired_jobslocal”和其余的代码中。整件事情都崩溃了。

 date_default_timezone_set("Asia/Kolkata");
date_default_timezone_get();
$date = date('Y/m/d H:i:s', time());


$sql = "SELECT hjl.local_job_id, hjl.job_status, hjl.hire_time_date, hjl.hirer_id, hjl.worker_id 
        FROM hired_jobslocal hjl, WHERE $job_status = 'open' AND $diff > 2332800";

    $result = mysqli_query($con, $sql);

    //Current date time

    if (mysqli_num_rows($result) > 0) {
 // output data of each row
 while($row = mysqli_fetch_assoc($result)) {

     //Getting values from table
    $local_job_id = $row["local_job_id"];
    $job_status = $row["job_status"];
    $hire_time_date = $row["hire_time_date"];

    //Checking the difference for 27 days
    //Getting difference in Unix Format(Decimal Seconds)
    $diffinSeconds = strtotime("$date") - strtotime("$hire_time_date");
    $diff = floor($diffinSeconds);

    //Getting differences in days hours and seconds
    $days    = floor($diffinSeconds / 86400);
    $hours   = floor(($diffinSeconds - ($days * 86400)) / 3600);
    $minutes = floor(($diffinSeconds - ($days * 86400) - ($hours * 3600))/60);
    $seconds = floor(($diffinSeconds - ($days * 86400) - ($hours * 3600) - ($minutes*60)));


    echo $local_job_id, '-local_job_id<br/>';
    echo $hire_time_date, '- Hirer time date<br/>';
    echo $date, '- Current time <br/>';
    echo $days, '-Difference in days <br/>';
    echo $diff, '-Difference in seconds <br/>';
    echo $job_status, '-job Status<br/><br/><br/>';

1 个答案:

答案 0 :(得分:0)

您的代码应该是什么样的模型:

<?php
$query = "SELECT id,col1,col2,col3 FROM your_table WHERE $job_status = 'open' AND $diff > 2332800";
//execute query
//fetch the result and do you calculation
// set a variable to store your id or an array if nur_rows>1
//if num_rows = 1
$query="UPDATE your_table SET col4 =what_you_want_to_enter WHERE id=$your_id";
//else
foreach($your_array AS $your_id){
$query="UPDATE your_table SET col4 =what_you_want_to_enter WHERE id=$your_id";
}
?>