Reputation:
I have a form and php processing to store text and image to server my problem is when I submit form and check values are stored as null
even if I enter values in input box
php processing:
<?php
$db_username = 'sanoj';
$db_password = '123456';
$file1 = isset($_FILES['files']['name'][0]) ? $_FILES['files']['name'][0] : null;
$file2 = isset($_FILES['files']['name'][1]) ? $_FILES['files']['name'][1] : null;
$file3 = isset($_FILES['files']['name'][2]) ? $_FILES['files']['name'][2] : null;
$file4 = isset($_FILES['files']['name'][3]) ? $_FILES['files']['name'][3] : null;
$file5 = isset($_FILES['files']['name'][4]) ? $_FILES['files']['name'][4] : null;
$newname = md5(rand() * time());
if (isset($_FILES['files'])) {
$uploadedFiles = array();
foreach ($_FILES['files']['tmp_name'] as $key => $tmp_name) {
$errors = array();
$file_name = md5(uniqid("") . time());
$file_size = $_FILES['files']['size'][$key];
$file_tmp = $_FILES['files']['tmp_name'][$key];
$file_type = $_FILES['files']['type'][$key];
if ($file_type == "image/gif") {
$sExt = ".gif";
} elseif ($file_type == "image/jpeg" || $file_type == "image/pjpeg") {
$sExt = ".jpg";
} elseif ($file_type == "image/png" || $file_type == "image/x-png") {
$sExt = ".png";
}
if (!in_array($sExt, array('.gif', '.jpg', '.png'))) {
$errors[] = "Image types alowed are (.gif, .jpg, .png) only!";
}
if ($file_size > 2097152000) {
$errors[] = 'File size must be less than 2 MB';
}
$desired_dir = "user_data/";
if (empty($errors)) {
if (is_dir($desired_dir) == false) {
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if (move_uploaded_file($file_tmp, "$desired_dir/" . $file_name . $sExt)) {
$uploadedFiles[$key] = array($file_name . $sExt, 1);
} else {
echo "Couldn't upload file " . $_FILES['files']['name'][$key];
$uploadedFiles[$key] = array($_FILES['files']['name'][$key], 0);
}
} else {
}
}
foreach ($uploadedFiles as $key => $row) {
if (!empty($row[1])) {
$codestr = '$file' . ($key + 1) . ' = $row[0];';
eval($codestr);
} else {
$codestr = '$file' . ($key + 1) . ' = NULL;';
eval($codestr);
}
}
}
$orig_directory = "user_data/"; //Full image folder
$thumb_directory = "thumb/"; //Thumbnail folder
/* Opening the thumbnail directory and looping through all the thumbs: */
$dir_handle = @opendir($orig_directory); //Open Full image directory
if ($dir_handle > 1) { //Check to make sure the folder opened
$allowed_types = array('jpg', 'jpeg', 'gif', 'png');
$file_type = array();
$ext = '';
$title = '';
$i = 0;
while ($file_name = @readdir($dir_handle)) {
/* Skipping the system files: */
if ($file_name == '.' || $file_name == '..')
continue;
$file_type = explode('.', $file_name); //This gets the file name of the images
$ext = strtolower(array_pop($file_type));
/* Using the file name (withouth the extension) as a image title: */
$title = implode('.', $file_type);
$title = htmlspecialchars($title);
/* If the file extension is allowed: */
if (in_array($ext, $allowed_types)) {
/* If you would like to inpute images into a database, do your mysql query here */
/* The code past here is the code at the start of the tutorial */
/* Outputting each image: */
$nw = 100;
$nh = 100;
$source = "$desired_dir{$file_name}";
$stype = explode(".", $source);
$stype = $stype[count($stype) - 1];
$dest = "thumb/{$file_name}";
$size = getimagesize($source);
$w = $size[0];
$h = $size[1];
switch ($stype) {
case 'gif':
$simg = imagecreatefromgif($source);
break;
case 'jpg':
$simg = imagecreatefromjpeg($source);
break;
case 'png':
$simg = imagecreatefrompng($source);
break;
}
$dimg = resizePreservingAspectRatio($simg, $nw, $nh);
imagepng($dimg, $dest);
}
}
/* Closing the directory */
@closedir($dir_handle);
}
try {
#connection
$conn = new PDO('mysql:host=localhost;dbname=3ss', $db_username, $db_password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$data = $conn->prepare('INSERT INTO mobile (mcat, mtype, mtitle, image1, image2, image3, image4, image5, description, mmodel, modelnumber, alsoinclude, mcondition, price, youare, mname, email, phone, ylocation, ystreet) VALUES (:mcat, :mtype, :mtitle, :image1, :image2, :image3, :image4, :image5, :description, :mmodel, :modelnumber, :alsoinclude, :mcondition, :price, :youare, :mname, :email, :phone, :ylocation, :ystreet)');
$mcat = filter_input(INPUT_POST, 'mtype', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$mtype = filter_input(INPUT_POST, 'mtype', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$mtitle = filter_input(INPUT_POST, 'mtitle', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$description = filter_input(INPUT_POST, 'description', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$mmodel = filter_input(INPUT_POST, 'mmodel', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$modelnumber = filter_input(INPUT_POST, 'modelnumber', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$alsoinclude = filter_input(INPUT_POST, 'alsoinclude', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$mcondition = filter_input(INPUT_POST, 'mcondition', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$price = filter_input(INPUT_POST, 'price', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$youare = filter_input(INPUT_POST, 'youare', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$mname = filter_input(INPUT_POST, 'mname', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$email = filter_input(INPUT_POST, 'email', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$phone = filter_input(INPUT_POST, 'phone', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$ylocation = filter_input(INPUT_POST, 'ylocation', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$ystreet = filter_input(INPUT_POST, 'ystreet', FILTER_SANITIZE_STRING, FILTER_FLAG_ENCODE_AMP);
$data->execute(array(
':mcat' => $mcat,
':mtype' => $mtype,
':mtitle' => $mtitle,
'image1' => $file1,
'image2' => $file2,
'image3' => $file3,
'image4' => $file4,
'image5' => $file5,
':description' => $description,
':mmodel' => $mmodel,
':modelnumber' => $modelnumber,
':alsoinclude' => $alsoinclude,
':mcondition' => $mcondition,
':price' => $price,
':youare' => $youare,
':mname' => $mname,
':email' => $email,
':phone' => $phone,
':ylocation' => $ylocation,
':ystreet' => $ystreet
));
#exception handiling
} catch (PDOException $e) {
echo $e->getMessage();
}
function resizePreservingAspectRatio($img, $targetWidth, $targetHeight) {
$srcWidth = imagesx($img);
$srcHeight = imagesy($img);
// Determine new width / height preserving aspect ratio
$srcRatio = $srcWidth / $srcHeight;
$targetRatio = $targetWidth / $targetHeight;
if (($srcWidth <= $targetWidth) && ($srcHeight <= $targetHeight)) {
$imgTargetWidth = $srcWidth;
$imgTargetHeight = $srcHeight;
} else if ($targetRatio > $srcRatio) {
$imgTargetWidth = (int) ($targetHeight * $srcRatio);
$imgTargetHeight = $targetHeight;
} else {
$imgTargetWidth = $targetWidth;
$imgTargetHeight = (int) ($targetWidth / $srcRatio);
}
// Creating new image with desired size
$targetImg = imagecreatetruecolor($targetWidth, $targetHeight);
// Add transparency if your reduced image does not fit with the new size
$targetTransparent = imagecolorallocate($targetImg, 255, 0, 255);
imagefill($targetImg, 0, 0, $targetTransparent);
imagecolortransparent($targetImg, $targetTransparent);
// Copies image, centered to the new one (if it does not fit to it)
imagecopyresampled($targetImg, $img, 0, 0, 0, 0, $targetWidth, $targetHeight, $srcWidth, $srcHeight);
return $targetImg;
}
?>
form:
<form class="form-horizontal" method="post" action="process/mobile/mobileprocessing.php" enctype="multipart/form-data">
<fieldset>
<!-- Multiple Radios (inline) -->
<div class="form-group">
<label class="col-md-4 control-label" for="type">Type Of Ad ✒</label>
<div class="col-md-4">
<label class="radio-inline" for="type-0">
<input type="hidden" value="mobile" name="mcat">
<input type="radio" name="mtype" id="type-0" value="sell" >
I Want To Sell
</label>
<label class="radio-inline" for="type-1">
<input type="radio" name="mtype" id="type-1" value="buy" >
I Want To Buy
</label>
</div>
</div><br>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Title For Your Ad <r>*</r> ✒</label>
<div class="col-md-4">
<input id="textinput" name="mtitle" type="text" placeholder="✒ 1 Year Old Sony Xperia Neo v in Market Road at Rs.10000" class="form-control input-md">
<span class="help-block">A title more than 60 characters have 2X more sell!.</span>
</div>
</div><br>
<div id="uploa">
<div class="dis1234 lift">
<input type="file" name="files[]" multiple id="files" class="hidde fileInpu"/>
</div>
<output id="list"></output>
<div class="dis1234 rite">
<span>Don't Upload Internet Images</span>
</div>
</div><br>
<div class="form-group">
<label class="col-md-4 control-label" for="textarea">Description <r>*</r> ✒</label>
<div class="col-md-4">
<textarea class="form-control" id="textarea" name="description" placeholder="Description About Your Ad"></textarea>
</div>
</div><br>
<!-- Select Multiple -->
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">Select Brand <r>*</r> ✒</label>
<div class="col-md-4">
<select id="dropone" name="mmodel" class="form-control">
<option>Select a Mobile Brand</option>
<option value="vodafone">Vodafone</option>
<option value="lg">LG</option>
<option value="o2">O2</option>
<option value="htc">HTC</option>
<option value="samsung">Samsung</option>
<option value="nokia">Nokia</option>
<option value="fly">FLY</option>
<option value="alcatel">Alcatel</option>
<option value="zen">Zen</option>
<option value="palm">Palm</option>
<option value="viva">Viva</option>
<option value="intex">Intex</option>
<option value="karbonn">Karbonn</option>
<option value="lava">Lava</option>
<option value="tataindicom">Tata Indicom</option>
<option value="rocker">Rocker</option>
<option value="lemon">Lemon</option>
<option value="wynncom">Wynncom</option>
<option value="virginmobile">Virgin Mobile</option>
<option value="gfive">G-Five</option>
<option value="geepee">Gee Pee</option>
<option value="inq">INQ</option>
<option value="iball">Iball</option>
<option value="airfone">AirFone</option>
<option value="acer">Acer</option>
<option value="byond">Byond</option>
<option value="beetel">Beetel</option>
<option value="sagem">Sagem</option>
<option value="toshiba">Toshiba</option>
<option value="benq">BenQ</option>
<option value="pantech">Pantech</option>
<option value="videocon">Videocon</option>
<option value="spice">Spice</option>
<option value="zte">ZTE</option>
<option value="blackberry">BlackBerry</option>
<option value="maxx">Maxx</option>
<option value="appleiphone">Apple iPhone</option>
<option value="micromax">Micromax</option>
<option value="sonyericsson">Sony Ericsson</option>
<option value="hp">HP</option>
<option value="motorola">Motorola</option>
<option value="dell">Dell</option>
<option value="imate">I-Mate</option>
<option value="other">Other</option>
</select>
</div>
</div><br>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Mobile Model <r>*</r> ✒</label>
<div class="col-md-4">
<input id="textinput" name="modelnumber" type="text" placeholder="Xperia Neo v" class="form-control input-md">
</div>
</div><br>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Also Includes ✒</label>
<div class="col-md-4">
<input id="textinput" name="alsoinclude" type="text" placeholder="Case, Headset, Charger" class="form-control input-md">
</div>
</div><br>
<!-- Multiple Radios (inline) -->
<div class="form-group">
<label class="col-md-4 control-label" for="radios">Condition <r>*</r> ✒</label>
<div class="col-md-4">
<label class="radio-inline" for="radios-0">
<input type="radio" name="mcondition" id="radios-0" value="new" >
New
</label>
<label class="radio-inline" for="radios-1">
<input type="radio" name="mcondition" id="radios-1" value="old">
Old
</label>
</div>
</div><br>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Price ✒</label>
<div class="col-md-2">
<input id="textinput" name="price" type="text" placeholder="₹" class="form-control input-md">
</div>
</div>
<hr>
<h3>
<center>Seller Information</center>
</h3>
<hr>
<div class="form-group">
<label class="col-md-4 control-label" for="selectbasic">You Are <r>*</r> ✒</label>
<div class="col-md-4">
<select id="dropone" name="youare" class="form-control">
<option>Select Bellow</option>
<option value="Individual">Individual</option>
<option value="Dealer">Dealer</option>
</select>
</div>
</div><br>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Name <r>*</r> ✒</label>
<div class="col-md-4">
<input id="textinput" name="mname" type="text" placeholder="Sanoj Lawrence" class="form-control input-md">
</div>
</div><br>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Email <r>*</r> ✒</label>
<div class="col-md-4">
<input id="textinput" name="email" type="text" placeholder="✒[email protected]" class="form-control input-md">
<span class="help-block">Your mail id will not be shared</span>
</div>
</div><br>
<!-- Text input-->
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Phone Number ✒</label>
<div class="col-md-4">
<input id="textinput" name="phone" type="text" placeholder="☎ Phone Number" class="form-control input-md">
</div>
</div><br>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Your Loaction ✒</label>
<div class="col-md-4">
<input id="textinput" name="ylocation" type="text" placeholder="Enter Your Loacation (Town or Village Name)" class="form-control input-md">
</div>
</div><br>
<div class="form-group">
<label class="col-md-4 control-label" for="textinput">Your Street ✒</label>
<div class="col-md-4">
<input id="textinput" name="ystreet" type="text" placeholder="Enter Your Street (Street or Area)" class="form-control input-md">
</div>
</div><br>
<center>
<input type="submit" class="btn btn-success"></center>
</fieldset>
</form>
SQL:
create table `mobile`(
`id` int(9) NOT NULL auto_increment,
`mcat` varchar(255) NULL default '',
`mtype` varchar(255) NULL default '',
`mtitle` varchar(255) NULL default '',
`image1` varchar(255) NULL default '',
`image2` varchar(255) NULL default '',
`image3` varchar(255) NULL default '',
`image4` varchar(255) NULL default '',
`image5` varchar(255) NULL default '',
`description` varchar(255) NULL default '',
`mmodel` varchar(255) NULL default '',
`modelnumber` varchar(255) NULL default '',
`alsoinclude` varchar(255) NULL default '',
`mcondition` varchar(255) NULL default '',
`price` varchar(255) NULL default '',
`youare` varchar(255) NULL default '',
`mname` varchar(255) NULL default '',
`email` varchar(255) NULL default '',
`phone` varchar(255) NULL default '',
`ylocation` varchar(255) NULL default '',
`ystreet` varchar(255) NULL default '',
`ipnu` varchar(255) NULL default '',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=1 ;
Upvotes: 0
Views: 385
Reputation: 199
Try to get rid of the NULL Default in your Data Base. Then, when you try to insert the values, it should give you the error.
Upvotes: 0
Reputation: 1286
What about getting rid of the fourth parameter in filter_input()
function?
Upvotes: 0
Reputation: 804
I copied your codes ( including the SQL ) tested it on my machine and with the exception of Thumbnail folder, which you did not remember to create, things worked fine. So to create this directory, I added the following after the definition:
if ( is_dir( $thumb_directory ) == false) {
mkdir("$thumb_directory", 0700); // Create directory if it does not exist
}
I guess the problem is with the environment. First find out if your files are actually being submitted. Do this at the very top ( the exit() is to terminate the script after dumping the $_FILES array):
var_dump( $_FILES['files'] );
exit();
This will print array of uploaded files on the screen. If the files are properly submitted, you should see something like
array(5) { ["name"]=> array(5) { [0]=> string(23) "Computer ICE Africa.JPG" [1]=> string(24) "Computer ICE Africa2.jpg" [2]=> string(27) "Computer ICE AfricaLogo.png" [3]=> string(17) "Guest - SLAB4.jpg" [4]=> string(17) "Guest - SLAB5.jpg" } ["type"]=> array(5) { [0]=> string(10) "image/jpeg" [1]=> string(10) "image/jpeg" [2]=> string(9) "image/png" [3]=> string(10) "image/jpeg" [4]=> string(10) "image/jpeg" } ["tmp_name"]=> array(5) { [0]=> string(24) "C:\xampp\tmp\php3630.tmp" [1]=> string(24) "C:\xampp\tmp\php3641.tmp" [2]=> string(24) "C:\xampp\tmp\php3642.tmp" [3]=> string(24) "C:\xampp\tmp\php3643.tmp" [4]=> string(24) "C:\xampp\tmp\php3653.tmp" } ["error"]=> array(5) { [0]=> int(0) [1]=> int(0) [2]=> int(0) [3]=> int(0) [4]=> int(0) } ["size"]=> array(5) { [0]=> int(103834) [1]=> int(95387) [2]=> int(12901) [3]=> int(204380) [4]=> int(179149) } }
If you see the above ( file names and sizes will differ), then inspect the size array:
["size"]=> array(5) { [0]=> int(103834) [1]=> int(95387) [2]=> int(12901) [3]=> int(204380) [4]=> int(179149)
Make sure you upload files of a few kb each. The total of those will be less than 1 MB to start with. Try all this and let's see how it goes.
Upvotes: 2
Reputation: 2515
You should check the query errors.
I guess your problem is related to:
'image1' => $file1,
'image2' => $file2,
'image3' => $file3,
'image4' => $file4,
'image5' => $file5,
It should be:
':image1' => $file1,
':image2' => $file2,
':image3' => $file3,
':image4' => $file4,
':image5' => $file5,
Also, if you want to check the query's error:
Error Checking for PDO Prepared Statements
Upvotes: 0