无法将数据插入MySQL事件数据库

时间:2016-04-26 07:51:27

标签: php mysql

我正在尝试创建一个PHP / MySQL事件日历。但它并不是很正确。以下是我的代码:

// calender.php          

$servername = "localhost";
$username = "username";
$password = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

@mysqli_select_db("calen");
if (!@mysqli_select_db) {
    die("database connection failed".mysqli_connect_error());
}

/*
mysqli_connect($hostname,$username,$password) or die ($error);
mysqli_select_db($dbname) or die ($error);
*/
            ?>
<html>
    <head>
        <script>
        function lastmonth(month,year) {
            if (month == 1) {
                --year;
                month = 13;
            }
            --month
            var monthstring= ""+month+"";
            var monthlength = monthstring.length;
            if (monthlength <=1) {
                monthstring = "0" + monthstring;
            }
            document.location.href = "<?= $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
        }

        function nextmonth(month,year) {
            if(month == 12) {
                ++year;
                month = 0;
            }
            ++month
            var monthstring= ""+month+"";
            var monthlength = monthstring.length;
            if (monthlength <=1) {
                monthstring = "0" + monthstring;
            }
            document.location.href = "<?=$_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
        }
        </script>
    </head>
    <body>
        <?php
        if (isset($_GET['day'])) { //to pass variable
            $day = $_GET['day'];
        } else {
            $day = date("j");
        }
        if (isset($_GET['month'])) {
            $month=$_GET['month'];
        } else {
            $month = date("n");
        }
        if (isset($_GET['year'])) {
            $year=$_GET['year'];
        } else {
            $year = date("y");
        }

        $currentTimeStamp = strtotime("$year-$month-$day");
        $monthname = date("F", $currentTimeStamp);
        $numofdays = date("t", $currentTimeStamp);
        $counter = 0;
        ?>
        <?php
        if (isset($_GET['add'])) {
            $title =$_POST['txttitle'];
            $detail =$_POST['txtdetail'];
            $eventdate = $month."/".$day."/".$year;
            $insert = "INSERT into calendar(Title,Detail,EventDate,DateAdded) values ('".$title."','".$detail."','".$eventdate."',now())";
            $check = mysqli_query($conn,$insert);
            if ($check ) {
                echo "Event Added...";
            } else {
                echo "Failed....";
            }
        }
        ?>

        <table border='1'>
            <tr>
                <td><input style='width:50px;' type='button' value='<' name='previous' onclick="lastmonth(<?php echo $month.",".$year ?>)">  </td>
                <td colspan='5' align='center'> <?php echo $monthname.",".$year ?> </td>
                <td><input style='width:50px;' type='button' value='>' name='next' onclick="nextmonth(<?php echo $month.",".$year ?>)"></td>
            </tr>
            <tr>
                <td width='50px' align='center'>Sun</td>
                <td width='50px'align='center'>Mon</td>
                <td width='50px'align='center'>Tue</td>
                <td width='50px'align='center'>Wed</td>
                <td width='50px'align='center'>Thu</td>
                <td width='50px'align='center'>Fri</td>
                <td width='50px'align='center'>Sat</td>
            </tr>
            <?php
            echo "<tr>";

            for ($i=1;$i<$numofdays+1;$i++,$counter++) {
                $TimeStamp = strtotime("$year-$month-$i");
                if ($i == 1) {
                    $firstday = date("w", $TimeStamp); //which day 1 falls on
                    for ($j = 0; $j < $firstday; $j++, $counter++) {
                        echo "<td>&nbsp;</td>";
                    }
                }
                if ($counter % 7 == 0) {
                    echo "</tr><tr>";
                }
                $monthstring=$month;
                $monthlength=strlen($monthstring);
                $daystring=$i;
                $daylength=strlen($daystring);
                if ($monthlength<=1) {
                    $monthstring = "0".$monthstring;
                }

                if ($daylength<=1) {
                    $daystring="0".$daystring;
                }

                echo "<td align='center'> <a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true'>".$i."</a> </td>";
            }
            echo "</tr>";
            ?>
        </table>

        <?php
        if (isset($_GET['v'])) {
            echo "<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true&f=true'>Add Event </a>";
            if (isset($_GET['f'])) {
                include("event.php");
            }
                    }
            ?>

    </body>
</html>

event.php

    <form name='event' method='POST' action="<?php $_SERVER['PHP_SELF']; ?>?month=<?php echo $month;?>&day=<?php echo $day;?>&year=<?php echo $year; ?>&v=true&add=true">
    <table width='400px' border='0'>
    <tr>
    <td width='150px'>Title</td>
    <td width='250px'><input type='text' name='txttitle'</td>
    </tr>
    <tr>
    <td width='150px'>Detail</td>
    <td width='250px'><textarea name='txtdetail'></textarea></td>
    </tr>
    <tr>
    <td colspan='2' align='center'><input type='submit' name='btnadd' value='Add Event'></td>
    </tr>
    </table>
    </form>

