PHP没有插入数据库SQL SERVER 2008

我有这个表单,我想用它来捕获数据并插入到数据库中:

<form actoin="request-new-price.php" method="post" id="demo-form2" data-parsley-validate>
<div>
    <label for="salesRep">Sales Rep:</label>
    <div>
        <input type="text" name="salesRep" id="salesRep" required="required" value="<?php echo $user['userName']; ?>">
    </div>
</div>
<div>
    <label for="CardName">Customer Name</label>
    <div>
        <input type="text" id="CardName" name="CardName" required="required" value="<?php echo $selectedCustomerName ?>">
    </div>
</div>
<div>
    <label for="CardCode">Customer Code</label>
    <div>
        <input type="text" id="CardCode" name="CardCode" required="required" value="<?php echo $selectedCustomerID ?>">
    </div>
</div>
<div>
    <label for="ItemName">Product Name</label>
    <div>
        <input type="text" id="ItemName" name="ItemName" required="required" value="<?php echo $selectedProductName ?>">
    </div>
</div>
<div>
    <label for="ItemCode">Product Code</label>
    <div>
        <input type="text" id="ItemCode" name="ItemCode" required="required" value="<?php echo $selectedProductCode ?>">
    </div>
</div>
<div>
    <label for="Price">Current Price</label>
    <div>
        <input type="text" id="Price" name="Price" required="required" value="£<?php echo $selectedProductPrice ?>">
    </div>
</div>
<div>
    <label for="requestedPrice">Requested Price</label>
    <div>
        <input type="text" id="requestedPrice" name="requestedPrice" required="required" value="£">
    </div>
</div>
<div>
    <div>
        <a href="specialprice.php?custCode=<?php echo $customer['CardCode'];?>&custName=<?php echo $customer['CardName'];?>">Cancel</a>
        <button type="submit" id="submit" name="submit" value="1">Submit</button>
    </div>
</div>
</form>

这是我的SQL / PHP:

<?php 

