简体   繁体   English

如何在相同ID但不同列值下插入值?

[英]how to insert values under same id but different column values?

I like to insert my data like following structure. 我喜欢按照以下结构插入我的数据。

    +------+----+
    |id|JAN||FEB|
    +------+----+
    |1 |  1||  5|
    +------+----+
    |2 |  8|| 12|
    +------+----+
    |3 | 15|| 19|
    +------+----+
    |4 | 22|| 26|
    +------+----+


<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "epi";

$conn = new mysqli($servername, $username, $password, $dbname);

if (!$conn) 
{
  die('Could not connect: ' . mysql_error());
}

$number_of_dates = 4;
$startDate_Jan = strtotime("2018-01-01");
for ($i = 0; $i <= $number_of_dates; $i++)
{
    $date = strtotime("Monday +" . ($i * 1) . ' weeks', $startDate_Jan);
    $month=date('m', $date).PHP_EOL;
    $datex=date('d', $date).PHP_EOL;
    $intmonth = intval($month);
    $intdate = intval($datex);

    echo $intdate;
    echo "<br>";

    $sql="INSERT INTO testdata (ID_DATE, EPI_DATE_JAN) VALUES ('','$intdate')";
    $result = mysqli_query($conn, $sql);
}

$number_of_dates = 4;
$startDate_Feb = strtotime("2018-02-01");
for ($i = 0; $i <= $number_of_dates; $i++)
{
    $date = strtotime("Monday +" . ($i * 1) . ' weeks', $startDate_Feb);
    $month=date('m', $date).PHP_EOL;
    $datex=date('d', $date).PHP_EOL;
    $intmonth = intval($month);
    $intdate = intval($datex);

    echo $intdate;
    echo "<br>";

    $sql="INSERT INTO testdata (ID_DATE, EPI_DATE_FEB) VALUES ('','$intdate')";
    $result = mysqli_query($conn, $sql);
}
$conn->close();
?>

when I do like above then total 8 id is being created. 当我像上面一样时,则创建了总共8个id。 So now I want to fix my problem. 所以现在我想解决我的问题。 tell me some solution how can solve my problem. 告诉我一些解决方法如何解决我的问题。 if you have still confusion then ask me, I will give some details also. 如果您仍然感到困惑,请问我,我也会提供一些细节。

If you have only 4 rows in your table and the ID_DATE is the number of the week, you could use an UPDATE in your second loop as follow: 如果表中只有4行,并且ID_DATE是星期数,则可以在第二个循环中使用UPDATE,如下所示:

// second loop:
$week = $i+1;
$sql="UPDATE testdata set EPI_DATE_FEB = $intdate WHERE ID_DATE=$week";
$result = mysqli_query($conn, $sql);

So the second loop could be: 因此,第二个循环可能是:

$number_of_dates = 4;
$startDate_Feb = strtotime("2018-02-01");
for ($i = 0; $i <= $number_of_dates; $i++)
{
    $date = strtotime("Monday +" . ($i * 1) . ' weeks', $startDate_Feb);
    $month=date('m', $date).PHP_EOL;
    $datex=date('d', $date).PHP_EOL;
    $intmonth = intval($month);
    $intdate = intval($datex);

    echo $intdate;
    echo "<br>";

    $week = $i+1;
    $sql="UPDATE testdata SET EPI_DATE_FEB=$intdate WHERE ID_DATE=$week";
    $result = mysqli_query($conn, $sql);
}

But it's even better to use prepared queries . 但是使用准备好的查询甚至更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将数组值插入具有相同ID的mysql中 - how to insert array values into mysql with the same id 如何将数组值插入不同的 MySQL 列 - How to insert array values to different MySQL column 在 Laravel 搜索结果的相同 column2 值下分组并显示不同的 column1 值 - Group and Display different column1 values under the same column2 values from Laravel Search Results 如何在mysql的同一列中计算不同的值? - How to count different values in a same column in mysql? 如何 select 在具有相同 ID 的列中的不同值然后删除它们 - How to select different values in a column with the same ID and then delete them PHP SQL Server 如何在数据库中具有相同ID的多行中插入复选框的多个值 - How to insert multiple values of checkbox in multiple rows with same id in database 选择具有相同id但不同值的不同行 - Selecting different rows with same id but different values 给定相同的主键,如何检查插入值是否不同? - How to check insert values are different given same primary key? 如何在单次插入时插入具有不同日期的相同值? - How I can insert same values with different date on single insertion? 如何以json格式将相同值的数组插入到列php mysql中 - How to insert arrays of same values in json format into a column php mysql
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM