PHP检查多文本文本框并插入数据错误

时间:2015-12-13 11:52:51

标签: php html mysqli

<?php
              $carCompany = $mysqli->real_escape_string($_POST['carCompanyName']);
              $carName = $mysqli->real_escape_string($_POST['carName']);
              $carModel = $mysqli->real_escape_string($_POST['carModel']);
              $carColor = $mysqli->real_escape_string($_POST['carColor']);
              $carFuelType = $mysqli->real_escape_string($_POST['carFuelType']);
              $carPlate = $mysqli->real_escape_string($_POST['carPlate']);
              $carOwnerName = $mysqli->real_escape_string($_POST['carOwnerName']);
              $carDescription = $mysqli->real_escape_string($_POST['carDescription']);

              if ($_POST) {
                if (!isset($_POST['carCompanyName'])) || strlen($_POST['carCompanyName'])<1 {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }
                if (!isset($_POST['carName'])) || strlen($_POST['carName'])<1 {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }
                if (!isset($_POST['carModel'])) || strlen($_POST['carModel'])<1 {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }
                if (!isset($_POST['carColor'])) || strlen($_POST['carColor'])<1 {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }
                if (!isset($_POST['carPlate'])) || strlen($_POST['carPlate'])<1 {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }
                if (!isset($_POST['carOwnerName'])) || strlen($_POST['carOwnerName'])<1 {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }
                if (!isset($_POST['carOwnerName'])) || strlen($_POST['carOwnerName'])<1 {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }

                if(mysqli_query($mysqli, "INSERT INTO cars(car_plate_no, car_company_name, car_name, car_model, car_color, car_fuel_type, car_owner_name, car_description) VALUES ('$carPlate','$carCompany','$carName','$carModel','$carColor','$carFuelType','$carOwnerName','$carDescription')" )){
                    printf("%d Row Inserted.\n", mysqli_affected_rows($mysqli));
                  }

              }

              ?>

错误说: 解析错误:语法错误,意外&#39; ||&#39;第33行的C:\ xampp \ htdocs \ meccaniko \ work \ profile \ add-car.php中的(T_BOOLEAN_OR)

这段代码发生了什么,它说||这是意料之外的

帮我检查空文本框并运行上面的插入查询

2 个答案:

答案 0 :(得分:1)

你的)位置错误。

if (!isset($_POST['carCompanyName']) || strlen($_POST['carCompanyName'])<1) {

}

答案 1 :(得分:0)

用上面的代码替换此代码,如果条件允许的话,我已更正括号中的代码错误。

<?php
              $carCompany = $mysqli->real_escape_string($_POST['carCompanyName']);
              $carName = $mysqli->real_escape_string($_POST['carName']);
              $carModel = $mysqli->real_escape_string($_POST['carModel']);
              $carColor = $mysqli->real_escape_string($_POST['carColor']);
              $carFuelType = $mysqli->real_escape_string($_POST['carFuelType']);
              $carPlate = $mysqli->real_escape_string($_POST['carPlate']);
              $carOwnerName = $mysqli->real_escape_string($_POST['carOwnerName']);
              $carDescription = $mysqli->real_escape_string($_POST['carDescription']);

              if ($_POST) {
                if (!isset($_POST['carCompanyName']) || strlen($_POST['carCompanyName'])<1) {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }
                if (!isset($_POST['carName']) || strlen($_POST['carName'])<1) {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }
                if (!isset($_POST['carModel']) || strlen($_POST['carModel'])<1) {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }
                if (!isset($_POST['carColor']) || strlen($_POST['carColor'])<1) {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }
                if (!isset($_POST['carPlate']) || strlen($_POST['carPlate'])<1) {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }
                if (!isset($_POST['carOwnerName']) || strlen($_POST['carOwnerName'])<1) {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }
                if (!isset($_POST['carOwnerName']) || strlen($_POST['carOwnerName'])<1) {

                  die('<div class="alert alert-danger"><a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a><strong>Danger!</strong> Indicates a dangerous or potentially negative action.</div>');
                }

                if(mysqli_query($mysqli, "INSERT INTO cars(car_plate_no, car_company_name, car_name, car_model, car_color, car_fuel_type, car_owner_name, car_description) VALUES ('$carPlate','$carCompany','$carName','$carModel','$carColor','$carFuelType','$carOwnerName','$carDescription')" )){
                    printf("%d Row Inserted.\n", mysqli_affected_rows($mysqli));
                  }

              }

              ?>