Issue with file upload PHP - php

I am not sure what is wrong with this code, so I was hoping for some helpful input.
The Problem
It will not get the name of the file that I have uploaded nor is it actually uploading it into the defined directory.
The PHP Code
<? include("header.php");
include("sidebar.php");
?>
<h2>Add Raffle</h2>
<?
$name = asql($_POST['name']);
$type = asql($_POST['prize']);
$min_points = asql($_POST['minp']);
$min_cash = asql($_POST['maxu']);
$cons = strtotime($_POST['cons']);
$cone = strtotime($_POST['cone']);
if($_POST['subm'])
{
if ($name == NULL OR $type == NULL OR $min_points == NULL OR $min_cash == NULL) {
print"Please Fill Out All Of The Required Fields<br /><a href='rafadd.php'><img src='img/green_arrow_left.png' alt='Go Back' /></a>"; include"footer.php";
exit();
}
if (!in_array($file['type'], array("image/gif", "image/jpeg", "image/png"))
|| $file['size'] > 20000)
{
if ($_FILES["file"]["error"] > 0)
{
echo "There was an error";
}
else
{
echo "";
$filename = $_FILES["file"]["name"];
if (file_exists("../images/rewards/" . $_FILES["file"]["name"]))
{
unlink("../images/raffles/" . $_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"],
"../images/raffles/" . $_FILES["file"]["name"]);
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"../images/raffles/" . $_FILES["file"]["name"]);
}
}
}
else
{
echo "Invalid file";
$updatecontests = mysql_query("INSERT INTO raffles (`id`, `raffle_name`, `raffle_prize`, `buy_in`, `max_entry`, `prize_image`, `start`, `end`) VALUES ('','$name','$type','$min_points','$min_cash','images/raffles/".$filename."','$cons','$cone')") or die(mysql_error());
if($updatecontests){
$create = mysql_query("CREATE TABLE IF NOT EXISTS `".$name."` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`username` text NOT NULL,
`entries` int(11) NOT NULL,
PRIMARY KEY (`username`(30)),
KEY `id` (`id`)
)");
}
print "You Have Successfully Added this Raffle<br /><br />
<a href='rafadd.php'><img src='img/green_arrow_left.png' alt='Go Back' /></a>";
}
else
{
print"<div class='form'>
<form action='' method='post'><input type=hidden name=subm value=1>";
?>
<div class="element">
<label for="name">Raffle Name: <span class="red">(required)</span></label>
<input id="name" name="name" size="50" />
</div>
<div class="element">
<label for="prize">Raffle Prize: <span class="red">(required)</span></label>
<input id="prize" name="prize" size="50" />
</div>
<div class='element'>
<label for='file'>Prize Image:</label>
<input type='file' name='file' id='file' />
</div>
<div class="element">
<label for="cons">Start Date: <span class="red">(required)</span></label>
<input id="cons" name="cons" size="50" /><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date">
</div>
<div class="element">
<label for="cone">End Date: <span class="red">(required)</span></label>
<input id="cone" name="cone" size="50" /><img src="images/cal.gif" width="16" height="16" border="0" alt="Pick a date">
</div>
<div class="element">
<label for="minp">Cost Per Ticket: <span class="red">(required)</span></label>
<input id="minp" name="minp" size="50" />
</div>
<div class="element">
<label for="maxu">Maximum Entries per User: <span class="red">(required 0 for unlimited)</span></label>
<input id="maxu" name="maxu" size="50" />
</div>
<?php
print" <dl class='submit'>
<input type='submit' name='subm' id='subm' value='Submit' />
</dl>
</form>
</div> ";
}
?>
</div><!-- end of right content-->
</div> <!--end of center content -->
<div class="clear"></div>
</div> <!--end of main content-->
<div class="footer">
<div class="left_footer">IN ADMIN PANEL | Powered by INDEZINER</div>
<div class="right_footer"><img src="images/indeziner_logo.gif" alt="" title="" border="0" /></div>
</div>
</div>
</body>
</html>
I have used the file upload snippet in another script and it uploads perfectly so I am not sure why it is not working here.

in the form tag add enctype="multipart/form-data"
<form action='' method='post'enctype="multipart/form-data">

