简体   繁体   English

如何通过SQL向数据库添加一系列值?

[英]How can I add a range of values to my database via SQL?

I need to be able to send a range of Date values to my database. 我需要能够将一系列Date值发送到我的数据库。 Here is my current working solution for adding a single date at a time: 这是我当前一次添加单个日期的工作解决方案:

HTML: EDIT: I updated the HTML form to include the from/to fields instead of a single field. HTML:编辑:我更新了HTML表单,使其包含from / to字段,而不是单个字段。

<form method="post" action="/PHP/update.php">
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">From:</td>
<td><input name="fromdate" type="date" id="fromdate"></td>
</tr>
<tr>
<td width="100">To:</td>
<td><input name="todate" type="date" id="todate"></td>
</tr>
<tr>
<td width="100">Customer Name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td width="100"> </td>
<td> </td>
</tr>
<tr>
<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="Update">
</td>
</tr>
</table>
</form>

PHP: PHP:

<?php

header( "refresh:2;url=/protected/update.html" );

if(isset($_POST['update']))
{
$dbhost = 'localhost';
$dbuser = 'USER';
$dbpass = 'PASS';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$date = $_POST['date'];
$name = $_POST['name'];

$sql = "INSERT INTO calendar (date, customer)
VALUES ('$date', '$name')";

mysqli_select_db($conn, 'website_main');
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
  die('Could not update data: ' . mysqli_error($conn));
}
echo "Updated data successfully,\n";
echo "You will be re-directed shortly.\n";
mysqli_close($conn);
}
else
{
?>
<?php
}
?>

This setup currently allows me to send a single date per click of the update button in the html form. 当前,此设置允许我在html表单中单击更新按钮来发送一个日期。

However I would like to edit the form ( which I can do myself ) to have Dates: From [dd/mm/yyyy] to [dd/mm/yyy]. 但是,我想编辑表格(我可以自己做)以具有日期:从[dd / mm / yyyy]到[dd / mm / yyy]。 and then when I hit update, the PHP would be able to fill in the blanks eg if it were 01/01/2016 to 03/01/2016, the PHP would submit to the database 01/01/2016, 02/01/2016 and 03/01/2016. 然后当我点击更新时,PHP将能够填补空白,例如,如果它是从01/01/2016到03/01/2016,则PHP将提交到数据库01/01/2016,02/01 / 2016和03/01/2016。

I am a total PHP noob, and had a lot of issues even trying to get the single date submission working. 我是一个PHP新手,即使尝试使单日提交工作也有很多问题。 Any and all help would be appreciated. 任何和所有帮助将不胜感激。

EDIT: I should have clarified this to begin with, my apologies. 编辑:首先,我应该对此作出澄清。 The Database is linked up to a calendar which updates and marks the dates red which are in the database. 数据库链接到日历,该日历更新并标记数据库中的红色日期。 Because of this, each date MUST have an individual entry in the table. 因此,每个日期必须在表中有一个单独的条目。 I CANNOT have startDate and endDate as 2 seperate columns in the same entry, due to the way that I have already setup the calendar. 由于我已经设置了日历,因此我不能在同一条目中将startDate和endDate作为2个单独的列。 Is there any way around this without having to redo the code for my calendar? 有什么办法可以解决而不必重做日历代码?

In your case I would have preferred using just two dates start_date and end_date and compare every date while retrieving data. 在您的情况下,我希望仅使用两个日期start_date和end_date并在检索数据时比较每个日期。 Well according to your requirement you can use this code and save comma separated dates. 根据您的要求,您可以使用此代码并保存逗号分隔的日期。

  while (strtotime($date) <= strtotime($end_date)) {
     echo "$date\n";
     $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
 }

However the former one will be the right solution. 但是,前者将是正确的解决方案。

You should catch both date parameters and save it in the database. 您应该捕获两个日期参数,并将其保存在数据库中。 You can do it with: 您可以执行以下操作:

$todate = $_POST['fromdate'];
$fromdate = $_POST['todate'];

Then you need to add a new column in your table. 然后,您需要在表中添加一个新列。 The new SQL can look like: 新的SQL可能如下所示:

$sql = "INSERT INTO calendar (fromdate,todate, customer)
VALUES ('".$fromdate."', '".$todate."', '".$name."')";

With the columns fromdate, todate and customer. 用栏目fromdate,todate和customer。 If you want to fill in the blank fields you should make a PHP file for the form. 如果要填写空白字段,则应为表单创建一个PHP文件。 Then you have a method to hand over the attributes. 然后,您有一种方法来移交属性。 Like sending it via GET. 就像通过GET发送一样。

header( "refresh:2;url=/protected/update.php" );

if(isset($_POST['update']))
{
$dbhost = 'localhost';
$dbuser = 'USER';
$dbpass = 'PASS';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$todate = $_POST['fromdate'];
$fromdate = $_POST['todate'];
$name = $_POST['name'];

$sql = "INSERT INTO calendar (fromdate,todate, customer)
VALUES ('".$fromdate."', '".$todate."', '".$name."')";

mysqli_select_db($conn, 'website_main');
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
  die('Could not update data: ' . mysqli_error($conn));
}
echo "Updated data successfully,\n";
echo "You will be re-directed shortly.\n";
mysqli_close($conn);
}
else
{
?>
<?php
}

Huge thanks to Harish Lalwani for pointing me down the right line for this. 非常感谢Harish Lalwani为此指出了我正确的方向。

A slight adaption to his suggestion worked a treat and I have managed to create a while loop which increments the date each loop until it reaches the end date. 稍微修改一下他的建议就可以了,我设法创建了一个while循环,该循环增加每个循环的日期,直到到达结束日期。

The working PHP: 工作的PHP:

<?php

header( "refresh:2;url=/protected/update.html" );

if(isset($_POST['update']))
{
$dbhost = 'localhost';
$dbuser = 'aidan';
$dbpass = 'Bathgate69107965';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}

$fromDate = $_POST['fromdate'];
$toDate = $_POST['todate'];
$name = $_POST['name'];

$date = $fromDate;

  while ($date <= $toDate) {

$sql = "INSERT INTO calendar (date, customer)
VALUES ('$date', '$name')";

mysqli_select_db($conn, 'website_main');
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
  die('Could not update data: ' . mysqli_error($conn));
}
echo "Updated data successfully,\n";

$date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));

}
echo "You will be re-directed shortly.\n";
mysqli_close($conn);
}
else
{
?>
<?php
}
?>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM