2017-08-12 62 views
1

我卡住了現在作爲我的更新和插入查詢不工作,選擇查詢工作非常好。更新並插入查詢不工作從php

這裏是我下面的代碼...

<?php 
    if(isset($_POST['save'])){ 
     $exam_type=$_POST['exam_type']; 
     $exam_from=$_POST['exam_from']; 
     $exam_to=$_POST['exam_to']; 
     $total_points=$_POST['total_points']; 
     $passing_grade=$_POST['passing_grade']; 
     $time_limit=$_POST['hrs']*360 + $_POST['min']*60; 
     $cid=$_GET['cid']; 

     $sql = $con->prepare("UPDATE new_oes.exam" 
     . " SET exam_type=?,exam_from =?, exam_to=?," 
     . "modified_by=?,passing_score=?, time_limit=?,passing_grade=? " 
     . "WHERE exam_type = ? AND cid =? "); 

     $sql->execute(array($exam_type, $exam_from, $exam_to, $username, 
     $total_points, $time_limit, $passing_grade, $exam_type,$cid)) or die(mysqli_error($con)); 

     // echo $result1; 
     // $this->query($sql); 
     if(mysqli_affected_rows($con)>0){ 
      echo "<script type='text/javascript'>alert('Exam Updated!!!')</script>"; 
      exit; 
     } else{ 
      echo "An error occurred, try again later!!!"; 
     } 
    } 
?> 

請與錯誤幫幫我!

注:查詢從phpMyAdmin的工作

+0

是這樣的mysqli或PDO? – RamRaider

+1

$ username在哪裏設置? –

+0

@NigelRen $用戶名來自會話,在此代碼之前的某處設置。建議將其設置到if語句中? – akinlex

回答

1

它似乎沒有定義$username,你應該定義$username

$username = "SOME_ONE"; // maybe you saved it in $_SESSION or its in $_POST 
0

通常你會想從試圖prepare之前的sql語句檢查返回值然後嘗試執行查詢。我不能肯定看執行調用,因爲它分配一個數組作爲在PDO更典型的做法的說法 - 我想你會想這樣在執行前的參數佔位符綁定:

<?php 
    try{ 
     if(isset( 
      $_POST['save'], 
      $_POST['exam_type'], 
      $_POST['exam_from'], 
      $_POST['exam_to'], 
      $_POST['total_points'], 
      $_POST['passing_grade'], 
      $_POST['hrs'], 
      $_GET['cid'], 
      $_GET['username']) 
     ){ 

      $exam_type=$_POST['exam_type']; 
      $exam_from=$_POST['exam_from']; 
      $exam_to=$_POST['exam_to']; 
      $total_points=$_POST['total_points']; 
      $passing_grade=$_POST['passing_grade']; 
      $time_limit=$_POST['hrs']*360 + $_POST['min']*60; 
      $cid=$_GET['cid']; 
      $username=$_GET['username']; 

      $stmt = $con->prepare("update `new_oes`.`exam` 
       set `exam_type`=?, `exam_from` =?, `exam_to`=?, `modified_by`=?, `passing_score`=?, `time_limit`=?, `passing_grade`=? 
       where `exam_type` = ? and `cid` =? "); 

      if($stmt){ 
       /* 
        Assumed to be that all parameters are strings apart from $cid which I took to be an integer 
        Change each letter in first parameter of `bind_param` accoring to whether the input variable is 
        either string or numeric. 

        s=string 
        i=integer 
       */ 

       $stmt->bind_param('ssssssssi', $exam_type, $exam_from, $exam_to, $username, $total_points, $time_limit, $passing_grade, $exam_type, $cid); 
       $stmt->execute(); 

       if($stmt->affected_rows > 0){ 
        exit("<script>alert('Exam Updated!!!')</script>"); 
       } else{ 
        throw new Exception('An error occurred, try again later!!!'); 
       }    

      } else { 
       throw new Exception('Failed to prepare SQL statement!'); 
      } 
     } else { 
      throw new Exception('Required variables are not yet set'); 
     } 
    }catch(Exception $e){ 
     echo $e->getMessage(); 
    } 
?> 
+0

非常感謝!但它似乎並沒有更新。 – akinlex

+0

嗖!它終於奏效了!我的html表單有問題。再次感謝 – akinlex