douna6802 2014-05-08 22:55
浏览 33
已采纳

PHP插入工作但数据不进入

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?

  • 写回答

2条回答 默认 最新

  • douzhi7082 2014-05-08 23:05
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题