【问题标题】:UPDATE/INSERT data to sql database not working and no errorsUPDATE/INSERT 数据到 sql 数据库不起作用并且没有错误
【发布时间】:2017-08-28 03:13:55
【问题描述】:

我可以检索数据但不能更新或插入,我的代码看起来不错但仍然无法正常工作。

我正在使用 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>

【问题讨论】:

  • 能否请您使用stackoverflow指南重写代码以提高可读性
  • 您对 SQL 注入持开放态度,请比doesn't work 更具体。
  • 连接正常,但是当我尝试更新/插入时,数据库上什么也没有

标签: php html sql


【解决方案1】:

尝试将此添加到您的 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() ;
}

【讨论】:

    【解决方案2】:

    试试这个:

    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");  
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2013-12-18
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-27
      • 2017-03-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多