SQL:

 CREATE TABLE `calendar` (
  `ID` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,

`Title` VARCHAR( 65 ) NOT NULL ,
`Detail` VARCHAR( 255 ) NOT NULL ,
`EventDate` VARCHAR( 10 ) NOT NULL ,
`DateAdded` DATE NOT NULL
);

我无法将数据插入数据库。我已经粘贴了calendar.php,event.php和SQL代码。请检查代码并帮助我插入数据。

1 个答案:

答案 0 :(得分:3)

您需要使用mysqli_select_db()添加连接变量。所以你的代码将是:

<?php
$servername = "localhost";
$username = "username";
$password = "";

// Create connection
$conn = mysqli_connect($servername, $username, $password);

// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

//@mysqli_select_db($conn, "calen");
if(!mysqli_select_db($conn, "calen")) {
    die ("database connection failed".mysqli_connect_error());
}
/*
mysqli_connect($hostname,$username,$password) or die ($error);
mysqli_select_db($dbname) or die ($error);
*/
?>
<html>
    <head>
        <script>
        function lastmonth(month,year)
        {
            if(month == 1)
                {
                    --year;
                    month = 13;
                }
                --month
                var monthstring= ""+month+"";
                var monthlength = monthstring.length;
                if(monthlength <=1)
                {
                    monthstring = "0" + monthstring;
                }
                document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
        }

        function nextmonth(month,year)
        {
            if (month == 12) {
                ++year;
                month = 0;
            }
            ++month
            var monthstring= ""+month+"";
            var monthlength = monthstring.length;
            if (monthlength <=1) {
                monthstring = "0" + monthstring;
            }
            document.location.href ="<?php $_SERVER['PHP_SELF'];?>?month="+monthstring+"&year="+year;
        }
        </script>
    </head>
    <body>
        <?php
        if (isset($_GET['day'])) { //to pass variable
            $day=$_GET['day'];
        } else {
            $day = date("j");
        }
        if (isset($_GET['month'])) {
            $month=$_GET['month'];
        } else {
            $month = date("n");
            if (isset($_GET['year'])) {
                $year=$_GET['year'];
            } else {
                $year = date("y");
            }
            $currentTimeStamp = strtotime("$year-$month-$day");
            $monthname = date("F", $currentTimeStamp);
            $numofdays = date("t", $currentTimeStamp);
            $counter = 0;
        ?>
        <?php
        if (isset($_GET['add'])) {
            $title =$_POST['txttitle'];
            $detail =$_POST['txtdetail'];
            $eventdate = $month."/".$day."/".$year;
            $insert = "INSERT INTO calendar (Title,Detail,EventDate,DateAdded) VALUES ('".$title."','".$detail."','".$eventdate."', NOW())";
            $check = mysqli_query($conn,$insert);
            if ($check) {
                echo "Event Added...";
            } else {
                echo "Failed....";
            }
        }
        ?>
        <table border='1'>
            <tr>
                <td> <input style='width:50px;' type='button' value='<' name='previous' onclick="lastmonth(<?php echo $month.",".$year ?>)">  </td>
                <td colspan='5' align='center'> <?php echo $monthname.",".$year ?> </td>
                <td><input style='width:50px;' type='button' value='>' name='next' onclick="nextmonth(<?php echo $month.",".$year ?>)"></td>
            </tr>
            <tr>
                <td width='50px' align='center'>Sun</td>
                <td width='50px'align='center'>Mon</td>
                <td width='50px'align='center'>Tue</td>
                <td width='50px'align='center'>Wed</td>
                <td width='50px'align='center'>Thu</td>
                <td width='50px'align='center'>Fri</td>
                <td width='50px'align='center'>Sat</td>
            </tr>
            <?php
            echo "<tr>";
            for ($i=1;$i<$numofdays+1;$i++,$counter++) {
                $TimeStamp = strtotime("$year-$month-$i");
                if ($i == 1) {
                    $firstday = date("w", $TimeStamp); //which day 1 falls on
                    for ($j = 0; $j < $firstday; $j++, $counter++) {
                        echo "<td>&nbsp;</td>";
                    }
                }
                if($counter % 7 == 0) {
                    echo "</tr><tr>";
                }
                $monthstring=$month;
                $monthlength=strlen($monthstring);
                $daystring=$i;
                $daylength=strlen($daystring);
                if ($monthlength<=1) {
                    $monthstring = "0".$monthstring;
                }
                if ($daylength<=1) {
                    $daystring="0".$daystring;
                }
                echo "<td align='center'> <a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true'>".$i."</a> </td>";
            }
            echo "</tr>";
            ?>
        </table>
        <?php if (isset($_GET['v'])) {
            echo "<a href='".$_SERVER['PHP_SELF']."?month=".$monthstring."&day=".$daystring."&year=".$year."&v=true&f=true'>Add Event </a>";
            if (isset($_GET['f'])) {
                include("event.php");
            }
        }
        ?>
    </body>
</html>

希望这适合你! 有关更多信息,请访问http://www.w3schools.com/php/func_mysqli_select_db.asp