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条)

报告相同问题?

悬赏问题

  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝
  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的