jhkhhkhkkhhkhk jhkhhkhkkhhkhk - 5 months ago 17
SQL Question

add day with multiple times mysql and php

I have been working on adding a timeline schedule into my database. my database consists of A(day) B(hour)

$date1
will equal the next Saturday and
$date2
will equal the Sundays. I have been trying to add at separate times. my problem is I need it to add in the day and the time then the same day and a different time. example day=2016-07-09 hour=9am day=2016-07-09 hour=10am. every line in a pattern. at the moment it only adds Saturday and Sunday and adds the last hour.

$sql = "INSERT INTO availability (day,hour)
VALUES ('$date1','9am');";
$sql = "INSERT INTO availability (day,hour)
VALUES ('$date1','10am');";
$sql = "INSERT INTO availability (day,hour)
VALUES ('$date1','11am');";


for the example above the date 2016-07-09 and it will only add 1 row in with the hour as 11am.

the full code is below.

$date1 = date('Y-m-d', strtotime('next saturday', strtotime($givenDate)));
$date2 = date('Y-m-d', strtotime('Next sunday', strtotime($givenDate)));

$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "grass";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}

$sql = "INSERT INTO availability (day,hour)
VALUES ('$date1','9am');";
$sql = "INSERT INTO availability (day,hour)
VALUES ('$date1','10am');";
$sql = "INSERT INTO availability (day,hour)
VALUES ('$date1','11am');";

$sql .= "INSERT INTO availability (day,hour)
VALUES ('$date2','');";


if ($conn->multi_query($sql) === TRUE) {
echo "New records created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
}


Thank you for taking your time for me, if you have any questions please raise them below.

Answer

You missed the . before $sql.. This must do it

<?php
$date1 = date('Y-m-d', strtotime('next saturday', strtotime($givenDate)));
$date2 = date('Y-m-d', strtotime('Next sunday', strtotime($givenDate)));

$servername = "localhost";
$username = "Admin_Jaiho";
$password = "Pradeepan";
$dbname = "Jaiho_Deals";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO availability (day,hour)
VALUES ('$date1','9am');";
$sql .= "INSERT INTO availability (day,hour)
VALUES ('$date1','10am');";
$sql .= "INSERT INTO availability (day,hour)
VALUES ('$date1','11am');";

$sql .= "INSERT INTO availability (day,hour)
VALUES ('$date2','');";


if ($conn->multi_query($sql) === TRUE) {
    echo "New records created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

?>