<form action='' method='post'>
is wrong, needs to be:
<form action='' method='post' enctype='multipart/form-data'>
Also, use <?php instead of <?
PS I miss the point where $file is set.

It doesn't look like file is being instantiated anywhere. Did you mean:
if (!in_array($_FILES['file']['type'], array("image/gif", "image/jpeg", "image/png"))
|| $_FILES['file']['size'] > 20000)

Related

update not working without any error i am using php and sql

I want to update my form using PHP and SQL but update query not working. select query working but update not working. there is no error. when I am using a select query for show value in the input field and it's working. I am trying much think but not figure out what is the error. I am trying much think but not figure out what is the error.
<?php
include 'connection.php';
include 'config.php';
if (isset($_GET['edit'])) {
{
$topHeading=$_POST['topHeading'];
$mainHeading=$_POST['mainHeading'];
$bottomHeading=$_POST['bottomHeading'];
$filesname = $_FILES['file']['name'];
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);
// Select file type
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Valid file extensions
$extensions_arr = array("jpg","jpeg","png","gif","svg");
// Check extension
if( in_array($imageFileType,$extensions_arr) ){
// Convert to base64
$image_base64 = base64_encode(file_get_contents($_FILES['file']['tmp_name']) );
$images = 'data:image/'.$imageFileType.';base64,'.$image_base64;
$sql = "update banner set topHeading='$topHeading', filesname='$filesname', images='$images', mainHeading='$mainHeading', bottomHeading='$bottomHeading' where id='$id'";
if (mysqli_query($connection, $sql)) {
echo "Done";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
// mysqli_close($connection);
// Upload file
move_uploaded_file($_FILES['file']['tmp_name'],$target_dir.$filesname);
if($sql)
{
echo "";
}
else
{
echo "<script>alert('Data not inserted');</script>";
}
}
}
}
$query = mysqli_query($connection, "select * from banner");
while ($row = mysqli_fetch_array($query)) {
echo "<b><a href='editbanner.php?id={$row['id']}'>{$row['topHeading']}</a></b>";
echo "<br />";
}
if (isset($_GET['edit'])) {
echo '<div class="form" id="form3"><br><br><br><br><br><br>
<Span>Data Updated Successfuly......!!</span></div>';
}
?>
<?php
if (isset($_GET['id'])) {
$id = $_GET['id'];
$count = 0;
$mysqli = mysqli_query($connection,"SELECT * FROM banner");
foreach($mysqli as $row){
$count++;
?>
<form class="form-horizontal" method="post" name="form1" id="form1" enctype="multipart/form-data">
<div class="form-group label-floating form-rose">
<label for="brandname" class="control-label col-xs-2">Brand Name of Stock Broker</label>
<div class="col-xs-12">
<input type="text" name="topHeading" id="brandname" class="form-control" value="<?php echo $row["topHeading"]; ?>"/>
<span class="error" id="pointfn"></span>
</div>
</div>
<div class="form-group label-floating form-rose">
<label for="name" class="control-label col-xs-2">Contact Person</label>
<div class="col-xs-12">
<input type="text" name="mainHeading" id="name" class="form-control" value="<?php echo $row["mainHeading"]; ?>"/>
<span class="error" id="pointln"></span>
</div>
</div>
<div class="form-group label-floating form-rose">
<label for="designation" class="control-label col-xs-2">Designation</label>
<div class="col-xs-12">
<input type="text" name="bottomHeading" id="designation" class="form-control" value="<?php echo $row["bottomHeading"]; ?>"/>
<span class="error" id="designationln"></span>
</div>
</div>
<div class="label-floating form-rose file-upload">
<div class="file-select">
<div class="file-select-button" id="fileName">Upload Logo</div>
<div class="file-select-name" id="noFile">No file chosen...</div>
<input type="file" name="file" id="chooseFile" required>
</div>
</div>
<div class="form-group label-floating form-rose">
<div class="col-xs-12"> <input type='checkbox' name='checkbox' id="checkbox" required /> <label for="checkbox" class="col-xs-10 term">I accept the terms and condition</label>
</div>
</div>
<button type="submit" name="edit" value="Submit" id="formT" class="button-cta cta btn" onClick="return validateForm()"> I am Interested!</button>
</form>
<?php }} ?>
</body>
</html>
$id is missing above your update query...
$id = $_REQUEST['id'];
$sql = "update banner set topHeading='$topHeading', filesname='$filesname', images='$images', mainHeading='$mainHeading', bottomHeading='$bottomHeading' where id='$id'";

