PHP值没有进入MySQL表

时间:2017-02-04 21:24:33

标签: php mysql forms

我一直在尝试通过HTML表单和PHPMyAdmin将值插入到SQL表中。它昨天工作正常,直到我添加了一些新表格。我已多次查看此代码,但无法找到错误。

http://aswanson.net/givDapps/City_Upload.php

<?php

$con=mysqli_connect("xxxx","xxxx","xxxx");
mysqli_select_db($con, "andrew_givDapps");


if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
} 
echo "Connected successfully";

$Description=$_POST['Description'];
$Type=$_POST['Type'];
$Name = $_POST['Name'];
$StateName = $_POST['StateName'];
$CountyName = $_POST['CountyName'];
$ZipCode = $_POST['ZipCode'];
$Address = $_POST['Address'];
$Population=$_POST['Population'];
$MediumIncome=$_POST['MediumIncome'];
$ProfitNumber = $_POST['ProfitNumber'];
$Average = $_POST['Average'];
$Homeless = $_POST['Homeless'];
$Shelters = $_POST['Shelters'];
$Adults = $_POST['Adults'];

$submit=$_POST['submit'];


if ($_SERVER["REQUEST_METHOD"] == "POST") {
  $Description= test_input($_POST["Description"]);
  $Type = test_input($_POST["Type"]);
  $Name = test_input($_POST["Name"]);
  $StateName = test_input($_POST["StateName"]);
  $CountyName = test_input($_POST["CountyName"]);
  $ZipCode = test_input($_POST["ZipCode"]);
  $Address = test_input($_POST["Address"]);
  $Population=test_input($_POST["Population"]);
  $MediumIncome=test_input($_POST["MediumIncome"]);
  $ProfitNumber = test_input($_POST["ProfitNumber"]);
  $Average = test_input($_POST["Average"]);
  $Homeless = test_input($_POST["Homeless"]);
  $Shelters = test_input($_POST["Shelters"]);
  $Adults = test_input($_POST["Adults"]);
}

function test_input($data) {
  $data = trim($data);
  $data = stripslashes($data);
  $data = htmlspecialchars($data);
  return $data;
}

 $DescriptionErr = $TypeErr = $NameErr = $StateNameErr = $CountyNameErr = $ZipCodeErr = $AddressErr = $PopulationErr = $MediumIncomeErr= $ProfitNumberErr = $AverageErr = $HomelessErr = $SheltersErr= $AdultsErr = "";


  if (empty($_POST["Description"])) {
$DescriptionErr = "*Description is required";
  } else {
    $Description= test_input($_POST["Description"]);
  }

  if (empty($_POST["Type"])) {
$TypeErr = "*Type is required";
  } else {
    $Type = test_input($_POST["Type"]);
  }

  if (empty($_POST["Name"])) {
    $NameErr = "*Name is required";
  } else {
    $Name  = test_input($_POST["Name"]);
  }

  if (empty($_POST["StateName "])) {
    $StateNameErr = "*State Name  is required";
  } else {
    $StateName  = test_input($_POST["StateName"]);
  }

  if (empty($_POST["CountyName"])) {
    $CountyNameErr = "*County Name is required";
  } else {
    $CountyName = test_input($_POST["CountyName"]);
  }

  if (empty($_POST["ZipCode"])) {
    $ZipCodeErr = "*Zip Code is required";
  } else {
    $ZipCode = test_input($_POST["ZipCode"]);
}

  if (empty($_POST["Population"])) {
    $PopulationErr = "*Population is required";
  } else {
    $Population = test_input($_POST["Population"]);
}

  if (empty($_POST["MediumIncome"])) {
    $MediumIncomeErr = "*Medium Income is required";
  } else {
    $MediumIncome = test_input($_POST["MediumIncome"]); 
}

  if (empty($_POST["ProfitNumber"])) {
    $ProfitNumberErr = "*Number of Non-Profits is required";
  } else {
    $ProfitNumber = test_input($_POST["ProfitNumber"]);
}

  if (empty($_POST["Average"])) {
    $AverageErr = "*Average Donations is required";
  } else {
    $Average= test_input($_POST["Average"]);
}

  if (empty($_POST["Homeless"])) {
    $HomelessErr = "*Homeless Number is required";
  } else {
    $Homeless = test_input($_POST["Homeless"]);
}

  if (empty($_POST["Shelters"])) {
    $SheltersErr = "*Number of homeless people in shelters is required";
  } else {
    $Shelters= test_input($_POST["Shelters"]);
}

  if (empty($_POST["Adults"])) {
    $AdultsErr = "*Single adults who are homeless is required";
  } else {
    $Adults = test_input($_POST["Adults"]);
  }










if(isset($_POST['submit']))
{
    if($Description&&$Type&&$Name&&$StateName&&$CountyName&&$ZipCode&&$Address&&$Population&&$MediumIncome&&$ProfitNumber&&$Average&&$Homeless&&$Shelters&&$Adults)
    {
    $insert=mysqli_query($con, "INSERT INTO city_upload (Description, Type, Name, StateName, CountyName, ZipCode, Address, Population, MediumIncome, ProfitNumber, Average, Homeless, Shelters, Adults) VALUES ('$Description', '$Type', '$Name', '$StateName', '$CountyName', '$ZipCode', '$Address' '$Population' '$MediumIncome', '$ProfitNumber', '$Average', '$Homeless', '$Shelters', '$Adults') ");

}
else{
    echo "Failed";
}
}

