Home ยป Php ยป can I save date as array to database in php – PHP

can I save date as array to database in php – PHP

Posted by: admin February 22, 2020 Leave a comment

Q(Question):

echo "<td>".'<input type="text" name="date_from[]" class="datepicker">'."</td>";

A(Answer):

Virenderkanger, can you be more specific with your question? Are you referring to timestamps?? ๐Ÿ™‚

A(Answer):

I want to save date from datepicker to database. I m using datepicker for no. of times but i m unable to save date to database. I can only save last selected date to all fields.

A(Answer):

how many datepicker field(s) you have in your form?

A(Answer):

It depends on driver name that i m fetching from database.

A(Answer):

if that so, put a unique id to your datepickerfield.

<input type="text" name="date_from[]" class="datepicker" id="uniqueid_'.$id.'">

your $id must be the id in your database, or the incremental one.

A(Answer):

I tried it using counter as u described($id).

A(Answer):

what happened? is it still saving the last datepicker value?

A(Answer):

using date as array it is not saving any value. it is saving 00-00-00.Will i use this id?

A(Answer):

try to change the type of your date, make it varchar

A(Answer):

It is looking impossible using varchar.

A(Answer):

If i use unique id then i can select date from datepicker for first field only.

A(Answer):

can you please post the codes you made?

A(Answer):

<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$(".datepicker").datepicker()
});
</script>
</head>
<body>
<?php
error_reporting(E_PARSE);
$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from=date('y-m-d',strtotime($_POST['date_from']));
$to=date('y-m-d',strtotime($_POST['date_to']));
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {
foreach($status as $k=>$s){
$ins = "insert into driver_status(driver_name,status,date_from,date_to) VALUES
('".$driver_name[$k]."','$s','$from','$to')";
$quer=mysqli_query($conn,$ins);
}
if($quer){
echo "Updated";
}else{
echo"Unable to update.".mysqli_error($conn);
}
}
$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr><td>".$row['Driver_name'] ."<input type=\"hidden\" name=\"driver_name[]\" value=\"".$row['Driver_name']."\"/></td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
echo "<td><select name=\"status[]\">";
while($row1=mysqli_fetch_assoc($query1)){
echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."</option>";
}
echo "</select></td>";
echo "<td>".'<input type="text" name="date_from" class="datepicker"/>'."</td>";
echo "<td>".'<input type="text" name="date_to" class="datepicker"/>'."</td>";
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
</body>
</html>

A(Answer):

<input type="text" name="date_from_'.$id.'" class="datepicker" id="uniqueid_'.$id.'">

try this one, your name also must be unique.

A(Answer):

I have used it but it is not working.How to use name in $_POST[].

A(Answer):

oh, i see it now.
you must put your


$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from=date('y-m-d',strtotime($_POST['date_from']));
$to=date('y-m-d',strtotime($_POST['date_to']));
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());

inside of your

if(isset($_POST['sub'])) {
}

so it must be like this.


if(isset($_POST['sub'])) {
$status = $_POST['status'];
$driver_name= $_POST['driver_name'];
$from=date('y-m-d',strtotime($_POST['date_from']));
$to=date('y-m-d',strtotime($_POST['date_to']));
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());
foreach($status as $k=>$s){
$ins = "insert into driver_status(driver_name,status,date_from,date_to) VALUES
('".$driver_name[$k]."','$s','$from','$to')";
$quer=mysqli_query($conn,$ins);
}
if($quer){
echo "Updated";
}else{
echo"Unable to update.".mysqli_error($conn);
}
}

A(Answer):

This is not working.

A(Answer):

You only ever reference one value of the datepicker. Look at how you’re doing driver name, there you’re referencing the multiple indexes in the driver name array. But for your dates, you don’t do that, you only reference one value.

A(Answer):

notes:
mysqli_connect() never returns false
– dates should never be saved as text type (VARCHAR) but as date type (DATE) so SQL can work with it.

A(Answer):

Can u explain Rabbit?

A(Answer):

Compare how you’re retrieving the value of those two variables

A(Answer):

But it is not working when i save date as array.

A(Answer):

I know. And it’s because you’re doing it one way for driver_name and a different way for the date variables. Look at your code and look at how you’re pulling those 2 values. They’re different.

A(Answer):

It is not working when i use it like driver_name.

A(Answer):

Please post the new code

A(Answer):

<!DOCTYPE html>
<html>
<head>
<link href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/themes/base/jquery-ui.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<script>
$(document).ready(function() {
$(".datepicker").datepicker({
changeMonth:true})
});
</script>
</head>
<body>
<?php
@$status =$_POST['status'];
@$driver_name= $_POST['driver_name'];
@$from=date('y-m-d',strtotime($_POST['date_from']));
@$to=date('y-m-d',strtotime($_POST['date_to']));
$conn = mysqli_connect('localhost', 'root', '', 'punbus') or
die("Database not connected" . mysqli_error());
if(isset($_POST['sub'])) {
foreach($status as $k=>$v){
$ins = "insert into driver_status(driver_name,status,date_from,date_to) VALUES
('".$driver_name[$k]."','$v','".$from[$K]."','".$to[$K]."')";
$quer=mysqli_query($conn,$ins);
}
if($quer){
echo "Updated";
}else{
echo"Unable to update.".mysqli_error($conn);
}
}
$sel = 'select Driver_name from driver_master';
$query = mysqli_query($conn, $sel);
echo "<form action='driver_status.php' method='post'>";
echo "<table cellpadding=5>";
echo "<tr>";
echo "<th>Driver Name</th>";
echo "<th>Status</th>";
echo "<th>From</th>";
echo "<th>To</th>";
echo "</tr>";
while($row=mysqli_fetch_assoc($query)){
echo "<tr><td>".$row['Driver_name'] ."<input type=\"hidden\" name=\"driver_name[]\" value=\"".$row['Driver_name']."\"/></td>";
$sel1='select d_status from status';
$query1=mysqli_query($conn,$sel1);
echo "<td><select name=\"status[]\">";
while($row1=mysqli_fetch_assoc($query1)){
echo "<option value=\"".$row1['d_status']."\">".$row1['d_status']."</option>";
}
echo "</select></td>";
echo "<td>".'<input type="text" name="date_from[]" class="datepicker"/>'."</td>";
echo "<td>".'<input type="text" name="date_to[]" class="datepicker"/>'."</td>";
echo "</tr>";
}
echo "</table>";
echo '<input type="submit" name="sub" value="Update"/>';
echo "</form>";
?>
</body>
</html>

This is the new code.

A(Answer):

strtotime() expects a string, not an array.

A(Answer):

When i use string it is also not working. Then how to do it.

A(Answer):

just like you do it with $driver_name.

A(Answer):

But i m fetching $driver_name as array from database.

A(Answer):

$driver_name= $_POST['driver_name'];

I never knew that POST data are coming from the database …

A(Answer):

I mgetting data from one table & saving it to another.

A(Answer):

but you do that via a form. and where the form gets its data from does not matter for the submitted form.