mysql null或usser输入

py49o6xq  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(253)

此代码用于将数据插入到我的表中。

$sku = $conn->real_escape_string($_REQUEST['sku']);
    $name = $conn->real_escape_string($_REQUEST['name']);
    $price =  $conn->real_escape_string($_REQUEST['price']);
    $type =  $conn->real_escape_string($_REQUEST['type']);
    $size =  $conn->real_escape_string($_REQUEST['size']);
    $bWeight =  $conn->real_escape_string($_REQUEST['bWeight']);
    $fHeight =  $conn->real_escape_string($_REQUEST['fHeight']);
    $fWeight =  $conn->real_escape_string($_REQUEST['fWeight']);
    $fLenght =  $conn->real_escape_string($_REQUEST['fLenght']);

    //Insert Into mainInfo
    $sql = "INSERT INTO mainInfo (sku, name, price, type) VALUES ('$sku', 
    '$name', '$price', '$type')";
    if($conn->query($sql) === TRUE){
      echo "Records inserted Successfully <br> ";
    } else {
      echo "Error couldnt insert records: " .$conn->error;
    }

    //Insert Into properties
    $sql = "INSERT INTO properties (size, bWeight, fHeight, fWeight, fLenght, 
    sku) VALUES ('$size', '$bWeight', '$fHeight', '$fWeight', '$fLenght', 
    '$sku')";
    if($conn->query($sql) === TRUE){
      echo "Records inserted Successfully <br> ";
    } else {
      echo "Error couldnt insert records: " .$conn->error;
    }

这是html代码,如果需要的话。

<form class="prodInf" method="post" action="pehape/apstrade.php" >

<div class="container-fluid">
  <div class="row">
    <div class="col-md-4">
        <label class="labelName" for="prodSKU"><b>SKU</b> </label>
        <input type="text" name="sku">
          <br>
        <label class ="labelName"for="prodName"><b>Name</b> </label>
        <input type="text" name="name">
          <br>
        <label class ="labelName"for="prodPrice"><b>Price</b> </label>
        <input type="text" name="price">
          <br>
        <label id="lastProdInf" class ="labelName"for="prodType">Type 
      Switcher</label>
        <select class="prodctType" name="type">
          <option value="1" selected="selected">DVD-Disc</option>
          <option value="2">Book</option>
          <option value="3">Furniture</option>
        </select>

     </div>
   </div>
 </div>

  <br>

  <!--type apraksts un ievade -->
 <div class="container-fluid">
  <div class="row">
    <div class="col-md-4">
      <div class="inputDisplay">

        <!--DvD logs -->
        <div class="inputDvd">

            <label class="typePar" for="size"><b>Size</b> </label>
            <input type="text" name="size">

          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed 
     gravida fringilla sem, at lobortis lorem pulvinar et.</p>
        </div>

        <!--book logs -->
        <div class="inputBook">

            <label class="typePar" for="bWeight"><b>Weight</b></label>
            <input type="text" name="bWeight">

          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed 
     gravida fringilla sem, at lobortis lorem pulvinar et.</p>
        </div>

        <!--furniture logs -->
        <div class="inputFurniture">

            <label class="typePar" for="fHeight"><b>Height</b> </label>
            <input type="text" name="fHeight">
              <br>
            <label class="typePar" for="fWeight"><b>Weight</b> </label>
            <input type="text" name="fWeight">
              <br>
            <label class="typePar" for="fLenght"><b>Lenght</b></label>
             <input type="text" name="fLenght">

          <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed 
    gravida fringilla sem, at lobortis lorem pulvinar et.</p>
        </div>

        </div>
      </div>
    </div>
  </div>

  <input type="submit" name="save" value="submit">
</form>

如果用户没有在某些字段中输入数据,我怎么能让它们为空,现在这个代码告诉我。错误couldnt insert records:在我仅以$size输入数据之后,第1行“bweight”列的整数值“”不正确

ve7v8dk2

ve7v8dk21#

首先,确保数据库模式中允许空值。那么,你不应该使用 real_escape_string() 因为这个函数总是返回一个字符串(见文档)。
你可以用 filter_var() 以确保提供的值是整数或强制强制转换为int $value = (int) $userInput .
还请注意,最好在任何地方使用准备好的语句,而不是 real_escape_string

相关问题