How to Insert and update using one form in php. Insert and Update not working
what to do. I have file productinsert.php and one file for display data displayProduct.php when i am inserting data are not inserted and it redirect on same page it not displaying data on addProduct.php also not updating plz help
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML
1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
<?php
include('header1.php');
?>
</head>
<body>
<?php
include('sidenav.php');
?>
<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']))
{
$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="get" enctype="multipart/form-data"
name="myform" action= "<?php echo $_SERVER['PHP_SELF'];?>">
<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 $query4['prodnm']; ?>"/>
</div>
<div class="form-group">
<textarea class="form-control" id="catdtl" name="catdtl"
placeholder="Product Detail" "rows="10" cols="62" required>
<?php 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 echo $query4['pdf']; ?>
</div>
<div class="checkbox">
<label>
<input id="checkbox" type="checkbox" name="updates" required
/> Is Downloadable <br />
</label>
</div>
<input type="text"
placeholder="Date of Upload" id="" name="date" required />
<div style="padding-top:20px;padding-bottom:40px;width:650px;">
<input type="submit" name="submit" id="button" tabindex="2"/>
<hr/>
</form>
<?php
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');
}
}
}
?>
</div>
<!-- /. ROW -->
</div>
<!-- /. PAGE INNER -->
</div>
<!-- /. PAGE WRAPPER -->
</div>
</body>
</html>
When is need to update add to form a hidden filed with id equal to row to update and name id. If need to insert don't use id row.
<form>
if(isset($_GET['update'])) echo '<input type="hidden" name="id" value="'.$_GET['id'].'">';
</form>
if(isset($_POST['id'])) { do sql to update where id=$_POST['id']; } else {do sql to insert}
Also to update you need to fill inputs with existing data from DB like this
<input value="<? if(isset($_GET['update'])) echo $value; ?>">
<select> <option value="1" if(isset($_GET['update']) && select1==1) echo 'selected'; >1</options>
<option value="2" if(isset($_GET['update']) && select1==2) echo 'selected'; >2</options>
Related
I'm updating my post with an image field. Every thing is going fine, but i lose the path of the image, son if i leave the field empty the image path is null.
I have been trying different ways of including the old path to the field image. Those are the lines that have the old path and the new path.
<input type="file" name="image" value="<?php echo $row['image']; ?>">
<input type="hidden" name="old_image" value="<?php echo $row['image']; ?>" >
I tried also with <input type="file" name="image" value="<?php echo $image; ?>"> but it doesn't work.
Any suggestion?
Thanks in advance!
:)
This is my code:
<?php
require 'database.php';
$db = mysqli_connect("", "", "", "");
$id=$_GET["id"];
$title='';
$image='';
$image_text='';
$category='';
$result = mysqli_query($db, "SELECT * FROM `images` WHERE `id` = $id ");
while ($row = mysqli_fetch_array($result))
{
$title=$row['title'];
$image=$row['image'];
$papayos = $row['papayos'];
$cantidad = $row['cantidad'];
$image_text=nl2br($row['image_text']);
$category=$row['category'];
}
?>
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Modificar. Lapapaya.org Educación y colaboración </title>
<meta name="keywords" content="colaboracion, economía del bien común, emprendimiento"/>
<meta name="description" content="Plataforma para desarrollo de proyectos productivos con bonos de intercambio"/>
<link rel="stylesheet" href="css/style.css">
<link rel="shortcut icon" type="image/png" href="images/favicon.png">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bulma#0.8.0/css/bulma.min.css">
<script defer src="https://use.fontawesome.com/releases/v5.3.1/js/all.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<section class="section has-background-white">
<div class="container">
<div class="columns is-centered">
<img class="logo-intro" src="images/logo.png" alt="Logo">
<br>
</div>
</div>
</section>
<form method="POST" action='' enctype="multipart/form-data">
<div class="container">
<h1>Título del artículo</h1>
<br>
<div class="field">
<div class="control">
<input class="input is-warning is-fullwidth" type="text" name="title" value="<?php echo $title; ?>" placeholder="Título">
</div>
<br>
<br>
<div>
<div class="notification"> <h1>Imagen</h1>
<input type="hidden" name="size" value="1000000">
<div class="field">
<div class="control">
<input type="file" name="image" value="<?php echo $row['image']; ?>">
<input type="hidden" name="old_image" value="<?php echo $row['image']; ?>" >
</div>
</div>
<div>
<h1>Descripción</h1>
<textarea class="textarea" name="image_text" rows="8" cols="80"><?php echo $image_text; ?></textarea>
<br>
<h1>Categoría</h1>
<div class=" select is-warning is-fullwidth">
<select type="text" class="" name="category" value="<?php echo $category; ?>">
<option value="1 ">Productos agrícolas</option>
<option value="2 ">Servicio</option>
<option value="3 ">Artesanía</option>
<option value="4 ">Arte</option>
<option value="5">Diseño Industrial</option>
<option value="6 ">Ciudad</option>
<option value="7 ">Economía</option>
<option value="8 ">Emprendimiento</option>
<option value="9 ">Programación</option>
<option value="10 ">Reflexión</option>
<option value="11">Río</option>
<option value="12 ">Seguridad alimentaria</option>
<option value="13 ">Vivienda</option>
</select>
</div>
<h1>Precio</h1>
<br>
<div class="field">
<div class="control">
<input class="input is-warning" type="number" name="papayos" value="<?php echo $papayos; ?>" placeholder="Papayos">
</div>
<br>
<h1>Cantidad</h1>
<br>
<div class="field">
<div class="control">
<input class="input is-warning" type="number" name="cantidad" value="<?php echo $cantidad; ?>" placeholder="Cantidad">
</div>
<br>
<br>
<div>
<button class="button is-outlined is-medium is-fullwidth" type="update" name="update">Modificar</button>
</div>
</div>
</form>
</body>
</html>
<?php
$server = '';
$username = '';
$password = '';
$database = '';
if (isset($_POST['update'])) {
$title=$_POST['title'];
$image_text=nl2br($_POST['image_text']);
$category=$_POST['category'];
$papayos =$_POST['papayos'];
$cantidad =$_POST['cantidad'];
$total = $papayos * $cantidad;
$old_image = $_POST['old_image'];
if(isset($_FILES['image']['name']) && ($_FILES['image']['name']!="")) {
$size=$_FILES['image']['size'];
$temp=$_FILES['image']['tmp_name'];
$type=$_FILES['image']['type'];
$image_name=$_FILES['image']['name'];
unlink("images/$old_image");
move_uploaded_file($temp, "images/$image_name");
}else {
$image_name=$old_image;
}
$update=mysqli_query($db, "UPDATE images SET title = '$title', image = '$image_name', image_text = '$image_text', category = '$category', papayos = '$papayos', cantidad = '$cantidad', total = '$total' WHERE id = $id");
if ($update) {
echo "<script>alert('Datos actualizados exitosamente')</script>";
echo "<script>window.open('index.php', '_self')</script>";
}
else {
echo "<script>alert('Inserción fallida')</script>";
}
} ?>
I fixed adding this above the imput file:
<img src="images/<?php echo $image; ?>">
:)
I want id of selected items from checkbox and insert to database. I can insert other items to database but id of selected checkbox is not inserting.
<div class="row" >
<div class="col-xs-3">
<form action="itemad.php" method="post">
<div class="multiselect">
<input type="text" name="datepicker" id="datepicker" placeholder="Date" </p>
<b>Select Menu:</b>
<div class="checkbox" onclick="showCheckboxes()">
<select>
<option>Select an option</option>
</select>
<div class="overSelect"></div>
</div>
<div id="checkboxes">
<?php
include 'connection.php';
$query="select item from item";
$result=mysql_query("$query");
while($row=mysql_fetch_array($result)):;
echo $row['item'];
for($i=0; $i<=count('item'); $i++ )
?>
<input type="checkbox" name="check_list[]" value="" ></input><br>
<?php endwhile;?>
</div>
<input type="text" name="price" Placeholder="Pricee" minlength="1" ><br><br>
<input type="submit" name="submit">
</div>
</div>
<script type="text/javascript">
var expanded = false;
function showCheckboxes() {
var checkboxes = document.getElementById("checkboxes");
if (!expanded)
{
checkboxes.style.display = "block";
expanded = true;
}
else
{
checkboxes.style.display = "none";
expanded = false;
}
}
</script>
</form>
</div>
</div>
itemad.php:
if(isset($_POST['submit']))
{
if(!empty($_POST['check_list']))
{
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $id)
{
echo "<p>".$id ."</p>";
} }
else
{ echo "<b>Please Select Atleast One Option.</b>"; }
$price=$_POST['price'];
$date=$_POST['datepicker'];
$query= "insert into menu(item,price,date) values('$id','$price','$date')";
$result=mysql_query($query);
echo "suucesful";
}
i want to store id of each item in database. but in database i am not getting id for selected items.
You missed one thing:
<input type="checkbox" name="check_list[]" value="" />
The check_list array should not have an empty value. You should have to put a value into it.
<?php
$query="select item_id, item from item";
$result=mysql_query("$query");
while($row=mysql_fetch_array($result)):;
?>
<input type="checkbox" name="check_list[]" value="<?php echo $row['item_id']; ?>" /><br>
<?php endwhile;?>
<div class="row" >
<div class="col-xs-3">
<form action="itemad.php" method="post">
<div class="multiselect">
<input type="text" name="datepicker" id="datepicker" placeholder="Date" />
<b>Select Menu:</b>
<div class="checkbox" onclick="showCheckboxes()">
<select>
<option>Select an option</option>
</select>
<div class="overSelect"></div>
</div>
<div id="checkboxes">
<?php
include 'connection.php';
$query="select item_id, item from item";
$result=mysql_query("$query");
while($row=mysql_fetch_array($result)):;
echo $row['item'];
?>
<input type="checkbox" name="check_list[]" value="<?php echo $row['item_id']; ?>" /><br>
<?php endwhile;?>
</div>
<input type="text" name="price" Placeholder="Price" minlength="1" ><br><br>
<input type="submit" name="submit">
</div>
<script type="text/javascript">
var expanded = false;
function showCheckboxes() {
if (!expanded)
{
$('#checkboxes input:checkbox').each(function () {
this.style.display = "block";
});
expanded = true;
}
else
{
$('#checkboxes input:checkbox').each(function () {
this.style.display = "none";
});
expanded = false;
}
}
</script>
</form>
</div>
</div>
itemad.php:
if(isset($_POST['submit']))
{
if(!empty($_POST['check_list']))
{
// Counting number of checked checkboxes.
$checked_count = count($_POST['check_list']);
echo "You have selected following ".$checked_count." option(s): <br/>";
// Loop to store and display values of individual checked checkbox.
foreach($_POST['check_list'] as $id)
{
echo "<p>".$id ."</p>";
$price=$_POST['price'];
$date=$_POST['datepicker'];
$query= "insert into menu(item,price,date) values('$id','$price','$date')";
$result=mysql_query($query);
echo "suucesful";
}
}
else
{
echo "<b>Please Select Atleast One Option.</b>";
}
}
I am new in php.I am Uploading a file from local to remote server but file is not uploading .I am uploading pdf file from server php which is in Dreamweaver but file is not uploaded what to do..And next thing i dont want extension .pdf i wants only name of pdf.. ?
if(isset($_POST['submit']))
{
echo"Upload Only PDf File";
$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)
{
$random=rand(1111,9999);
$newFileName=$random.$fileName;
$uploadPath="testUpload/".$newFileName;
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 file.";
}
<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 category");
?>
<select class=form-control m-b-10 name=catnm id=catnm >
<?php
echo"<option>---Select---</option>";
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/>
<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" >
file : <?php if(isset($query4['pdf'])){
echo $query4['pdf'];} ?>
</div>
<div class="checkbox">
<label>
<input id="checkbox" type="checkbox" name="updates"
<?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" name="date" value="<?php
if(isset($_GET['id'])){$id = $_GET['id'];echo $query4['date'];
}
else
{
echodate('d/m/Y');
}? >"id="datepicker-7">
<div style="padding-top:20px;padding-bottom:40px;width:650px;">
<input type="submit" name="submit" id="button"
onclick="madeSelection(document.getElementById('catnm'),'Please Select`
Category',event)" value="<?php if(isset($_GET['id'])){$id =
$_GET['id'];echo"Update";}else{echo"Submit";}?>"/>
<input type="button" value="cancel" onclick="window.location='index.php'"/>
currently I am doing multiple page form (page 1 and page 2). I save the details in page 1 using SESSION and insert successfully to database. I also successfully retrieve the data from database and display on page (activityLog.php).
However, I face problem when I want to edit/update the form.The value in the form wasn't update as well as the database. Please help.Thanks.
Below is my display cause page and edit form (editIndividual.php).
activityLog.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title>My Activity Log</title>
<?php
session_start();
include 'header.php';
?>
<div id="content">
<div class="section">
<h4 align="center" style="font-size:28px;">My Activity Log</h4>
<div>
Basic Setting
Change Password
<a class="selected" href="activityLog.php">My Activity Log</a>
</div>
<label style="font-size:19px;color:#333;"<strong>Manage your cause below.</strong>
<div class="figure">
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" class="register">
<div class="first" >
<?php
include 'dbconn.php';
if(isset($_SESSION['email'])){
$query="SELECT * from cause join user_info on cause.userID=user_info.userID where email='{$_SESSION['email']}'";
$result=mysqli_query($conn, $query);
while ($row=mysqli_fetch_array($result)){
$causeID = $row['causeID'];
$title = $row['title'];
$category = $row['category'];
$donateType = $row['donateType'];
$goal = $row['goal'];
$description = $row['description'];
$createDate = $row['createDate'];
$image = $row['image'];
echo "<a href='editIndividual.php?u=$causeID'>".$title."</a><br>";
echo "<img height='80' width='100' src='upload/".$image."'><br>";
}
}
?>
<fieldset>
</fieldset>
</div>
</form><!--end form-->
</div>
</div>
</div>
<?php include 'footer.php';?> <!--include footer.php-->
</body>
</html>
editIndividual.php
<html>
<head>
<meta charset="UTF-8" />
<!--include header-->
<?php
session_start();
include 'dbconn.php';
include 'header.php';
if(!isset($_SESSION['email'])){
echo "Please login before proceed!";
header("location:login.php");
}
else{
if(isset($_GET['u'])){
$causeID = $_GET['u'];
$query="SELECT * from cause where causeID=$causeID ";
$result=mysqli_query($conn, $query);
while ($row=mysqli_fetch_array($result)){
$causeID = $row['causeID'];
$title = $row['title'];
$category = $row['category'];
$donateType = $row['donateType'];
$goal = $row['goal'];
$description = $row['description'];
$createDate = $row['createDate'];
$image = $row['image'];
}
//update title
$title = $_SESSION['title'];
$upTitle = "UPDATE cause set title='$title' where causeID='$causeID'";
$upTitleResult = mysqli_query($conn,$upTitle);
//update category
$category = $_SESSION['category'];
$upCategory = "UPDATE cause set category='$category' where causeID='$causeID'";
$upCategoryResult = mysqli_query($conn,$upCategory);
//update donate type
$donateType = $_SESSION['donateType'];
$upDonateType = "UPDATE cause set donateType='$donateType' where causeID='$causeID'";
$upDonateTypeResult = mysqli_query($conn,$upDonateType);
//update goal
$goal = $_SESSION['goal'];
$upGoal = "UPDATE cause set goal='$goal' where causeID='$causeID'";
$upGoalResult = mysqli_query($conn,$upGoal);
//update description
$description = $_POST['description'];
$upDes = "UPDATE cause set description='$description' where causeID='$causeID'";
$upDesResult = mysqli_query($conn,$upDes);
//update image
$image = $_FILES['imageToUpload']['name'];
$upImage = "UPDATE cause set image='$image' where causeID='$causeID'";
$upImageResult = mysqli_query($conn,$upImage);
}
}
?>
<!--Change choose file button default name-->
<script>
function HandleBrowseClick()
{
var fileinput = document.getElementById("imageToUpload");
fileinput.click();
}
function Handlechange()
{
var fileinput = document.getElementById("imageToUpload");
fileinput.value;
}
</script>
<div id="content">
<div>
<form action="activityLog.php" id="editInd_form" name="editInd_form" class= "register" method="post">
<div class="first">
<fieldset>
<label for="title"><strong>Cause Title: </strong></label><br>
<input type="text" id="title" name="title" class="inputtext" value="<?php echo $title?>"/><br>
<label for="category" ><strong><span class="error-message" style="color:red">*</span>Category:</strong></label><br>
<select id="category" name="category" onchange="document.getElementById('editInd_form').submit()">
<?php
$categoryArray=array("Select a category", "Animal Welfare", "Children", "Education", "Environment", "Health", "OKU", "Refugees", "Senior Citizen", "Community", "Women Welfare", "Youth");
for ($i=0; $i<count($categoryArray); $i++){
if ($i == $category){
echo "<option value='".$i."' selected>".$categoryArray[$i]."</option>";
}
else{
echo "<option value='".$i."'>".$categoryArray[$i]."</option>";
}
}
?>
</select><br>
<label for="donateType"><strong><span class="error-message" style="color:red;">*</span>Type of Donation:</strong></label><br>
<select id="donateType" name="donateType" onchange="document.getElementById('editInd_form').submit()" >
<?php
$donateTypeArray=array("Please Select","Fundraising","Books","Clothing","Electric product", "Food","Water","Other");
for ($j=0; $j<count($donateTypeArray); $j++){
if ($j == $donateType){
echo "<option value='".$j."' selected>".$donateTypeArray[$j]."</option>";
}
else{
echo "<option value='".$j."'>".$donateTypeArray[$j]."</option>";
}
}
?>
</select><br>
<label for="goal"><strong><span class="error-message" style="color:red">*</span>Please state your goal:</strong></label><br>
<input type="text" id="goal" name="goal" class="inputtext" value="<?php echo $goal?>" placeholder="enter an amount(RM) for fundraising, uniform, 1.5L mineral water, ..."><br>
</fieldset></div>
<div><fieldset>
<label for="description"><strong>Tell us your story: </strong></label><br>
<textarea name="description" style="width:350px;height:150px;"><?php echo $description?>
</textarea><br>
<!-- <img src="images/image-icon.png" class="image-icon" height="150" width="150"> <img src="images/video-icon.png" height="150" width="150">-->
<label for="imageToUpload"><strong>Upload Your Photo:</strong></label><br>
<input type="file" name="imageToUpload" id="imageToUpload" style="display:none;" onChange="Handlechange();">
<input type="button" value="Change image" id="fakeBrowse" onclick="HandleBrowseClick();"/>
<?php include 'upload1.php';?>
<input type="submit" name="submit" id="save" value=""><br>
</fieldset>
</div>
</form>
</table>
</div>
</div> <!--content end-->
<!--include footer-->
<?php include 'footer.php';?>
</body>
</html>
for your reference, this is my create form (2 pages form)
createIndividual.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<!--include header-->
<?php
session_start();
include 'dbconn.php';
if(!isset($_SESSION['email'])){
echo "Please login before proceed!";
header("location:login.php");
}
include 'header.php';
?>
<div id="content">
<div>
<h3 align="center"> Create your own cause</h3>
<h4><strong> Step 1: Title, Category, Goal</strong></h4>
<form action="createIndividual2.php" id="createInd_form" class= "register" method="post">
<div class="form">
<fieldset>
<label for="title"><strong>Cause Title: </strong></label><br>
<input type="text" id="title" name="title" class="inputtext"/><br>
<label for="category" ><strong><span class="error-message" style="color:red">*</span>Category:</strong></label><br>
<select id="category" name="category">
<option value="0"> Select a category</option>
<option value="1"> Animal Welfare</option>
<option value="2"> Children</option>
<option value="3"> Education </option>
<option value="4"> Environment</option>
<option value="5"> Health</option>
<option value="6"> OKU</option>
<option value="7"> Refugees</option>
<option value="8"> Senior Citizen</option>
<option value="9"> Community</option>
<option value="10"> Women Welfare</option>
<option value="11"> Youth</option>
</select><br>
<label for="donateType"><strong><span class="error-message" style="color:red;">*</span>Type of Donation:</strong></label><br>
<select id="donateType" name="donateType">
<option value="0">Please Select</option>
<option value="1">Fundraising</option>
<option value="2">Books</option>
<option value="3">Clothing</option>
<option value="4">Electric product</option>
<option value="5">Food</option>
<option value="6">Water</option>
<option value="7">Other</option>
</select><br>
<label for="goal"><strong><span class="error-message" style="color:red">*</span>Please state your goal:</strong></label><br>
<input type="text" id="goal" name="goal" class="inputtext" placeholder="enter an amount(RM) for fundraising, uniform, 1.5L mineral water, ..."><br>
<input type="submit" name="submit" id="next" value=""><br>
</fieldset>
</div>
</form>
</table>
</div>
</div> <!--content end-->
<!--include footer-->
<?php include 'footer.php';?>
</body>
</html>
createIndividual2.php
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<!--include header-->
<?php
session_start();
$_SESSION['title'] = $_POST['title'];
$_SESSION['category'] = $_POST['category'];
$_SESSION['donateType'] = $_POST['donateType'];
$_SESSION['goal'] = $_POST['goal'];
include 'header.php';
?>
<div id="content">
<div>
<h3 align="center"> Create your own cause</h3>
<h4><strong> Step 2: Tell us your story</strong></h4>
<form action="checkCause.php" id="createIndividual" class= "register" method="post" enctype="multipart/form-data">
<div class="form">
<fieldset>
<label for="title"><strong>Tell us your story: </strong></label><br>
<textarea name="description" style="width:500px;height:150px;">
</textarea><br>
<!-- <img src="images/image-icon.png" class="image-icon" height="150" width="150"> <img src="images/video-icon.png" height="150" width="150">-->
<label for="imageToUpload"><strong>Upload Your Photo:</strong></label><br>
<input type="file" name="imageToUpload" id="imageToUpload">
<input type="submit" name="upload" id="submit" value=""><br>
<button onclick="goBack()" id="back"></button>
<script>
function goBack() {
window.history.back();
}
</script>
<?php include 'upload1.php';?>
</fieldset>
</div>
</form>
</table>
</div>
</div> <!--content end-->
<?php include 'footer.php';?> <!--include footer.php-->
</body>
</html>
In editIndividual.php you have a error. You first get data from MySQL and after that you setup data from sessions. But where you check if new data exist and return old if not exists?
Look my integration in your code:
<html>
<head>
<meta charset="UTF-8" />
<!--include header-->
<?php
session_start();
include 'dbconn.php';
include 'header.php';
if(!isset($_SESSION['email'])){
echo "Please login before proceed!";
header("location:login.php");
}
else{
if(isset($_GET['u'])){
$causeID = $_GET['u'];
$query="SELECT * from cause where causeID=$causeID ";
$result=mysqli_query($conn, $query);
while ($row=mysqli_fetch_array($result)){
$causeID = $row['causeID'];
$title = $row['title'];
$category = $row['category'];
$donateType = $row['donateType'];
$goal = $row['goal'];
$description = $row['description'];
$createDate = $row['createDate'];
$image = $row['image'];
}
//update title
$title = (isset($_SESSION['title'])&&!empty($_SESSION['title'])?$_SESSION['title']:$title);
$upTitle = "UPDATE cause set title='$title' where causeID='$causeID'";
$upTitleResult = mysqli_query($conn,$upTitle);
//update category
$category = (isset($_SESSION['category'])&&!empty($_SESSION['category'])?$_SESSION['category']:$category);
$upCategory = "UPDATE cause set category='$category' where causeID='$causeID'";
$upCategoryResult = mysqli_query($conn,$upCategory);
//update donate type
$donateType = (isset($_SESSION['donateType'])&&!empty($_SESSION['donateType'])?$_SESSION['donateType']:$donateType);
$upDonateType = "UPDATE cause set donateType='$donateType' where causeID='$causeID'";
$upDonateTypeResult = mysqli_query($conn,$upDonateType);
//update goal
$goal = (isset($_SESSION['goal'])&&!empty($_SESSION['goal'])?$_SESSION['goal']:$goal);
$upGoal = "UPDATE cause set goal='$goal' where causeID='$causeID'";
$upGoalResult = mysqli_query($conn,$upGoal);
//update description
$description = (isset($_POST['description'])&&!empty($_POST['description'])?$_POST['description']:$description);
$upDes = "UPDATE cause set description='$description' where causeID='$causeID'";
$upDesResult = mysqli_query($conn,$upDes);
//update image
$image = (isset($_FILES['imageToUpload']['name']) && !empty($_FILES['imageToUpload']['name'])?$_FILES['imageToUpload']['name']:$image);
$upImage = "UPDATE cause set image='$image' where causeID='$causeID'";
$upImageResult = mysqli_query($conn,$upImage);
}
}
?>
<!--Change choose file button default name-->
<script>
function HandleBrowseClick()
{
var fileinput = document.getElementById("imageToUpload");
fileinput.click();
}
function Handlechange()
{
var fileinput = document.getElementById("imageToUpload");
fileinput.value;
}
</script>
<div id="content">
<div>
<form action="activityLog.php" id="editInd_form" name="editInd_form" class= "register" method="post">
<div class="first">
<fieldset>
<label for="title"><strong>Cause Title: </strong></label><br>
<input type="text" id="title" name="title" class="inputtext" value="<?php echo $title?>"/><br>
<label for="category" ><strong><span class="error-message" style="color:red">*</span>Category:</strong></label><br>
<select id="category" name="category" onchange="document.getElementById('editInd_form').submit()">
<?php
$categoryArray=array("Select a category", "Animal Welfare", "Children", "Education", "Environment", "Health", "OKU", "Refugees", "Senior Citizen", "Community", "Women Welfare", "Youth");
for ($i=0; $i<count($categoryArray); $i++){
if ($i == $category){
echo "<option value='".$i."' selected>".$categoryArray[$i]."</option>";
}
else{
echo "<option value='".$i."'>".$categoryArray[$i]."</option>";
}
}
?>
</select><br>
<label for="donateType"><strong><span class="error-message" style="color:red;">*</span>Type of Donation:</strong></label><br>
<select id="donateType" name="donateType" onchange="document.getElementById('editInd_form').submit()" >
<?php
$donateTypeArray=array("Please Select","Fundraising","Books","Clothing","Electric product", "Food","Water","Other");
for ($j=0; $j<count($donateTypeArray); $j++){
if ($j == $donateType){
echo "<option value='".$j."' selected>".$donateTypeArray[$j]."</option>";
}
else{
echo "<option value='".$j."'>".$donateTypeArray[$j]."</option>";
}
}
?>
</select><br>
<label for="goal"><strong><span class="error-message" style="color:red">*</span>Please state your goal:</strong></label><br>
<input type="text" id="goal" name="goal" class="inputtext" value="<?php echo $goal?>" placeholder="enter an amount(RM) for fundraising, uniform, 1.5L mineral water, ..."><br>
</fieldset></div>
<div><fieldset>
<label for="description"><strong>Tell us your story: </strong></label><br>
<textarea name="description" style="width:350px;height:150px;"><?php echo $description?>
</textarea><br>
<!-- <img src="images/image-icon.png" class="image-icon" height="150" width="150"> <img src="images/video-icon.png" height="150" width="150">-->
<label for="imageToUpload"><strong>Upload Your Photo:</strong></label><br>
<input type="file" name="imageToUpload" id="imageToUpload" style="display:none;" onChange="Handlechange();">
<input type="button" value="Change image" id="fakeBrowse" onclick="HandleBrowseClick();"/>
<?php include 'upload1.php';?>
<input type="submit" name="submit" id="save" value=""><br>
</fieldset>
</div>
</form>
</table>
</div>
</div> <!--content end-->
<!--include footer-->
<?php include 'footer.php';?>
</body>
</html>
<?php
function VerifyForm(&$values, &$errors)
{
if (strlen($values['fname']) == 0)
$errors['fname'] = 'Enter First Name';
if (strlen($values['lname']) == 0)
$errors['lname'] = 'Enter Last Name';
if (strlen($values['mname']) == 0)
$errors['mname'] = 'Enter Middle Name';
if (strlen($values['address']) == 0)
$errors['address'] = 'Enter Address';
if (strlen($values['terms']) == 0)
$errors['terms'] = 'Please Read Terms and Agreement and Check the box.';
if (!ereg('.*#.*\..{2,4}', $values['email']))
$errors['email'] = 'Email address invalid';
else if (strlen($values['email']) < 0)
$errors['email'] = 'Enter Email Address';
return (count($errors) == 0);
}
function DisplayForm($values, $errors)
{
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>GIA Soap » Products » Customer Informations</title>
<link href="stylesheet/style.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js_files/jquery.js"></script>
<script type="text/javascript" src="js_files/sliding_effect.js"></script>
<script type="text/javascript" src="js_files/slideshow.js"></script>
</head>
<body>
<div class="bg_top">
<div class="bg_bottom">
<div class="wrapper">
<div class="header">
<div class="logo">
</div>
<div class="logo_text">
<div class="logo_head_text">Gia Soap Making</div>
<div class="logo_sub_text">Sub text here</div>
</div>
</div>
<div class="h_nav">
<div class="h_nav_dash">
</div>
</div>
<div class="container">
<div class="content_term">
<div class="content_terms">
<br />
<h1><p>Customer Information</p></h1><br />
<p>Please the following correctly.</p>
<div class="customer_info">
<?php
if (count($errors) > 0)
echo "<p>There were some errors in your submitted form, please correct them and try again.</p>";
?>
<form method="post" action="<?= $_SERVER['PHP_SELF'] ?>">
<!-- hidden values -->
<input type="hidden" value="<?php echo $papaya; ?>" name="papaya" />
<input type="hidden" value="<?php echo $carrot; ?>" name="carrot" />
<input type="hidden" value="<?php echo $guava; ?>" name="guava" />
<label for="customer_fname">First Name (<i>Required</i>)</label>
<input type="text" class="textbox" id="customer_fname" name="customer_fname" value="<?= htmlentities($values['fname']) ?>" />
<span class="error_msg"><?= $errors['fname'] ?></span>
<label for="customer_lname">Last Name (<i>Required</i>)</label>
<input type="text" class="textbox" id="customer_fname" name="customer_fname" value="<?= htmlentities($values['lname']) ?>" />
<span class="error_msg"><?= $errors['lname'] ?></span>
<label for="customer_mname">Middle Name (<i>Required</i>)</label>
<input type="text" class="textbox" id="customer_fname" name="customer_fname" value="<?= htmlentities($values['mname']) ?>" />
<span class="error_msg"><?= $errors['mname'] ?></span>
<label for="customer_add">Address (<i>Required : Complete Address Please</i>)</label>
<input type="text" class="textbox" id="customer_add" name="customer_add1" value="<?= htmlentities($values['address']) ?>" /><br />
<input type="text" class="textbox" id="customer_add" name="customer_add2" /><br />
<input type="text" class="textbox" id="customer_add" name="customer_add3" />
<span class="error_msg"><?= $errors['address'] ?></span>
<label for="customer_email">Email Address (<i>Required</i>)</label>
<input type="text" class="textbox" id="customer_email" name="customer_email" value="<?= htmlentities($values['email']) ?>" />
<span class="error_msg"><?= $errors['email'] ?></span>
<label for="customer_phone">Phone Number </label>
<input type="text" class="textbox" id="customer_phone" name="customer_phone" />
<label for="customer_mobile">Mobile Number </label>
<input type="text" class="textbox" id="customer_mobile" name="customer_mobile" />
<br /><br />
<div class="terms">
<center>
<h1>Terms and Agreement</h1><br />
<p>Please read the following.</p><br />
</div>
<br />
<input type="checkbox" name="terms" value="<?= htmlentities($values['terms']) ?>" /> I Read the Terms and Agreement<br /><br />
<span class="error_msg"><?= $errors['terms'] ?></span>
<input type="submit" value="Send Order" class="prod_subbtn" />
</center>
</form>
</div>
</div>
</div>
<div class="clear"></div>
</div>
<?php include ('includes/footer.php'); ?>
</div>
</div>
</div>
</body>
</html>
<?php
}
function ProcessForm($values)
{
$papaya = $_POST['papaya'];
$carrot = $_POST['carrot'];
$guava = $_POST['guava'];
$fname = $_POST['fname'];
$lname = $_POST['lname'];
$mname = $_POST['mname'];
$address = $_POST['address'];
}
if ($_SERVER['REQUEST_METHOD'] == 'POST')
{
$formValues = $_POST;
$formErrors = array();
if (!VerifyForm($formValues, $formErrors))
DisplayForm($formValues, $formErrors);
else
ProcessForm($formValues);
}
else
DisplayForm(null, null);
?>
The output is:
Problem
The PHP code that is supposed to put in the field values can be seen by users.
Chances are short_open_tags is off. Use <?php echo ...; ?> instead of <?=... ?>, like this:
<?php echo htmlentities($values['lname']); ?>
<?= $errors['fname'] ?> is equal to <?php echo $errors['fname'] ?>.
<?= are called 'short tags', which were removed (deprecated) from php.
Use <?php echo $errors['fname']; ?> to see the actual variable value.
The directive short tags is set to off in the php.ini. That disallows <? $phpcode ?> and <?=$monkey?>
The only one allowed is <?php $monkeybusiness ?>
either change <?= to <?php echo or turn short_open_tags = on in the php.ini