HTML表单:

 <div class="tab-content">
                            <div id="tab-1" class="tab-pane active">
                                <div class="panel-body">
                                <form action="City_Upload.php" method="POST" enctype="multipart/form-data">
                                    <fieldset class="form-horizontal"> 

                                  <div class="form-group"><label class="col-sm-2 control-label">Type:</label>

                                            <div class="col-sm-10">
                                                <select class="form-control" id="Type" name="Type">
                                                    <option>City</option>
                                                    <option>Town</option>
                                                    <option>Neighborhood</option>
                                                    <option>University</option>
                                                </select>
                                        <div class="form-group"><label class="col-sm-2 control-label">Name:</label>
                                            <div class="col-sm-10"><input type="text" id="Name" name="Name" placeholder="City/Neighborhood/University Name"></div>
                                        </div>
                                        <div class="form-group"><label class="col-sm-2 control-label">State Name:</label>
                                            <div class="col-sm-10"><input type="text" id="StateName" name="StateName" placeholder="Example: New York"></div>
                                        </div>
                                        <div class="form-group"><label class="col-sm-2 control-label">Description:</label>
                                            <div class="col-sm-10"><textarea id="Description" name="Description" rows="10" cols="50"></textarea>
                                                <div class="summernote">


                                                </div>
                                            </div>
                                        </div>
                                        <div class="form-group"><label class="col-sm-2 control-label">County Name:</label>
                                            <div class="col-sm-10"><input type="text" id="CountyName" name="CountyName" placeholder="County Name"></div>
                                        </div>
                                        <div class="form-group"><label class="col-sm-2 control-label">Zip Code:</label>
                                            <div class="col-sm-10"><input type="text" id="ZipCode" name="ZipCode" placeholder="Zip Code"></div>
                                        </div>
                                        <div class="form-group"><label class="col-sm-2 control-label">Address (optional):</label>
                                            <div class="col-sm-10"><input type="text" id="Address" name="Address" placeholder="Lorem, Ipsum, has, been"></div>
                                        </div>
                                        <fieldset class="form-horizontal">
                                        <div class="form-group"><label class="col-sm-2 control-label">Population:</label>
                                            <div class="col-sm-10"><input type="text" id="Population" name="Population" placeholder="543"></div>
                                        </div>
                                        <div class="form-group"><label class="col-sm-2 control-label">Medium Income:</label>
                                            <div class="col-sm-10"><input type="text" id="MediumIncome" name="MediumIncome" placeholder="..."></div>
                                        </div>
                                        <div class="form-group"><label class="col-sm-2 control-label">Number of Non-Profits:</label>
                                            <div class="col-sm-10"><input type="text" id="ProfitNumber" name="ProfitNumber" placeholder="500"></div>
                                        </div>
                                        <div class="form-group"><label class="col-sm-2 control-label">Average Donations:</label>
                                            <div class="col-sm-10">
                                                <select class="form-control" id="Average" name="Average">
                                                    <option> $25</option>
                                                    <option> $50 </option>
                                                    <option> $75</option>
                                                    <option> $100 </option>
                                                    <option> $150</option>
                                                    <option> $200 </option>
                                                    <option> $300</option>
                                                    <option> $500 </option>
                                                    <option> $600 </option>
                                                    <option> $800 </option>
                                                    <option> $1000</option>
                                                    <option> $2000 </option>
                                                    <option> $4000</option>
                                                    <option> $6000 </option>
                                                    <option> $8000</option>
                                                    <option> $10000 </option>
                                                </select>
                                            </div>
                                        </div>
                                        <div class="form-group"><label class="col-sm-2 control-label">HUD Homeless Number:</label>
                                            <div class="col-sm-10"><input type="text" id="Homeless" name="Homeless" placeholder="Quantity"></div>
                                        </div>
                                        <div class="form-group"><label class="col-sm-2 control-label">Homeless People Who Are in Shelters:</label>
                                            <div class="col-sm-10"><input type="text" id="Shelters" name="Shelters" placeholder="2"></div>
                                        </div>
                                        <div class="form-group"><label class="col-sm-2 control-label">Single Adults Who Are Homeless:</label>
                                            <div class="col-sm-10"><input type="text" id="Adults" name="Adults" placeholder="0"></div>
                                        </div>

                                        <tr>
                                         <td colspan="2">
                                            <input type="submit" name="submit" value="Submit">

                                                <br><br>
                                            </td>
                                        </tr>
                                    </fieldset>
                                    </form>

                                </div>
                            </div>

1 个答案:

答案 0 :(得分:0)

我的猜测是你的INSERT语句因为条件而无法运行:

if($Description&&$Type&&$Name&&$StateName&&...

可能其中一个值的计算结果为FALSE(空字符串,零值)......

编辑:我猜是错的。您在'$Address''$Population'之间的查询中缺少逗号。