$street_address = NULL;
$price = NULL;
$number_bedrooms = NULL;
$number_baths = NULL;
$sq_ft = NULL;
$year_built = NULL;
$featured = NULL;
$pImage = NULL;
//create storage for the checkbox values for featured house items.
$pool = 0;
$finished_basement = 0;
$fenced_yard = 0;
if (isset($_POST['submit'])) {//checks to see if the user submit the form.
// print_r ($_POST); // This will show you what array information is being sent to post on the screen
//print_r ($_FILES); //can be used to view the contents of an array
// Gather the home listing data from the POST
if(isset($_POST['pImage'])) {
$pImage = $_POST['pImage'];
}
$street_address = $_POST['street_address'];
$price = $_POST['price'];
$number_bedrooms = $_POST['number_bedrooms'];
$number_baths = $_POST['number_baths'];
$sq_ft = $_POST['sq_ft'];
$year_built = $_POST['year_built'];
$pDesc = $_POST['pDesc'];
if (isset($_POST['featured']) ) { //used to check whether the user selected if the home was selected as featured. If they didn't the item is not passed to the POST.
$featured = $_POST['featured'];
} else {
$featured = NULL;
}
if (isset($_POST['pool']) ) {
$pool = $_POST['pool'];
} else {
$pool = 0;
} // this end bracket is "attached" to the process checking to see if the checkbox was 'ticked'.
//process checked finished basement box.
if (isset($_POST['finished_basement']) ) {
$finished_basement = $_POST['finished_basement'];
} else {
$finished_basement = 0;
} // end of finished_basement checkbox check
//process checked fenced yard box.
if (isset($_POST['fenced_yard']) ) { // Fenced Yard Checkbox Process Check
$fenced_yard = $_POST['fenced_yard'];
} else {
$fenced_yard = 0;
} // end of finished_basement checkbox check
} // end of $_POST submission check (starts on line #40)
// Street Address Validation Check - See if address was entered into the form.
if (empty($street_address)) {
echo "You didn't fill in a streetaddress. <br />";
$output_form = true; // will print form.
} // end of street_address check
// Image , Price & Image validation check
// Price Validation Check - See if Price was entered into the form as a number.
if (!is_numeric($price)) { //is not a number function.
echo "You didn't fill in price as a number.";
$output_form = true; // will print form.
}
// Used to store information for uploading images associative array.
$pImage = $_FILES['pImage']['name']; //array variable. Code pulled from 02fileglobaladd.txt document. $_FILES used to store file information from uploa
$pImage_type = $_FILES['pImage']['type'];
$pImage_size = $_FILES['pImage']['size'];
// Image File Size Validation check -- checks to be sure that the uploaded image is no larger than 1 MB
if (!empty($pImage)) { // empty is for name of file.
if ((($pImage_type == 'image/gif') || ($pImage_type == 'image/jpeg') || ($pImage_type == 'image/pjpeg') || ($pImage_type == 'image/png'))
&& ($pImage_size > 0) && ($pImage_size <= GW_MAXFILESIZE)) { // check to make sure that the file type is valid and that the file is larger than 0 but less than 1meg
if ($_FILES['pImage']['error'] == 0) { // test to be sure file gets uploaded.
$target = GW_UPLOADPATH . $pImage; // try to move file to images folder // FILE UNIQUE time function added from book (page 252) that duplicates and chages file name if there
// is more than one version
if (move_uploaded_file($_FILES['pImage']['tmp_name'], $target)) {
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Write the data to the database
$query = "INSERT INTO homes ( street_address, price, number_bedrooms, number_baths, sq_ft, year_built, pool, finished_basement, fenced_yard, featured, pDesc, pImage ) " .
"VALUES ('$street_address', '$price', '$number_bedrooms', '$number_baths', '$sq_ft', '$year_built', '$pool', '$finished_basement', '$fenced_yard', '$featured', '$pDesc','$pImage')";
$result = mysqli_query($dbc, $query)
or die('Error querying database.');
/* Testing echos
echo $query;
echo '<br />'
/**/
// Confirmation for the user that will be displayed once the user clicks the submit button (As long as there are no errors with the PHP code)
echo '<p>Thank you for Your Submission. The home has been added to the database.</p>';
echo 'Street Address: ' . $street_address . '<br />';
echo 'Price: ' . $price . '<br />';
echo 'Number of Bedrooms: ' . $number_bedrooms . '<br />';
echo 'Number of Baths: ' . $number_baths . '<br />';
echo 'Square Feet: ' . $sq_ft . '<br />';
echo 'Year Built: ' . $year_built . '<br />';
// featured if statement
echo 'Featured:' ;
if ($featured = 1) {echo ' Yes <br />'; } else {echo ' No <br /> ';}
//example of using ternary format of if/else -- additional items of home (pool, finished basement, fenced in yard ) checkboxes
echo 'Pool: ' . (($pool) ? 'YES' : 'NO' ). '<br />' ;
echo 'Finished Basement: ' . (($finished_basement) ? 'YES' : 'NO' ). '<br />' ;
echo 'Fenced Yard: ' . (($fenced_yard) ? 'YES' : 'NO' ). '<br />' ;
echo 'Desc:<span class="desc"> ' . ($pDesc). '</span><br />';
echo 'Image File: ';
echo '<img src= " ' . GW_UPLOADPATH . $pImage . ' " alt="Homes Image" /></p>';
echo '<p><a href="index.php"><< Back to Listings Page.</a></p>';
// Clear the data in the form
$street_address = "";
$price = "";
$number_bedrooms = "";
$number_baths = "";
$sq_ft = "";
$year_built = "";
$pool = "";
$finished_basement = "";
$featured = "";
$pImage = "";
$pDesc ="";
mysqli_close($dbc);
} //movefile worked....
else { //movefile didn't work... error message is printed out
echo '<p class="error">Sorry, there was a problem loading your product image.</p>';
}
}
} // no file type or size error / ENDING BRACKET
else { // there was a file type or size error / ENDING BRACKET
echo '<p class="error">The screen shot must be a GIF, JPEG, or PNG image file no greater than ' . (GW_MAXFILESIZE / 1024) . ' KB in size.</p>';
} // type size error message printed. / ENDING BRACKET
@unlink($_FILES['pImage']['tmp_name']); // unlink is php command to delete the file. Its getting rid of the temporary file. @ symbol ignores any error message.
} // data validated
else { // data didn't validate, show error message.
echo '<p class="error">Please enter all the required product information.</p>';
}
?>
我在GET&amp;添加了一个if isset命令。 POST语句,有人可以帮助我。我知道在修复此问题后我必须修改我的代码。我的教授提到了使用print-r语句......
感谢您的帮助......我是PHP的新手
答案 0 :(得分:0)
问题在于这句话:
if(isset($_POST['pImage'])) {
$pImage = $_POST['pImage'];
}
由于$_POST['pImage']
是图片,因此您无法使用$_POST
。要检查是否已上传,只需使用:
if($_FILES['pImage']['name') {
这将检查图像的名称,如果可用,将返回true
。
这句话错了:
if ($featured = 1) {echo ' Yes <br />'; } else {echo ' No <br /> ';}
在比较$featured
是否等于1
时,您需要使用PHP的比较运算符==
。 =
用于分配。
因此,它应该是:
if ($featured == 1) {echo ' Yes <br />'; } else {echo ' No <br /> ';}
有关PHP运算符的更多信息:http://php.net/manual/en/language.operators.comparison.php。
此外,您不应该禁止错误消息:
@unlink($_FILES['pImage']['tmp_name']);
删除@以启用错误报告。它在诊断语法错误时非常有用。