if(isset($_POST['submit'])){
    print_r($_POST);

    $query = prepare("INSERT INTO PriceRequests (salesRep, CardName, CardCode, ItemName, ItemCode, Price, requestedPrice) 
                VALUES (:salesRep, :cardName, :cardCode, :itemName, itemCode, :itemPrice, :newPrice)
            ");
            $insertSql = sqlsrv_query($sapconn, $query);

    $insertSql->bindParam(":salesRep",$salesRep);
    $insertSql->bindParam(":cardName",$cardName);
    $insertSql->bindParam(":cardCode",$cardCode);
    $insertSql->bindParam(":itemName",$itemName);
    $insertSql->bindParam(":itemCode",$itemCode);
    $insertSql->bindParam(":itemPrice",$itemPrice);
    $insertSql->bindParam(":newPrice",$newPrice);

    $salesRep = trim($_POST['salesRep']);
    $cardName = trim($_POST['CardName']);
    $cardCode = trim($_POST['CardCode']);
    $itemName = trim($_POST['ItemName']);
    $itemCode = trim($_POST['ItemCode']);
    $itemPrice = trim($_POST['Price']);
    $newPrice = trim($_POST['requestedPrice']);


    $insertSql->execute();   
    return $insertSql;
}

?>

但是数据没有插入到数据库中我对PHP来说相当新,这是我第一次尝试写回数据库,所以我可能会遗漏一些简单的东西,或者它可能完全错误.

无论哪种方式,所有帮助表示赞赏.

编辑:

我的PHP现在是这样的:

if(isset($_POST['submit'])){
   //print_r($_POST);

    $query = "INSERT INTO PriceRequests (salesRep, CardName, CardCode, ItemName, ItemCode, Price, requestedPrice) 
                                        VALUES (:salesRep, :cardName, :cardCode, :itemName, :itemCode, :itemPrice, :newPrice)
                    ";
    $stmt = $sapconn->prepare($query);

    $salesRep = (isset($_POST['salesRep']) && !empty($_POST['salesRep']))?$_POST['salesRep'] : NULL;
    $cardName = (isset($_POST['CardName']) && !empty($_POST['CardName']))?$_POST['CardName'] : NULL;
    $cardCode = (isset($_POST['CardCode']) && !empty($_POST['CardCode']))?$_POST['CardCode'] : NULL;
    $itemName = (isset($_POST['ItemName']) && !empty($_POST['ItemName']))?$_POST['ItemName'] : NULL;
    $itemCode = (isset($_POST['ItemCode']) && !empty($_POST['ItemCode']))?$_POST['ItemCode'] : NULL;
    $itemPrice = (isset($_POST['Price']) && !empty($_POST['Price']))?$_POST['Price'] : NULL;
    $newPrice = (isset($_POST['requestedPrice']) && !empty($_POST['requestedPrice']))?$_POST['requestedPrice'] : NULL;

    $stmt->bindValue(':salesRep', $salesRep, PDO::PARAM_STR);
    $stmt->bindValue(':cardName', $cardName, PDO::PARAM_STR);
    $stmt->bindValue(':cardCode', $cardCode, PDO::PARAM_STR);
    $stmt->bindValue(':itemName', $itemName, PDO::PARAM_STR);
    $stmt->bindValue(':itemCode', $itemCode, PDO::PARAM_STR);
    $stmt->bindValue(':itemPrice', $itemPrice, PDO::PARAM_STR);
    $stmt->bindValue(':newPrice', $newPrice, PDO::PARAM_STR);

    $stmt->execute();   
    return $stmt;
    }

但我仍然没有输入到我的数据库,我收到以下错误:
PHP致命错误:未捕获错误:在资源上调用成员函数prepare()

数据库连接:

<?php 
$serverName = "serverName";
$connectionInfo = array( "Database"=>"database_name", "UID"=>"user_Id", "PWD"=>"Password", "ReturnDatesAsStrings"=>true);
$sapconn = sqlsrv_connect( $serverName, $connectionInfo);
?>

最佳答案 PHP代码中还有一个拼写错误:

$query = prepare("INSERT INTO PriceRequests (salesRep, CardName, CardCode, ItemName, ItemCode, Price, requestedPrice) 
                VALUES (:salesRep, :cardName, :cardCode, :itemName, itemCode, :itemPrice, :newPrice)
            ");

占位符itemCode没有后缀“:”.
检查并尝试.
谢谢.

更新:

我试过你在问题中写的东西.在分配参数之前,您已尝试将参数绑定到占位符.

当我试图这样做时,我得到了例外.我认为这可能是数据未插入的原因.

我建议你按以下方式编写代码:

PHP代码:

<?php 

if(isset($_POST['submit'])){
    print_r($_POST); //Unnecessary, you can remove it

    $query = prepare("INSERT INTO PriceRequests (salesRep, CardName, CardCode, ItemName, ItemCode, Price, requestedPrice) 
                VALUES (:salesRep, :cardName, :cardCode, :itemName, :itemCode, :itemPrice, :newPrice)
            ");
            $insertSql = sqlsrv_query($sapconn, $query);

    $salesRep = trim($_POST['salesRep']);
    $cardName = trim($_POST['CardName']);
    $cardCode = trim($_POST['CardCode']);
    $itemName = trim($_POST['ItemName']);
    $itemCode = trim($_POST['ItemCode']);
    $itemPrice = trim($_POST['Price']);
    $newPrice = trim($_POST['requestedPrice']);


    $insertSql->bindParam(":salesRep",$salesRep);
    $insertSql->bindParam(":cardName",$cardName);
    $insertSql->bindParam(":cardCode",$cardCode);
    $insertSql->bindParam(":itemName",$itemName);
    $insertSql->bindParam(":itemCode",$itemCode);
    $insertSql->bindParam(":itemPrice",$itemPrice);
    $insertSql->bindParam(":newPrice",$newPrice);

    $insertSql->execute();   
    return $insertSql;
}

?>

我建议一些改变:

1.由于此处使用PDO,请使用变量来获取数据库连接(假设其​​为$db_conn).

代替

$insertSql = sqlsrv_query($sapconn,$query);

使用

$db_conn = new PDO(< connection-string>,< user-name>,< password>);

$stmt = $db_conn-> prepare($query)

然后通过以下方式绑定值:

$stmt-> bindValue(< placeholder>,< variable_vlaue>,< value_type>);

例如:$stmt-> bindValue(:itemName,$itemName,PDO :: PARAM_STR);

然后执行执行:

$stmt->执行();

2.如果您对数据进行一些验证,将会有所帮助:

通过验证将POST的值分配给变量

例如:

$itemName =(isset($_ POST [‘ItemName’])&&!empty($_ POST [‘ItemName’]))?$_ POST [‘ItemName’]:NULL;

这里,当使用’NULL’执行insert查询时,它将引发异常.

注: :应该使用try-catch块.

我认为现在应该可行了.

请随时告诉它是否不起作用,我会再次检查.

点赞