2017-08-27 106 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"); 
    } 
}