[英]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.