Insert and update in one form

yesterday my question regarding insert and update in one form i did but data are inserted and when i am clicking on edit link which is available on displayp.php it display all data's in field but when i am update any data and click on submit button its not update but it insert data next time in database and i am getting multiple record in database. Actually I had put insert and update in if condition when i am updating its not moving in else part so please help me... Thank You..
<div id="page-wrapper" >
<div id="page-inner">
<div class="row">
<div class="col-md-12">
<h2>Add Product</h2>
</div>
</div>
<!-- /. ROW -->
<hr />
<div class="row">
<?php
include('config.php');
if($_SERVER['REQUEST_METHOD']=='POST')
{
if(isset($_POST['submit'])=='POST')
{
echo "working";
$fileName=$_FILES["filepdf"]["name"];
$fileSize=$_FILES["filepdf"]["size"]/2024;
$fileType=$_FILES["filepdf"]["type"];
$fileTmpName=$_FILES["filepdf"]["tmp_name"];
if($fileType=="application/pdf"){
if($fileSize<=200){
//New file name
$random=rand(1111,9999);
$newFileName=$random.$fileName;
//File upload path
$uploadPath="testUpload/".$newFileName;
//function for upload file
if(move_uploaded_file($fileTmpName,$uploadPath))
{
echo "Successful<BR>";
echo "File Name :".$newFileName."<BR>";
echo "File Size :".$fileSize." kb"."<BR>";
echo "File Type :".$fileType."<BR>";
}
else
{
echo "Maximum upload file size limit is 200 kb";
}
}
else
{
return false;
echo "You can only upload a pdf doc file.";
}
echo "here downld";
$updates = isset($_POST['updates']) ? 'Yes' : 'No';
$catnm=$_POST['catnm'];
$prodnm=$_POST['prodnm'];
$prod_dtl=$_POST['prod_dtl'];
//$updates=$_POST['downld'];
$date=$_POST['date'];
$query=("insert into addproduct(catnm,prodnm,prod_dtl,pdf,downld,date)
values('$catnm','$prodnm','$prod_dtl','$uploadPath','$updates','$date')");
$result = mysql_query($query,$conn);
echo"record inserted";
if($result)
{
header("location:addProduct.php");
}
}
}
}
else if($_SERVER['REQUEST_METHOD']=='GET')
{
if(isset($_GET['id']))
{
$id = $_GET['id'];
if(isset($_POST['submit']))
{
$updates['downld'] = isset($_POST['downld']) ? 'Yes' : 'No';
$cat['catnm']=$_POST['catnm'];
$prodnm['prodnm']=$_POST['prodnm'];
$prod_dtl['prod_dtl']=$_GET['prod_dtl'];
$pdf['pdf']=$_GET['pdf'];
$downld['downld']=$_GET['downld'];
$date['date']=$_GET['date'];
$query3=mysql_query("update addproduct set catnm='$cat',
prodnm='$prodnm',prod_dtl='$prod_dtl'
,pdf='$pdf',downld='$updates',date='$date' where id='$id'");
if($query3)
{
header('location:addProduct.php');
}
}
$query1=mysql_query("select * from addproduct where id='$id'");
$query4=mysql_fetch_array($query1);
//echo "<pre>";
//print_r($query4);
}
}
?>
<div class="col-md-6 col-sm-12 col-xs-12">
<div class="panel-body">
<form role="form" method="post"
enctype="multipart/form-data" name="myform" >
<br/>
<label>Select Category:</label>
<div class="form-group">
<?php
include('config.php');
$query1=mysql_query("select catnm from addcategory");
?>
<select class=form-control m-b-10 name=catnm id=catnm >
<?php
echo"<option>---Select---</option required>";
while($query=mysql_fetch_array($query1))
{
$selectedCat = '';
if($query[catnm] == $query4['catnm'])
{
$selectedCat = "selected";
}
echo "<option value='$query[catnm]' $selectedCat >$query[catnm]</option>";
}
?>
</select>
<br/>
<div class="form-group">
<input type="text" class="form-control"
id="prodnm" placeholder="Product Name" name="prodnm" required
value= " <?php if(isset($query4['prodnm'])){echo $query4['prodnm'];}?>"/>
</div>
<div class="form-group">
<textarea class="form-control" id="catdtl"
name="prod_dtl" placeholder="Product Detail"
"rows="10" cols="62" required>
<?php if(isset($query4['prod_dtl'])){
echo $query4['prod_dtl'];} ?>
</textarea>
</div>
<div class="form-group">
<label for="exampleInputFile">PDF File</label>
<input type="file" id="exampleInputFile"
accept="application/pdf" name="filepdf" required >
file : <?php if(isset($query4['pdf'])){
echo $query4['pdf'];} ?>
</div>
<div class="checkbox">
<label>
<input id="checkbox" type="checkbox" name="updates" required
<?php if(isset($query4['downld']))
{ if($query4['downld'] == 'Yes')
echo "checked='checked'"; echo $query4['downld'];}
?> />
Is Downloadable <br />
</label>
</div>
<input type="text"
placeholder="Date of Upload" id="datepicker-7"
name="date" value="<?php if(isset($query4['date'])){
echo $query4['date'];}?>" required />
<div style="padding-top:20px;padding-bottom:40px;width:650px;">
<input type="submit" name="submit" id="button" tabindex="2"/>
<hr/>
</form>
</div>
<!-- /. ROW -->
</div>
<!-- /. PAGE INNER -->
</div>
<!-- /. PAGE WRAPPER -->
</div>
</body>
</html>
I answered you yesterday, but you not added an hidden input with id. This you can do?
insert and update in one form in php
Also, How you imagine this post in get?
if(isset($_GET['id']))
{
$id = $_GET['id'];
if(isset($_POST['submit']))
{
* * *
}
}

single quotes and double quotes not inserting in datadase using php

i am inserting data into mysql using php it's work partialy it's inserting everything but not single quotes(') ex. principle's message. and when i insert it like principle"s message. it's inserting in database but it's only displaying principle in text box after inserting. and my file is save.php is here.
<?php session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_SESSION['user'])) {
if($_GET['catId'] == '' || $_GET['catId'] == null)
header('location:../user/logout.php');
$inc = -1;
if($_POST['title'] == '' || $_POST['title'] == null) {
$inc++;$_SESSION['error'][$inc] = "TITLE IS REQUIRED";
}
$selectImg=mysql_query("SELECT pri_img FROM aboutus_tbl WHERE id=4");
if ($_GET['catId']==4) {
if($_FILES["file"]["name"]) {
$allowedExts = array("gif", "jpeg", "jpg", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = end($temp);
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 5242880)
&& in_array($extension, $allowedExts))
$imageNewName = md5(date("l, F d, Y h:i" ,time()) . (microtime())) . "." . $extension;
else {
$inc++;$_SESSION['error'][$inc] = "IVALID IMAGE";
}
}
else {
$inc++;$_SESSION['error'][$inc] = "IMAGE IS REQUIRED";
}
}
function inputValues() {
$_SESSION['values']['title'] = $_POST['title'];
$_SESSION['values']['sub_title1'] = $_POST['sub_title1'];
$_SESSION['values']['desc1'] = $_POST['desc1'];
$_SESSION['values']['sub_title2'] = $_POST['sub_title2'];
$_SESSION['values']['desc2'] = $_POST['desc2'];
$_SESSION['values']['sub_title3'] = $_POST['sub_title3'];
$_SESSION['values']['desc3'] = $_POST['desc3'];
header("location:../../views/aboutus_content/list.php?catId=".$_GET['catId']);
}
if($inc > -1)
inputValues();
else {
require_once('../../includes/connect.php');
if($_GET['catId']==4 && isset($_FILES["file"]["name"])) {
$update="UPDATE aboutus_tbl SET title='".$_POST['title']."',sub_title1='".$_POST['sub_title1']."',desc1='".$_POST['desc1']."',sub_title2='".$_POST['sub_title2']."',desc2='".$_POST['desc2']."',sub_title3='".$_POST['sub_title3']."',desc3='".$_POST['desc3']."',pri_img='".$imageNewName."' WHERE id='".$_GET['catId']."'";
}
else{
$update="UPDATE aboutus_tbl SET title='".$_POST['title']."',sub_title1='".$_POST['sub_title1']."',desc1='".$_POST['desc1']."',sub_title2='".$_POST['sub_title2']."',desc2='".$_POST['desc2']."',sub_title3='".$_POST['sub_title3']."',desc3='".$_POST['desc3']."' WHERE id='".$_GET['catId']."'";
}
if(mysql_query($update)) {
if($_GET['catId']==4 && isset($_FILES["file"]["name"])) {
move_uploaded_file($_FILES["file"]["tmp_name"],"../../public/img/principal/".$imageNewName);
unlink("../../public/img/principal/".mysql_result($selectImg, 0, "pri_img"));
}
$_SESSION['message'] = $_POST['title']." SUCESSFULLY UPDATED";
header('location:../../views/aboutus_content/list.php?catId='.$_GET['catId']);
} else {
$_SESSION['error'] = "ERROR : '".mysql_error()."' CODE : ".mysql_errno();
inputValues();
}
}
} else
header('location:../user/logout.php')
?>
and designing file is here list.php
<?php
ob_start();
include '../../includes/header.php';
if(!isset($_GET['catId']) || $_GET['catId']=='')
header('location:../error');
$contactResult = mysql_query("SELECT * FROM aboutus_tbl WHERE id='".$_GET['catId']."'");
if(mysql_num_rows($contactResult) != 1)
header('location:../error');
else {
?>
<div class="mainbar">
<div class="page-head">
<div class="container">
<div class="row">
<div class="col-md-12 col-sm-12 col-xs-12">
<h2><i class="fa fa-desktop"></i> <?php echo mysql_result($contactResult, 0, "title");?> Content</h2>
</div>
<div class="clearfix"></div>
</div>
</div>
</div>
<div class="container">
<?php
if(isset($_SESSION['error'])) {
echo"<div class='alert alert-danger'>";
for($i=0;$i<sizeof($_SESSION['error']);$i++)
echo "<p><b>".$_SESSION['error'][$i]."</b></p>";
echo"</div>";
unset($_SESSION['error']);
}
if(isset($_SESSION['message'])) {
echo"<div class='alert alert-success'><p><b>".$_SESSION['message']."</b></p></div>";
unset($_SESSION['message']);
}
?>
<div class="hide alert alert-danger" id="errorContainer"></div>
<div class="row">
<div class="col-lg-12">
<form action="../../controllers/aboutus_content/save.php?catId=<?php echo $_GET['catId'];?>" method="post" class="contactForms" role="form" enctype="multipart/form-data">
<div class="col-lg-6">
<div class="form-group">
<label for="title"><span class="text-danger">* </span>Title</label>
<input name="title" id="title" data-validation-allowing="'" class="form-control" placeholder="Enter Title" value="<?php if(isset($_SESSION['values'])) echo $_SESSION['values']['title']; echo mysql_result($contactResult, 0, "title");?>" />
</div>
<div class="form-group">
<label for="sub_title1"><span class="text-danger">* </span>Sub Title 1</label>
<input name="sub_title1" id="sub_title1" data-validation-allowing="'" class="form-control" placeholder="Enter Sub Title 1" value="<?php if(isset($_SESSION['values'])) echo $_SESSION['values']['sub_title1']; else echo mysql_result($contactResult, 0, "sub_title1");?>" />
</div>
<div class="form-group">
<label for="desc1"><span class="text-danger">* </span>Description 1</label>
<textarea name="desc1" id="desc1" data-validation-allowing="'" class="form-control" placeholder="Enter Description 1"><?php if(isset($_SESSION['values'])) echo $_SESSION['values']['desc1']; else echo mysql_result($contactResult, 0, "desc1");?></textarea>
</div>
<div class="form-group">
<label for="sub_title2"><span class="text-danger">* </span>Sub Title 2</label>
<input name="sub_title2" id="sub_title2" data-validation-allowing="'" class="form-control" placeholder="Enter Sub Title 2" value="<?php if(isset($_SESSION['values'])) echo $_SESSION['values']['sub_title2']; else echo mysql_result($contactResult, 0, "sub_title2");?>" />
</div>
<div class="form-group">
<label for="desc2"><span class="text-danger">* </span>Description 2</label>
<textarea name="desc2" id="desc2" data-validation-allowing="'" class="form-control" placeholder="Enter Description 2"><?php if(isset($_SESSION['values'])) echo $_SESSION['values']['desc2']; else echo mysql_result($contactResult, 0, "desc2");?></textarea>
</div>
</div>
<div class="col-lg-6">
<div class="form-group">
<label for="sub_title3"><span class="text-danger">* </span>Sub Title 3</label>
<input name="sub_title3" id="sub_title3" data-validation-allowing="'" class="form-control" placeholder="Enter Sub Title 3" value="<?php if(isset($_SESSION['values'])) echo $_SESSION['values']['sub_title3']; else echo mysql_result($contactResult, 0, "sub_title3");?>" />
</div>
<div class="form-group">
<label for="desc3"><span class="text-danger">* </span>Description 3</label>
<textarea name="desc3" id="desc3" data-validation-allowing="'" class="form-control" placeholder="Enter Description 3"><?php if(isset($_SESSION['values'])) echo $_SESSION['values']['desc3']; else echo mysql_result($contactResult, 0, "desc3");?></textarea>
</div>
<div class="form-group">
<label for="pri_img"><span class="text-danger">* </span>Principle Image(Only For Principal's Message)</label>
<input type="file" name="file" id="file" class="form-control">
</div>
</div>
<div class="form-group text-center">
<input type="submit" class="btn btn-info" value="Save" />
Reset
</div>
</form>
</div>
</div>
</div>
</div>
<?php
}
?>
<script type="text/javascript">
window.onload = function() {
$(document).ready(function() {
$('.aboutus_content').addClass('current');
$('.aboutus_content').addClass('open');
$(".courImgItm<?php echo $_GET['catId']?>").addClass('active');
});
}
</script>
<?php
include '../../includes/footer.php';
if(isset($_SESSION['values']))
unset($_SESSION['values']);
ob_flush();
?>
please help me.
Run your string through this first:
mysql_real_escape_string($string);
It'll fix it for ya
try using this way
in your sql query---
$m1=$_REQUEST['message'];
$msg='".str_replace("\"",""",str_replace("'","''",$m1))."';
hope this will help

Error in storing the records in database

The following code for create edit form to update records. If I give all field it update correctly if I didn't change image it does not update the record. I want to update record without change image and also I want to update records by only changing the image.
<section id="main-content">
<section class="wrapper">
<h3><i class="fa fa-angle-right"></i> About Components</h3>
<!-- BASIC FORM ELELEMNTS -->
<div class="row mt">
<div class="col-lg-12">
<div class="form-panel">
<h3><i class="fa fa-angle-right"></i> Team Update</h3>
<form class="form-horizontal style-form" enctype="multipart/form-data" method="post">
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Title</label>
<div class="col-sm-10">
<textarea type="text" name="Title" class="form-control" required><?php echo $img['a_title']; ?></textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Description</label>
<div class="col-sm-10">
<textarea type="text" name="Description" class="form-control" required><?php echo $img['a_description']; ?></textarea>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label">Picture</label>
<div class="col-sm-10">
<div class="clear" id="im">
<?php echo "<img src='/myproject/cp/image/".$img['a_image']."'height='50' width='50' >"; ?>
<button type="button" id="remove" >remove</button>
</div>
<div id="browse" style="display:none;">
<input type="file" name="imageupload" value="imageupload" />
</div>
</div>
</div>
<div class="form-group">
<label class="col-sm-2 col-sm-2 control-label"></label>
<div class="col-sm-10">
<input class="btn btn-theme" id="focusedInput" type="submit" name="submit" value="Submit" />
</div>
</div>
<a class="btn btn-primary" href="about.php">
<span>List view</span>
</a>
</form>
</div>
</div><!-- col-lg-12-->
</div><!-- /row --><?php
if(isset($_POST['submit']))
{
$var=$_GET['a_id'];
$file=$_SERVER['DOCUMENT_ROOT'].'myproject/cp/image/'.$img['a_image'];
unlink($file);
$var1=$_POST['Title'];
$var2=$_POST['Description'];
$image=$_FILES['imageupload']['name'];
$root=$_SERVER['DOCUMENT_ROOT'].'/myproject/cp/image/';
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$expo=explode(".", $_FILES["imageupload"]["name"]);
$upload_exts = end($expo);
if ((($_FILES["imageupload"]["type"] == "image/gif")|| ($_FILES["imageupload"]["type"] == "image/jpeg")|| ($_FILES["imageupload"]["type"] == "image/png")|| ($_FILES["imageupload"]["type"] == "image/jpg"))&& ($_FILES["imageupload"]["size"] < 2000000)&& in_array($upload_exts,$file_exts))
{
if ($_FILES["imageupload"]["error"] > 0)
{
echo "Return Code: " . $_FILES["imageupload"]["error"] . "<br>";
}
else
{
$image= rand(0,9999).'.'.$upload_exts;
move_uploaded_file($_FILES["imageupload"]["tmp_name"],$root.$image);
$sql=mysql_query("UPDATE about SET a_title='".$var1."',a_description='".$var2."',a_image='".$image."' WHERE a_id='".$var."'") or die(mysql_error());
echo "<meta http-equiv=refresh content=\"0; URL=about.php\">";
}
}
}
?>
As you said in comment , when you save the edits all other details get saved but your image get's removed even if you haven't changed it , clearly it's your php code , even before checking that if you are submiting an image or not you already doing an unlink on it , you have to use the edit below :
<?php
if(isset($_POST['submit']))
{
$var=$_GET['a_id'];
$file_del =$_SERVER['DOCUMENT_ROOT'].'myproject/cp/image/'.$img['a_image'];
$var1=$_POST['Title'];
$var2=$_POST['Description'];
$image=$_FILES['imageupload']['name'];
$root=$_SERVER['DOCUMENT_ROOT'].'/myproject/cp/image/';
$file_exts = array("jpg", "bmp", "jpeg", "gif", "png");
$expo=explode(".", $_FILES["imageupload"]["name"]);
$upload_exts = end($expo);
if ((($_FILES["imageupload"]["type"] == "image/gif")|| ($_FILES["imageupload"]["type"] == "image/jpeg")|| ($_FILES["imageupload"]["type"] == "image/png")|| ($_FILES["imageupload"]["type"] == "image/jpg"))&& ($_FILES["imageupload"]["size"] < 2000000)&& in_array($upload_exts,$file_exts))
{
if ($_FILES["imageupload"]["error"] > 0)
{
echo "Return Code: " . $_FILES["imageupload"]["error"] . "<br>";
}
else
{
unlink($file_del);
$image= rand(0,9999).'.'.$upload_exts;
move_uploaded_file($_FILES["imageupload"]["tmp_name"],$root.$image);
$sql=mysql_query("UPDATE about SET a_title='".$var1."',a_description='".$var2."',a_image='".$image."' WHERE a_id='".$var."'") or die(mysql_error());
echo "<meta http-equiv=refresh content=\"0; URL=about.php\">";
}
}
}

sending $_SERVER['request_uri'] via post

i got a usual form sending values to php file.
<form action=\"/admin/save\" method=\"post\" ENCTYPE=\"multipart/form-data\">
<div class=\"element\">
<label for=\"name\">Page ID <span class=\"red\">(required)</span></label>
<input id=\"id\" name=\"id\" value=".$edit['id']." class=\"text\" />
</div>
<div class=\"element\">
<label for=\"name\">Page title <span class=\"red\">(required)</span></label>
<input id=\"title\" name=\"title\" value=".$edit['title']." class=\"text\" />
</div>
<div class=\"element\">
<label for=\"category\">Category <span class=\"red\">(required)</span></label>
<input id=\"category\" name=\"category\" value=".$edit['category']." class=\"text\" />
</div>
<div class=\"element\">
<label for=\"attach\">Attachments</label>
<input type=\"file\" id=\"img\" name=\"img\" />
</div>
<div class=\"element\">
<label for=\"short-content\">Short content <span class=\"red\">(required)</span></label>
<textarea name=\"short_content\" id=\"short_content\" class=\"textarea\" rows=\"10\">".$edit['short_content']."</textarea>
</div>
<div class=\"element\">
<label for=\"content\">Long content <span class=\"red\">(required)</span></label>
<textarea name=\"long_content\" id=\"long_content\" class=\"textarea\" rows=\"10\">".$edit['content']."</textarea>
</div>
<div class=\"element\">
<label for=\"date\">Date <span class=\"red\">(required)</span></label>
<input id=\"date\" name=\"date\" class=\"text\" value=".$edit['date']." />
</div>
<div class=\"element\">
<label for=\"language\">Language <span class=\"red\">(required)</span></label>
<input id=\"language\" name=\"language\" value=".$edit['lang']." class=\"text\" />
</div>
<div class=\"entry\">
<button type=\"submit\" id=\"button-save\" class=\"add button-save\">Save page</button>
</div>
</form>
Sends to..
<?php include('/views/admin/header.php'); ?>
<div class="wrap">
<div id="header">
<div id="top">
<div class="left">
<p>Welcome, <strong><?php echo $_SESSION['user'];?></strong> [ logout ]</p>
</div>
<div class="right">
<div class="align-right">
<p>Avetisyan | Admin Panel</p>
</div>
</div>
</div>
<div id="nav">
<?php include '/views/admin/upper-menu.php'; ?>
</div>
</div>
<div id="content">
<div id="sidebar">
<div class="box">
<div class="h_title">› Pages</div>
<?php include '/views/admin/left-menu.php'; ?>
</div>
</div>
<div id="main">
<div class="full_w">
<?php
$id = $_POST['id'];
$category = $_POST['category'];
$title = $_POST['title'];
$short_content = $_POST['short_content'];
$long_content = $_POST['long_content'];
$date = $_POST['date'];
$lang = $_POST['language'];
if(empty($id)){
echo "<h3 style=\"color:red;\">Please fill ID</h3>";
}
if(empty($category)){
echo "<h3 style=\"color:red;\">Please fill Category</h3>";
}
if(empty($title)){
echo "<h3 style=\"color:red;\">Please fill Title</h3>";
}
if(empty($date)){
echo "<h3 style=\"color:red;\">Please fill Date</h3>";
}
if(empty($lang)){
echo "<h3 style=\"color:red;\">Please fill Lang</h3>";
}
if(isset($_FILES['img']['name'])){
$extension = end(explode(".",$_FILES['img']['name']));
//echo "file format: ".$extension."<br>";
$name = $_FILES['img']['name'];
$size = $_FILES['img']['size'];
if(file_exists("views/admin/uploads/".$name)){
echo $_FILES['img']['name']." exists";
}
if($extension != "jpg" && $extension != "png" && $extension != "gif"){
echo "File with format: ".$extension." is not aviable to upload";
}
if($size > 1000000) {
echo $size." is greater than 1 mb !";
}
else {
move_uploaded_file($_FILES['img']['tmp_name'],"views/admin/uploads/".$name);
}
}
?>
</div>
<div class="clear"></div>
</div>
<div id="footer">
<div class="left">
<p>Webex Technologies LLC | Admin Panel: Avetisyan</p>
</div>
<div class="right">
</div>
</div>
</div>
<?php include('/views/admin/footer.php');?>
In second file, i want to do an link back, so if user wrote something wrong, he click on that link, go back to edit page (edit page link look like this - /admin/edit?id=5&lang=arm). How i can send $_SERVER['request_uri'] via post method but not via inputs, i dont want to have there another input with it.. Or can u give me any solution here?
Use $_SERVER['HTTP_REFERER'] in the file you process the submission.
Easiest way is to have a hidden input:
<input type="hidden" name="previous_page" value="<?=$_SERVER['REQUEST_URI']?>" />
You then have access to the previous page URI via $_POST['previous_page'].

Categories