2017-08-27 70 views
-2

我可以檢索數據,但不更新或插入,我的代碼看起來不錯,但仍然無法正常工作。更新/插入數據到sql數據庫不工作,沒有錯誤

我正在使用mysql_query。

插入查詢無法正常工作,沒有錯誤發現的PHP,但選擇工作正常。

這是我的PHP代碼:

if(isset($_POST['txtBrName'])){ 
    if(isset($_POST['hdn']) && $_POST['hdn'] == '0'){ 
    $sql_branch = "INSERT INTO `tblbranch`(`branch_name`, `b_email`, `b_contact_no`, `b_address`,`b_status`) VALUES ('$_POST[txtBrName]','$_POST[txtBrEmail]','$_POST[txtBrConNo]','$_POST[txtareaAddress]','$_POST[radioStatus]')"; 
    mysql_query($sql_branch,$link) or die(mysql_error($link)); 
    mysql_close($link); 
    $url = WEB_URL . 'branch/branchlist.php?m=add'; 
    header("Location: $url"); 

    } 
    else{ 

     //echo mysql_errno($link,mysql_query()) . ": " . mysql_error($link). "\n"; 

     $sql_branch = "UPDATE `tblbranch` SET `branch_name`='".$_POST['txtBrName']."',`b_email`='".$_POST['txtBrEmail']."',`b_contact_no`='".$_POST['txtBrConNo']."',`b_address`='".$_POST['txtareaAddress']."',`b_status` ='".$_POST['radioStatus']."' WHERE branch_id = '".$_GET['id']."'"; 
     mysql_query($sql_branch,$link); 
     mysql_close($link); 
     $url = WEB_URL . 'branch/branchlist.php?m=up'; 
     header("Location: $url"); 
    } 

這是HTML代碼:

<form onSubmit="return validateMe();" action="<?php echo $form_url; ?>" method="post" enctype="multipart/form-data"> 
    <div class="box-body"> 
     <div class="form-group"> 
     <label for="txtBrName">Branch Name :</label> 
     <input type="text" name="txtBrName" id="txtBrName" value="<?php echo $branch_name; ?>" class="form-control" /> 
     </div> 
     <div class="form-group"> 
     <label for="txtBrEmail">Email :</label> 
     <input type="text" name="txtBrEmail" id="txtBrEmail" value="<?php echo $b_email; ?>" class="form-control" /> 
     </div> 
     <div class="form-group"> 
     <label for="txtBrConNo">Contact No :</label> 
     <input type="text" name="txtBrConNo" value="<?php echo $b_contact_no; ?>" id="txtBrConNo" class="form-control" /> 
     </div> 
    <div class="form-group"> 
     <label for="txtareaAddress">Branch address :</label> 
     <input type="text" name="txtareaAddress" value="<?php echo $b_address; ?>" id="txtareaAddress" class="form-control" /> 
     </div> 
     <!--div class="form-group"> 
     <label for="txtStatus">Branch Status :</label> 
     <input type="text" name="txtStatus" value="" id="b_status" class="form-control" /> 
     </div--> 
     <div class="form-group"> 
     <label for="txtBranch_status">Branch Status :</label> 
     <!--input type="radio" name="txtBranch_status" value="" id="b_status" class="form-control" /></br--> 
      <input type="radio" name="radioStatus" value="Enable" id="radioStatus"> Enable 
      <input type="radio" name="radioStatus" value="Disable" id="radioStatus"> Disable 

     </div> 

<div class="form-group pull-right"> 
      <input type="submit" name="submit" class="btn btn-primary" value="<?php echo $button_text; ?>"/> 
      &nbsp; 
      <input type="reset" onClick="javascript:window.location.href='<?php echo WEB_URL; ?>branch/addbranch.php';" name="btnReset" id="btnReset" value="Reset" class="btn btn-primary"/> 
      </div> 
     </div> 
     <input type="hidden" name="hdnSpid" value="<?php echo $hval; ?>"/> 



    </form> 
+3

請問您可以使用stackoverflow指導行來重寫代碼以提高可讀性 – adeguk

+1

您對SQL注入開放,請比'不工作'更具體。 – chris85

+1

連接正在工作,但當我嘗試更新/插入我沒有得到任何數據庫 – Jacob

回答

0

嘗試將此添加到您的PHP代碼中查找錯誤:

if(!mysql_query("INSERT INTO `tblbranch`(`branch_name`, `b_email`, `b_contact_no`, `b_address`,`b_status`) VALUES ('$_POST[txtBrName]','$_POST[txtBrEmail]','$_POST[txtBrConNo]','$_POST[txtareaAddress]','$_POST[radioStatus]')"") 
{ 
echo mysql_errno() . ": " . mysql_error() ; 
} 
0

試試這個:

if(isset($_POST['txtBrName'])){ 
    if(isset($_POST['hdn']) && $_POST['hdn'] == '0'){ 
     $branch_name = $_POST['txtBrName']; 
     $b_email = $_POST['txtBrEmail']; 
     $b_contact_no = $_POST['txtBrConNo']; 
     $b_address = $_POST['txtareaAddress']; 
     $b_status = $_POST['radioStatus']; 
     $branch_id = $_GET['id']; 

     $sql_branch = "INSERT INTO tblbranch (branch_name, b_email, b_contact_no, b_address,b_status) VALUES ('$branch_name', '$b_email', '$b_contact_no', '$b_address', '$b_status')"; 
     mysql_query($sql_branch,$link) or die(mysql_error($link)); 
     mysql_close($link); 
     $url = WEB_URL . 'branch/branchlist.php?m=add'; 
     header("Location: $url"); 

    } 
    else{ 

     //echo mysql_errno($link,mysql_query()) . ": " . mysql_error($link). "\n"; 

     $sql_branch = "UPDATE tblbranch SET branch_name='$branch_name', b_email ='$b_email', b_contact_no ='$b_contact_no', b_address ='$b_address', b_status ='$b_status' WHERE branch_id = '$branch_id'"; 
     mysql_query($sql_branch,$link); 
     mysql_close($link); 
     $url = WEB_URL . 'branch/branchlist.php?m=up'; 
     header("Location: $url"); 
    } 
} 
相關問題