php
  • mysql
  • undefined
  • 2017-11-25 61 views 0 likes 
    0

    這個問題可能看起來類似,但請理解我的問題。在我的更新頁面我與$_GET方法混淆。在我的第一頁我給了一個鏈接,通過使用mrinsertid編輯表這樣

    的index.php與現有值顯示

    <?php 
    include_once("db.php"); 
    
        $result1 = mysqli_query($connect, "SELECT * FROM mrinsert WHERE userid='{$_SESSION['user_id']}'"); 
    ?> 
    <?php 
    
         while($res = mysqli_fetch_array($result1)) {   
          echo "<tr>"; 
          echo "<td>".$res['bugta']."</td>"; 
          echo "<td>".$res['releaseta']."</td>"; 
          echo "<td>".$res['ccota']."</td>";  
          echo "<td><a href=\"mredit.php?mrinsertid=$res[mrinsertid]\">Edit</a> | <a href=\"mrdelete.php?mrinsertid=$res[mrinsertid]\" onClick=\"return confirm('Are you sure you want to delete?')\">Delete</a></td>";   
         } 
         ?> 
    

    mredit.php頁。但是,當嘗試更新它在這個地方給錯誤的價值觀:

    <?php 
    //getting id from url 
    
    
    $mrinsertid = $_GET['mrinsertid']; //ERROR Undefined index 
    
    //selecting data associated with this particular id 
    $result = mysqli_query($connect, "SELECT * FROM mrinsert WHERE mrinsertid=$mrinsertid"); //WARNING in this line 
    
    while($res = mysqli_fetch_array($result)) 
    { 
        $releaseta=$res['releaseta']; 
        $keyta=$res['keyta']; 
        $programleadsta=$res['programleadsta']; 
        $ccota=$res['ccota']; 
        $nextmilestonesta=$res['nextmilestonesta']; 
        $bugta=$res['bugta']; 
        $risksta=$res['risksta']; 
        $summaryta=$res['summaryta']; 
    
        ?> 
    

    我的整個mredit.php

    <?php 
    // including the database connection file 
    include_once("db.php"); 
    
    if(isset($_POST['update'])) 
    {  
        $mrinsertid = mysqli_real_escape_string($connect,$_POST['mrinsertid']); 
        $releaseta= mysqli_real_escape_string($connect,$_POST['releaseta']);  
        $keyta= mysqli_real_escape_string($connect,$_POST['keyta']); 
        $programleadsta= mysqli_real_escape_string($connect,$_POST['programleadsta']); 
        $ccota= mysqli_real_escape_string($connect,$_POST['ccota']); 
        $nextmilestonesta= mysqli_real_escape_string($connect,$_POST['nextmilestonesta']);   
        $bugta= mysqli_real_escape_string($connect,$_POST['bugta']); 
        $risksta= mysqli_real_escape_string($connect,$_POST['risksta']); 
        $summaryta= mysqli_real_escape_string($connect,$_POST['summaryta']); 
    
    
    
    
    
    
    
        // checking empty fields 
        if(empty($releaseta) || empty($keyta) || empty($programleadsta)||empty($ccota) || empty($nextmilestonesta) || empty($bugta)|| empty($riskta) || empty($summaryta)) {    
         if(empty($releaseta)) { 
          echo "<font color='red'>release field is empty.</font><br/>"; 
         } 
    
         if(empty($keyta)) { 
          echo "<font color='red'>Akey field is empty.</font><br/>"; 
         } 
    
         if(empty($programleadsta)) { 
          echo "<font color='red'>program field is empty.</font><br/>"; 
         }   
         if(empty($ccota)) { 
          echo "<font color='red'>cco field is empty.</font><br/>"; 
         } 
    
         if(empty($nextmilestonesta)) { 
          echo "<font color='red'>nextmilestone field is empty.</font><br/>"; 
         } 
    
         if(empty($bugta)) { 
          echo "<font color='red'>bug field is empty.</font><br/>"; 
         } 
         if(empty($risksta)) { 
          echo "<font color='red'>risk field is empty.</font><br/>"; 
         } 
    
         if(empty($summaryta)) { 
          echo "<font color='red'>summary field is empty.</font><br/>"; 
         } 
        } else {  
         //updating the table 
         $result = mysqli_query($connect, "UPDATE mrinsert SET releaseta='$releaseta',keyta='$keyta',programleadsta='$programleadsta',ccota='$ccota',nextmilestonesta='$nextmilestonesta',bugta='$bugta',risksta='$risksta',summaryta='$summaryta' WHERE mrinsertid='$mrinsertid'"); 
    
         //redirectig to the display page. In our case, it is index.php 
         header("Location: index.php"); 
        } 
    } 
    
    ?> 
    <?php 
    //getting id from url 
    
    
    $mrinsertid = $_GET['mrinsertid']; 
    
    //selecting data associated with this particular id 
    $result = mysqli_query($connect, "SELECT * FROM mrinsert WHERE mrinsertid=$mrinsertid"); 
    
    while($res = mysqli_fetch_array($result)) 
    { 
        $releaseta=$res['releaseta']; 
        $keyta=$res['keyta']; 
        $programleadsta=$res['programleadsta']; 
        $ccota=$res['ccota']; 
        $nextmilestonesta=$res['nextmilestonesta']; 
        $bugta=$res['bugta']; 
        $risksta=$res['risksta']; 
        $summaryta=$res['summaryta']; 
    
        ?> 
    
    <html> 
    <head>  
        <title>Edit Data</title> 
    </head> 
    <meta charset="utf-8"> 
        <meta name="viewport" content="width=device-width, initial-scale=1"> 
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css"> 
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script> 
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> 
        <style> 
        .wrap { 
        position:relative; 
        width:50px; 
    } 
    .wrap button { 
        border:5px solid; 
        position:absolute; 
        top:50px; 
        border-color:black; 
        border-radius: 50%; 
        margin-left:20px; 
        display:block; 
        height: 50px; 
        width: 50px; 
        background-color:green; 
    
        } 
        </style> 
    <body> 
        <a href="index.php">Home</a> 
        <br/> 
    
        <form name="form1" method="post" action="mredit.php"> 
    
        <input type="text" name="mrinsertid" value=<?php echo $_GET['mrinsertid'];?> > 
        <input type="submit" name="update" value="update"> 
    
    <div id="MRtableDIV"> 
    <table border="2"> 
    <tr style="background-color:#80bfff"> 
    <th style="width:8%"><center>Release</center></th> 
    <th style="width:5%"><center>Status</center></th> 
    <th style="width:6%"><center>Key</center></th> 
    <th style="width:15%"><center>Program Leads</center></th> 
    <th style="width:6%"><center>CCO Dates</center></th> 
    <th style="width:6%"><center>Next Milestones</center></th> 
    <th style="width:10%"><center>Bug Projection</center></th> 
    <th style="width:14%"><center>Risks</center></th> 
    <th style="width:20%"><center>Summary</center></th> 
    <tr> 
    <tr> 
    <tr> 
        <td><textarea name="releaseta" id="ReleaseTA" style="width:100%;height:200px;resize:none;background-color:#e6f2ff""><?php echo $res['releaseta'];?></textarea></td> 
        <td> 
        <div class="wrap"> 
        <button id="button1" value = "button" style= "color:white" onclick="setColor('button1', '#101010')";></button> 
        </div> 
        <textarea name="" id="StatusTA" style="width:100%;height:200px;resize:none;background-color:#e6f2ff"></textarea> 
        </td> 
        <td><textarea name="keyta" id="KeyTA" style="width:100%;height:200px;resize:none;background-color:#e6f2ff"><?php echo $res['keyta'];?></textarea></td> 
        <td><textarea name="programleadsta" id="ProgramLeadsTA" style="width:100%;height:200px;;resize:none;background-color:#e6f2ff"><?php echo $res['programleadsta'];?></textarea></td> 
        <td><textarea name="ccota" id="CCOTA" style="width:100%;height:200px;resize:none;background-color:#e6f2ff"><?php echo $res['ccota'];?></textarea></td> 
        <td><textarea name="nextmilestonesta" id="NextMilestonesTA" style="width:100%;height:200px;resize:none;background-color:#e6f2ff"><?php echo $res['nextmilestonesta'];?></textarea></td> 
        <td><textarea name="bugta" id="BugTA" style="width:100%;height:200px;resize:none;background-color:#e6f2ff"><?php echo $res['bugta'];?></textarea></td> 
        <td><textarea name="risksta" id="RisksTA" style="width:100%;height:200px;resize:none;background-color:#e6f2ff"><?php echo $res['risksta'];?></textarea></td> 
        <td><textarea name="summaryta" id="SummaryTA" style="width:100%;height:200px;resize:none;background-color:#e6f2ff"><?php echo $res['summaryta'];?></textarea></td> 
        </tr> 
    </tr> 
    </table> 
    </div> 
        </form> 
    </body> 
    </html> 
    <?php 
    } 
    ?> 
    
    +1

    [PHP:「Notice:Undefined variable」,「Notice:Undefined index」和「Notice:Undefined offset」]的可能重複(https://stackoverflow.com/questions/4261133/php-notice- undefined-variable-notice-undefined-index-and-notice-undef) –

    +0

    您的腳本存在[SQL注入攻擊](https://stackoverflow.com/q/60174/5914775)的風險。看看[Little Bobby Tables]發生了什麼事(http://bobby-tables.com/)。即使[如果你正在逃避投入,它不安全!](https://stackoverflow.com/q/5741187/5914775)。改用[準備好的參數化語句](https://php.net/manual/en/mysqli.quickstart.prepared-statements.php)。 –

    +0

    也有XSS問題,並且'font'標籤在1980年代被棄用; p –

    回答

    0

    試試這個

    我只是刪除{ }直接".$_SESSION['user_id'];添加和查詢存儲在被稱爲$result1變量。

    $result1 = "SELECT * FROM mrinsert WHERE user_id=".$_SESSION['user_id']; 
        $result = mysqli_query($conn, $result1); 
    
        if (mysqli_num_rows($result) > 0) { 
         // output data of each row 
         while($res = mysqli_fetch_array($result)){ 
           echo "<tr>"; 
           echo "<td>".$res['bugta']."</td>"; 
           echo "<td>".$res['releaseta']."</td>"; 
           echo "<td>".$res['ccota']."</td>";  
           echo "<td><a href='mredit.php?mrinsertid=".$res['mrinsertid']."'>Edit</a></td>"; 
         } 
        } 
        else { 
         echo "0 results"; 
        } 
        mysqli_close($conn); 
    

    mredit.php

    echo $mrinsertid = $_GET['mrinsertid']; 
    
    0

    刪除動作部分從形式,它會正常工作。在之間,不要忘記在使用之前將值轉換爲整數。另外,儘可能使用prepare語句。

    +0

    錯誤已消失,但未更新和重定向。 @Pradeesh –

    0

    的問題是,在 「mredit.php」 第一個電話你使用的是GET請求,因爲你點擊了一個鏈接<a href>。在這種情況下,您的$_GET['mrinsertid']將被設置爲URL中設置的值。在第二次調用「myedit.php」時,您使用的表單中定義的POST請求的屬性爲method="post"。因此,該值不在$_GET[]中,而是在$_POST[]中。

    有幾種解決方案如何解決這一問題:

    1. 使用$_REQUEST而不是$_GET$_REQUEST array$_GET,$_POST$_COOKIE數組的合併,所以當值來自URL或來自HTML表單(通過POST請求發送)時,無關緊要。
    2. <form>標記中的method=屬性更改爲GET,將其作爲GET請求發送。但是,由於各種原因,通常不會這樣做,因爲HTML表單應該通過POST請求發送。
    3. mrinsertid值添加到<form>標記的action=屬性的URL中。這樣mrinsertid的值是$_GET,但表格數據仍然在$_POST
    相關問題