Question

Here is my code, if I check PHP error log there are no errors, as far as I can tell the database is connected to and the data is being processed however it is not showing up in the DB table 'calendar'?

here is config.php:

<?php

global $db;
global $productid;

$db = mysqli_connect("localhost", "********", "********", "database");

if (mysqli_connect_errno($db)) {
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// checks if user is logged in for admin panel
function isLoggedin ($db)
{
    if (isset($_SESSION['login'])) {

        $query = mysqli_query($db, "SELECT ID, username FROM sr_users WHERE ID='".mysqli_real_escape_string($db, $_SESSION['login_userId'])."'") or die(mysql_error());
        $row = mysqli_fetch_array($query, MYSQLI_ASSOC);

        if ($_SESSION['login_hash'] == hash('sha512', $row['username'] . $row['ID'])) {
            return true;
        } else {
            session_destroy();
            header("location: login.html");
        }
    } else {
      session_destroy();
      header("location: login.html");
    }
}

if (isset($_GET['productid'])) {
    if (!empty($_GET['productid'])) {
         $productid = $_GET['productid'];  
    } else {
       $productid = '';  
    }
} else {
    $productid = '';
}
?>

Here is date picker (using jquery-ui) to insert date into calendar db:

<?php 

session_start();

include_once("./config.php");

isLoggedin ($db);

?>

<html lang="en">
<head>
  <meta charset="utf-8">
  <title>jQuery UI Datepicker - Default functionality</title>
  <link rel="stylesheet" href="//code.jquery.com/ui/1.10.4/themes/smoothness/jquery-ui.css">
  <script src="//code.jquery.com/jquery-1.10.2.js"></script>
  <script src="//code.jquery.com/ui/1.10.4/jquery-ui.js"></script>
  <script>
  $(function() {
    $( "#datepicker" ).datepicker({dateFormat: 'yy-mm-dd' });
  });
  </script>
</head>
<body>

<?php

    $form = "

        <form action='' method='post' id='RegisterForm' autocomplete='off'>

            <table>

                <tr>

                    <td><p>Date: <input type='text' name='datepicker' id='datepicker'></p></td>

                </tr>

                <tr>

                    <td><input type='submit' name='submitbtn' value='Register'></td>

                </tr>

            </table>

        </form>


    ";

    if ($_POST['submitbtn'])

    {

        $datepicker = strip_tags($_POST['datepicker']);

        if ($datepicker) 
        {

            mysqli_query($db, "INSERT INTO calendar (date)
            VALUES ('$datepicker')");

            echo "<h2>Done!!!</h2>";

        }
        else

            echo "Please enter a valid date";

    }

    else

        echo "$form";

?>


</body>
</html>

I am using the database user from my star reviews (http://codecanyon.net/item/starreviews-ajax-jquery-rating-and-review-form/6584956) adddon, and I have added an extra table for an events calendar (http://www.phpjabbers.com/free-availability-calendar-script/) called 'calendar' and I need to insert the data in? Any ideas?

Was it helpful?

Solution

The code should be:

    if ($datepicker) 
    {

        mysqli_query($db, "INSERT INTO calendar (date)
        VALUES ('" . mysqli_real_escape_string($db, $datepicker) . "')") or die(mysqli_error($db));

You left out the argument that specifies the database connection to use. This should have generated an error something like mysqli_query expects parameter 1 to be mysqli.

I've added escaping of the data, and an error report if the insert fails.

OTHER TIPS

Check your variables into POST request var_dump($_POST['submitbtn']); var_dump($_POST['datepicker']); if you don't see string Done!!!, that mean the block with if not true.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top