2014-03-07 82 views
-1

我知道在stackoverflow上有很多像這樣的問題,但沒有人銷售我的問題。向數據庫添加數據PHP

我正在處理一個事件日曆,我目前正在構建一個添加事件頁面。當頁面提交時,沒有顯示錯誤,但表單信息沒有上傳到數據庫,我不確定爲什麼我檢查了語法並且看不到任何錯誤。

* addes loginform.php和core.php請求顯示$ _SESSION ['user_id']。來自 代碼: loginform.php

<?php 
    $con = mysql_connect("localhost","pytsuemg_brodie","brodie"); 
    $db = mysql_select_db("pytsuemg_brodie"); 

    if (isset($_POST['username'])&&isset($_POST['password'])) { 
     $username = $_POST['username']; 
     $password = $_POST['password']; 
     $password_hash =md5($password); 

     if (!empty($username)&&!empty($password)) { 
      $query="SELECT `AccountID` FROM `Parent` WHERE `Username`='$username' AND `Password`='$password_hash'"; 

      if ($query_run = mysql_query($query)) { 
       $query_num_rows = mysql_num_rows($query_run); 

       if ($query_num_rows==0) { 
        echo 'Invalid username/password combination'; 
       } else 
       if ($query_num_rows==1) { 
        $user_id = mysql_result($query_run,0,'AccountID'); 
        $_SESSION['user_id']=$user_id; 

        header('Location: profilepage.php'); 
       } 

      } 

     } else { 
      echo 'You must supply a username and password.'; 
     } 

    } 

    ?> 
<form action="login.php" method="POST"> 
Username: <input type="text" name="username"> 
Password: <input type="password" name="password"> 
<input type="submit" value="Log In"> 
</form> 

core.php中

<?php 
    ob_start(); 
    session_start(); 
    $current_file = $_SERVER['SCRIPT_NAME']; 
    function loggedin() { 

     if (isset($_SESSION['user_id'])&&!empty($_SESSION['user_id'])) { 
      return true; 
     } else { 
      return false; 
     } 

    } 


    ?> 

addevent.php

require 'core.php'; 
    $con=mysqli_connect("localhost","pytsuemg_brodie","brodie","pytsuemg_brodie"); 
    if (isset($_POST['DateStart'])&&isset($_POST['DateEnd'])&&isset($_POST['TimeStart'])&&isset($_POST['TimeEnd'])&&isset($_POST['EventType'])&&isset($_POST['Description'])&&isset($_POST['Children'])&&isset($_POST['Location'])&&isset($_POST['MealsGiven'])){ 

    $DateStart = $_POST['DateStart']; 
    $DateEnd = $_POST['DateEnd']; 
    $TimeStart = $_POST['TimeStart']; 
    $TimeEnd = $_POST['TimeEnd']; 
    $EventType = $_POST['EventType']; 
    $Description = $_POST['Description']; 
    $Children = $_POST['Children']; 
    $Location = $_POST['Location']; 
    $MealsGiven = $_POST['MealsGiven']; 

    if (!empty($DateStart)&&!empty($DateEnd)&&!empty($TimeStart)&&!empty($TimeEnd)&&!empty($EventType)&&!empty($Description)&&!empty($Children)&&!empty($Location)&&!empty($MealsGiven)){ 



    $sql = "INSERT INTO Events (DateStart, DateEnd, TimeStart, TimeEnd, Location, EventType, MealsGiven, description,Children, AccountID) VALUES(
    '$DateStart','$DateEnd','$TimeStart','$TimeEnd','$Location','$EventType','$MealsGiven','$Description','$Children','".$_SESSION['user_id']."')"; 

    mysqli_query($con, $sql); 

    /* commit transaction */ 
    if (!mysqli_commit($con)) { 
     print("Transaction commit failed\n"); 
     exit(); 
    } 
    } else { 
        echo'All fields are required'; 
       } 
    } 
    /* close connection */ 

    mysqli_close($con); 
    ?> 
    <br> 

    <form action="addevent.php" method="post"> 
    <p> Date:<label for="from"> From</label> 
    <input type="text" id="from" name="DateStart"> 
    <label for="to">to</label> 
    <input type="text" id="to" name="DateEnd"></p> 
     <p>Start Time:<input type="text" name="TimeStart" /></p> 
     <p>End Time:<input type="text" name="TimeEnd" /></p> 
     <p>Event Type : 
     <select name="EventType"> 
    <option value="Change over">Changeover</option> 
    <option value="Parents Evening">Parents Evening</option> 
    <option value="After School Activity">After School Activity</option> 
    <option value="Weekend Activity">Weekend Activity</option> 
    <option value="Holiday">Holiday</option> 
     </select> </p> 

    <p>Description (max 100 Characters) <br><textarea rows="2" cols="50" maxlength="100" name="Description"></textarea></p> 
    <p>Children Involved: <br><textarea rows="1" cols="50" maxlength="32" name="children"></textarea></p> 
     <p>Location: <input type="text" name="Location" /></p> 
     <p>Meals Given: <select name="MealsGiven"> 
    <option value="Yes">Yes</option> 
    <option value="No">No</option> 
    </select></p> 



     <p><input type="submit" value="Submit"/></p> 
    </form> 
+0

我沒有看到對'$ _SESSION ['user_id']'的引用,也沒有在您的代碼中顯示'session_start();'。如果引用爲空,則整個查詢將失敗。請參考它或刪除它。 –

+0

它在core.php中。我在其他頁面中使用$ _SESSION ['user_id'],它工作正常 – user3393477

+0

'core.php'究竟是什麼? –

回答

0

name="children"$_POST['Children']是不一樣的。

您需要將其更改爲name="Children"

POST變量是區分大小寫的。

看看Brad's comment(非常重視)這是非常值得的努力。

This article是一個很好的閱讀。

我注意到您正在存儲密碼md5()(根據您的loginform.php文件),這被認爲是「太快」。這不再被認爲是安全的使用。考慮使用一些在密碼存儲技術的最新諸如PHP的password_hash()功能(如果在PHP 5.5)或crypt()

loginform.php文件使用mysql_*功能,而你的addevent.php使用mysqli_*功能。我建議您對所有文件使用相同的SQL函數,mysqli_*prepared statementsPDO