2016-04-26 17 views
-2

PHP/MySQLイベントカレンダーを作成しようとしています。しかし、それはまともに行かない。以下は私のコードです: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 ([email protected]_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コードを貼り付けました。コードを確認してデータを挿入するのを手伝ってください。

+0

**警告:** mysqliを使用する場合は、パラメータ化されたクエリと['bind_param'](http://php.net/manual/en/mysqli-stmt.bind- param.php)を使用してユーザーデータをクエリに追加します。 **重大な[SQLインジェクションのバグ](http://bobby-tables.com/)を作成したため、文字列の補間または連結を使用してこれを実行しないでください。 ** '$ _POST'や' $ _GET'データを直接クエリに入れないでください。誰かがあなたのミスを悪用しようとすると、非常に危険です。 – tadman

答えて

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

+0

ありがとうRobin .....私は接続を可決しましたが、依然としてデータを挿入できません。 –

+0

もう一度やり直せますか?私はMySQLの挿入クエリを変更しました。 –

関連する問題