Having issues with multiple forms in one page in php - php

I issue i am having is that, I created two forms, it is A PROFILE PAGE one is for uploading user image to the database, while the other is for updating user basic information, like name, email etc. The issue i am having is that only one of the form is working, the other form stops working. here is my code.
<?php if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "pic_form") &&(($_POST['check'])=== 1)) {
$id=$_POST['monitorimg'];
move_uploaded_file($_FILES['pic']['tmp_name'],"imageupload/".$id);
$updateSQL = sprintf("UPDATE administrator SET pix=%s WHERE adminid=%s",
GetSQLValueString($_POST['monitorimg'], "text"),
GetSQLValueString($_POST['ids'], "int"));
mysqli_select_db($myconn,$database_myconn);
$Result1 = mysqli_query($myconn,$updateSQL) or die(mysqli_connect_error());
}
?>
<div class="box box-primary">
<div class="box-body box-profile">
<img class="profile-user-img img-responsive img-circle" width="68" height="68" src="imageupload/<?php echo $row_rslinks['adminid']; ?>"."" alt="profile picture">
<form action="<?php echo $editFormAction; ?>" name="pic_form" enctype="multipart/form-data" method="POST">
<input type="file" name="pic" class="btn-facebook" required>
<input type="submit" class="btn-dropbox" name="sub" id="sub" value="Upload">
<input type="hidden" name="ids" id="ids" value="<?php echo $row_rslinks['adminid'];?>">
<input type="hidden" name="monitorimg" id="monitorimg" value="<?php echo $row_rslinks['adminid'];?>">
<input type="hidden" name="MM_update" value="pic_form">
<input type="hidden" name="check" id="check" value="1">
</form>
<h3 class="profile-username text-center"></h3>
<p class="text-muted text-center"></p>
<!-----------This is the second Table code-------------->
<?php if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1") &&(($_POST['check'])=== 1)) {
$updateSQL = sprintf("UPDATE administrator SET password=%s, adminname=%s, email_id=%s, contactno=%s WHERE adminid=%s",
GetSQLValueString($_POST['pwd'], "text"),
GetSQLValueString($_POST['inputName'], "text"),
GetSQLValueString($_POST['inputEmail'], "text"),
GetSQLValueString($_POST['num'], "text"),
GetSQLValueString($_POST['stuid'], "int"));
mysql_select_db($database_myconn, $myconn);
$Result1 = mysql_query($updateSQL, $myconn) or die(mysql_error());
}
?>
<div class="tab-content">
<div class="active tab-pane" id="settings">
<form action=<?php echo $editFormAction; ?>"" name=form11" method=POSTT" enctype="application/x-www-form-urlencoded" class="form-horizontal" id="form1">
<div class="form-group" id="stuid">
<label for="stuid" class="col-sm-2 control-label">User ID</label>
<div class="col-sm-10">
<input name="stuid" type="text" class="form-control" id="stuid"
value="<?php echo $row_rslinks['adminid']; ?>" readonly>
</div>
</div>
<div class="form-group" id="inputeName">
<label for="inputName" class="col-sm-2 control-label">Name</label>
<div class="col-sm-10">
<input type="text" class="form-control" name="inputName" id="inputName"
value="<?php echo $row_rslinks['adminname']; ?>">
</div>
</div>
<div class="form-group">
<label for="inputEmail" class="col-sm-2 control-label" >Email</label>
<div class="col-sm-10">
<input type="email" class="form-control" name="inputEmail" id="inputEmail"
value="<?php echo $row_rslinks['email_id']; ?>">
</div>
</div>
<div class="form-group">
<label for="pwd" class="col-sm-2 control-label">Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="pwd" name="pwd" value="">
</div>
</div>
<div class="form-group">
<label for="rpwd" class="col-sm-2 control-label">Re-Password</label>
<div class="col-sm-10">
<input type="password" class="form-control" id="rpwd" name="rpwd" value="">
</div>
</div>
<div class="form-group">
<label for="num" class="col-sm-2 control-label">Mobile</label>
<div class="col-sm-10">
<input type="text" class="form-control" id="num" name="num" value="<?php echo $row_rslinks['contactno']; ?>">
</div>
</div><input type="hidden" name="check" id="check" value="2">
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Save</button>
</div>
</div>
Sorry guys for my long code....will appreciate your contribution. the second Table is not updating to my database..when the update button is pressed nothing happens. no error message is shown.

The problem and solution:
The main issue I see here is that your second form uses method="POSTT". Of course, there is no such method as POSTT so it will default to using the GET method instead. Remove the extra 'T' and it should fix the issue.
How to debug step by step:
I also recommend applying some common debugging techniques to trace through any future errors you may encounter.
Step 1: Check your PHP error logs and make sure they are reporting errors.
Step 2: Look at your resulting source code in the browser and run it through a validator such as https://validator.w3.org/
Step 3: If no errors were encountered above use lots of echo statements at various places throughout your program or at least in the problem areas. For example, if you had placed an echo statement such as:
echo "Second if block";
(I often go a bit overboard placing echo statements in my program just in case I am overlooking something. If this is the case, I'll usually just use number such as ...echo 7;...echo 8..., etc. before, after, and inside of each block of code)
inside of your second if block you would have noticed that it did not run. That would have clued you in to the fact that your if statement containing your $_POST variables evaluated to false. As you know it should have evaluated to true you know this is the source of your problem. You could then display the contents of your form variables as follows:
echo "<pre>";
echo "GET:";
print_r($_GET);
echo "POST:";
print_r($_POST);
echo "</pre>";
This would have shown you that the data is in your $_GET variable rather than your $_POST variable as you had expected.
Step 4: If your error is not detected in any of the previous steps, your last step is to verify that your SQL statement is correct. Try running it outside of PHP directly in your SQL environment and see if you get any errors. Also, set your PHP script to display SQL errors.
Before your site goes live make sure that any errors get sent only to the log file and not to the browser.

Related

Why I can't update the record in codeigniter?

I am getting this error,
A PHP Error was encountered
Severity: Notice
Message: Trying to get property 'post_title' of non-object.
I cannot get the form inputs to print on the page and cannot update. Please help!
So, I have tried to update each record and only artist users are able to do that. But I cannot figure out how to show the individual post on the form page. The records saves correctly and deletes but cannot update them.
Controller
function editpost($post_id)//Edit post page
{
if (!$this->session->Role =='member,artist')
{
redirect(base_url() . 'login');
}
$data['success'] = 0;
if( !empty($this->input->post('post_title')) ) {
$data['post_title'] = $this->input->post('post_title');
$data['post'] = $this->input->post('post');
$data['active'] = 1;
/* >> file attach */
View
<form action="<?= base_url()?>starter/editpost/" method="post" class="justify-
content-center" enctype='multipart/form-data'>
<div class="form-group">
<label class="sr-only">Title</label>
<input type="text" class="form-control" placeholder="Title"
name="post_title" value="<?php echo $post->post_title; ?>">
</div>
<div class="form-group">
<label class="sr-only">Description</label>
<textarea class="form-control" placeholder="Describe it" name="post" ><?
php echo $post->post; ?></textarea>
</div>
<div class="row py-4">
<div class="col-lg-6 mx-auto">
<!-- Upload image input-->
<!-- File Button -->
<div class="col-md-5">
<input name="main_img[]" type="file" accept="image/x-png,image/gif,image/jpeg">
</div>
<br>
<br>
<p style="padding-top: 15px"><span> </span><input class="submit" type="submit" name="update"
value="Publish" /></p>
</form>
use direct the variable because you are passing just values which you are posting from form.
//like :
//for post_title
<input type="text" class="form-control" placeholder="Title"
name="post_title" value="<?php echo $post_title; ?>">
// for description
textarea class="form-control" placeholder="Describe it" name="post" ><?
php echo $post; ?></textarea>
You don't need to pass the post data in controller you can get post data anywhere
<input type="text" class="form-control" placeholder="Title" name="post_title" value="<?php echo ($this->input->post('post_title')!='')?$this->input->post('post_title'):''; ?>">

Make File Upload Field Optional on Contact Form

I have a basic HTML/PHP web form. Everything works fine when I fill out all fields, except I want to make the file upload optional, not required. I have taken "required" off of the form html for the upload. I checked my handler.php and it does not say that field is required. I checked the formhandler.php and it says that attachments are null. I have looked through similar questions, but any of the more complex solutions I am probably implementing incorrectly. I've expended my knowledge of php looking for a solution. What am I missing?
Here is the html of the form:
<div class="row pb6">
<div class="col-md-6 offset-md-3">
<form role="form" method="post" id="reused_form" enctype="multipart/form-data" >
<div class="form-group">
<label for="name"> Name:</label>
<input type="text" class="form-control" id="name" name="name" required maxlength="50">
</div>
<div class="form-group">
<label for="email"> Email:</label>
<input type="email" class="form-control" id="email" name="email" required maxlength="50">
</div>
<div class="form-group">
<label for="tel"> Phone: </label>
<input type="tel" class="form-control" id="tel" name="tel" maxlength="50">
</div>
<div class="form-group">
<label for="name"> Message:</label>
<textarea class="form-control" type="textarea" name="message" id="message" placeholder="We want to hear all about your performance! Also, please include the date you need the music." maxlength="6000" rows="7"></textarea>
</div>
<div class="form-group">
<label for="file">File Upload (optional)</label>
<input type="file" class="form-control" id="image" name="image">
</div>
<button type="submit" class="btn btn-lg btn-success pull-right" id="btnContactUs">Post It! →</button>
</form>
<div id="success_message" style="width:100%; height:100%; display:none; "> <h3>Sent your message successfully!</h3> </div>
<div id="error_message" style="width:100%; height:100%; display:none; "><h3>Error</h3>We are very sorry. There was an error sending your form. Email us and we will send you a free demo.</div>
</div>
</div>
</div>
And here is the handler.php:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
/*
Tested working with PHP5.4 and above (including PHP 7 )
*/
require_once './vendor/autoload.php';
use FormGuide\Handlx\FormHandler;
$pp = new FormHandler();
$validator = $pp->getValidator();
$validator->fields(['name','email','tel'])->areRequired()->maxLength(50);
$validator->field('email')->isEmail();
$validator->field('message')->maxLength(6000);
if($_FILES['image']){ $pp->attachFiles(['image']); }
$pp->sendEmailTo('email'); //
echo $pp->process($_POST);
after code edit:
1)Chrome and Firefox will send form, Safari will not.
2)All browsers show a perpetual "Sending" message, and do not show a success or failure message for the form.
3) Forms send WITH attachments will not send the form with attachment to email, only the form alone.
You are trying to attach the image whether it exists or not with $pp->attachFiles(['image']);. You should first check if it does indeed exists and only attach it if it does like so:
if($_FILES['image']){
$pp->attachFiles(['image']);
}

Form posting does not work

I am having trouble to post a html form. I am posting one form and getting the post value to my variable and then I am post this form but this form is not posting.
HTML code:
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" autocomplete="off">
<div class="widget-box">
<div class="widget-title"> <span class="icon"> <i class="icon-user"></i> </span>
<h5>Amc details</h5>
</div>
<div class="widget-content">
<div class="controls controls-row">
<div class="control-group span3">
<label for="normal" class="control-label">Installation Date<span style="color:red">*</span></label>
<div class="controls">
<input type="text" id="amc-ins-date" data-date="01-02-2016" name="amc-ins-date" data-date-format="dd-mm-yyyy" class="datepicker span12" placeholder="Enter installation date">
</div>
</div>
<div class="control-group span3">
<label for="normal" class="control-label">Start Date<span style="color:red">*</span></label>
<div class="controls">
<input type="text" id="amc-start-date" data-date="01-02-2016" name="amc-start-date" data-date-format="dd-mm-yyyy" placeholder="Enter amc start date" class="datepicker span12 ins-date">
</div>
</div>
<div class="control-group span3">
<label class="control-label">End Date<span style="color:red">*</span></label>
<div class="controls">
<input type="text" id="amc-end-date" data-date="01-02-2016" name="amc-end-date" data-date-format="dd-mm-yyyy" placeholder="Enter amc end date" class="datepicker span12 ins-date">
</div>
</div>
<div class="control-group span3">
<label class="control-label">Amount<span style="color:red">*</span></label>
<div class="controls">
<input type="text" id="amc-amount" name="amc-amount" class="span12" placeholder="Enter amc amount">
</div>
</div>
</div>
</div>
<div class="form-actions">
<input style="float:right" type="submit" name="amc-installation" class="btn btn-success" value="Save">
</div>
</form>
PHP code:
// i have submitted a form here and its posted
// installation details
$mc_serial = $_POST['mc-serial'];
$mc_model = $_POST['mc-model'];
$contract_type = $_POST['contract_type'];
$no_of_copies = $_POST['no-of-copies'];
$spare_part = join(",",$_POST['spare-part']);
$eng_name = $_POST['eng-name'];
$review = $_POST['review'];
// check if the machine already exits
if(IsMachine($mc_serial,$con)){
echo msgIsMachine();
exit();
}
if($contract_type == 'AMC'){
require './forms/amc.php'; // this is the html i have shown above
} elseif ($contract_type == 'ASC') {
require './forms/asc.php';
} elseif ($contract_type == '4C') {
require './forms/4c.php';
} elseif ($contract_type == 'RENTAL') {
require './forms/rental.php';
} elseif ($contract_type == 'WARRANTY') {
require './forms/warranty.php';
}
if(isset($_POST['amc-installation']) && !empty($_POST['amc-installation'])){
echo "posted";
var_dump($_POST);($_POST);
}
The output of var_dump is NULL. I don't get any problem.
You echo the second form (during the script which responds to the submission of the first one), but then immediately check for values returned from it within the same script execution. You have to wait for the user to post the form back before you can check the submitted values. This would be a separate postback, and therefore a separate execution context for the PHP.
So the code to check the values from the second form needs to be in a separate section (or file) which is triggered by the submission of the second form.
There's no $POST in PHP you should use $_POST instead :
if(isset($_POST['amc-installation']) && !empty($_POST['amc-installation'])){
echo "posted";
var_dump($_POST);
}
NOTE : You should place the var_dump($_POST); inside the if statement, so it will be trrigered just after the submit.
Hope this helps.
if(isset($_POST['amc-installation']) && !empty($_POST['amc-installation'])){
echo "posted";
var_dump($_POST);
}
You should use $_POST not $POST.
Hope this will helps you :)

sql update not executing

Apologies if there is another feed with this same problem, I have tried different suggested solutions but I still get an error, and I cant see why!
I want to update a row in my table using a html form. I have populated the form with the existing values, and want to be able to edit those and update them when the form is submitted, but I am getting this error:
Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[HY093]: Invalid parameter number: parameter was not defined'
in
/Applications/XAMPP/xamppfiles/htdocs/love-deals/admin/update_offer.php:46
Stack trace: #0
/Applications/XAMPP/xamppfiles/htdocs/love-deals/admin/update_offer.php(46):
PDOStatement->execute(Array) #1 {main} thrown in
/Applications/XAMPP/xamppfiles/htdocs/love-deals/admin/update_offer.php
on line 46
Here is the php / sql code:
if(isset($_POST['update'])) {
$updateTitle = trim($_POST['title']);
$updateDesc = trim($_POST['desc']);
$updateRedeem = trim($_POST['redeem']);
$updateStart = trim($_POST['start']);
$updateExpiry = trim($_POST['expiry']);
$updateCode = trim($_POST['code']);
$updateTerms = trim($_POST['terms']);
$updateImage = trim($_POST['image']);
$updateUrl = trim($_POST['url']);
$updateSql = 'UPDATE codes SET (title,description,redemption,start,expiry,textcode,terms,image,url) = (:title,:description,:redeem,:start,:exp,:code,:terms,:image,:url) WHERE id=:offerid';
$update = $db->prepare($updateSql);
$update->execute(array(':title'=>$updateTitle,':description'=>$updateDesc,':redeem'=>$updateRedeem,':start'=>$updateStart,':exp'=>$updateExpiry,':code'=>$updateCode,':terms'=>$updateTerms,':image'=>$updateImage,':url'=>$updateUrl,':id'=>$offerID));
}
and the html form:
<form id="update_offer" class="col-md-6 col-md-offset-3" method="post" action="update_offer.php?id=<?php echo $offerID; ?>">
<div class="form-group col-md-12">
<label class="col-md-12" for="title">Title</label>
<input id="title" class="form-control col-md-12" type="text" name="title" placeholder="Offer Title" value="<?php echo $title; ?>" required>
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="desc">Description</label>
<textarea id="desc" class="form-control col-md-12" name="desc" placeholder="Description" value="<?php echo $desc; ?>"></textarea>
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="redeem">Redemption</label>
<input id="redeem" class="form-control col-md-12" type="text" name="redeem" placeholder="Where to redeem" value="<?php echo $redeem; ?>" required>
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="start">Start Date</label>
<input id="start" class="form-control col-md-12" type="date" name="start" value="<?php echo $startDate->format('Y-m-d'); ?>" min="<?php echo date('Y-m-d') ?>" max="2021-12-31" required>
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="expiry">Expiry Date</label>
<input id="expiry" class="form-control col-md-12" type="date" name="expiry" value="<?php echo $expDate->format('Y-m-d'); ?>" min="<?php echo date('Y-m-d') ?>" max="2021-12-31" required>
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="code">Code</label>
<input id="code" class="form-control col-md-12" type="text" name="code" placeholder="Code (if applicable)" value="<?php echo $code; ?>">
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="terms">Terms</label>
<textarea id="terms" class="form-control col-md-12" name="terms" placeholder="Terms & Conditions" value="<?php echo $terms; ?>" required></textarea>
</div>
<div class="form-group col-md-12">
<label class="col-md-12" for="url">Offer URL</label>
<input id="url" class="form-control col-md-12" type="text" name="url" placeholder="Offer URL (if applicable)" value="<?php echo $url; ?>">
</div>
<div class="form-group col-md-12">
<label class="col-md-8" for="image">Image <img src="../images/offers/<?php echo $image; ?>" alt="" style="width: 200px;" /></label>
<input id="image" class="form-control col-md-4" type="file" name="image">
</div>
<div class="form-group col-md-12 pull-right">
<button id="update" type="submit" name="update" class="btn btn-primary"><i class="glyphicon glyphicon-refresh"></i> Update</button>
</div>
</form>
what am i doing wrong?! Im still learning php etc, so please be gentle, any help is much appreciated.
First, you have wrong syntax for update statement, as other guys mentioned already, change:
UPDATE codes SET (title,description,redemption,start,expiry,textcode,terms,image,url) = (:title,:description,:redeem,:start,:exp,:code,:terms,:image,:url) WHERE id=:offerid
Into
UPDATE `codes`
SET `title` = :title,
`description` = :description,
`redemption` = :redeem,
`start` = :start
`expiry` = :expiry
`textcode` = :code
`terms` = :terms
`image` = :image
`url` = :url
WHERE `id` = :offerid
Learn more about the SQL Update syntax here.
Then, one thing more you have a mistake in execute(). Change your :id into :offerid like below:
$update->execute(array(
':title' => $updateTitle,
':description' => $updateDesc,
':redeem' => $updateRedeem,
':start' => $updateStart,
':exp' => $updateExpiry,
':code' => $updateCode,
':terms' => $updateTerms,
':image' => $updateImage,
':url' => $updateUrl,
':offerid' => $offerID
));
You are using wrong syntax of Update
It would be
$updateSql = "UPDATE codes SET title =:title,
description =:description,
redemption =:redeem,
start =:start,
expiry =:exp,
textcode =:code,
terms :=terms,image =:image,
url =:url
WHERE id=:id";// write id instead of offset because you are binding ':id'=>$offerID
Check http://dev.mysql.com/doc/refman/5.7/en/update.html
thanks for your replies. My original update syntax was actually as you mentioned, I had changed it when looking through some other solutions, and not changed it back, but either way, even with correct syntax, I still got the same error.
Looking through your replies, I can see that I have ':id'=> $offerID but have used :offerid in the sql code, which obviously needs to be updated, so thanks for pointing that out! Hopefully that will fix the problem...

Update SESSION variable

I have the following code in index.php:
<div class="done">
<b>Thank you<?php
echo $_SESSION['session_vname']." ";
echo $_SESSION['session_lname']."! </b><br><br>Email: ";
echo $_SESSION['session_email']."<br> Status: ";
echo $_SESSION['session_status']."<br>";
?>
</div>
and
<div class="form">
<form method="post" action="process.php">
<div class="element">
<label>First Name</label>
<input type="text" name="vname" class="text" />
</div>
<div class="element">
<label>Last Name</label>
<input type="text" name="lname" class="text" />
</div>
<div class="element">
<label>Email</label>
<input type="text" name="email" class="text" />
</div>
<div class="element">
<label>Status</label>
<input type="text" name="status" class="text" />
</div>
<div class="element">
<input type="submit" id="submit"/>
<div class="loading"></div>
</div>
</form>
</div>
I execute the form with an ajax submit so the index.php gets no refresh. When finished
gets a fadeOut and a fadeIn.
This works fine except for the following part:
When div class="form" is shown, it shows the session variable from the form before the last submit.
Example:
first input: session_vname = testa
first output $_SESSION['session_vname'] = nothing
second input: session_vname = overflow
second output $_SESSION['session_vname'] = testa
So finally the question:
How can I force the to update the session variables after form submitting also index.php is not reloaded?
PHP code is executed on server side, before the page is shown to the viewer. That means you need to update static elements with JavaScript if you don't want to refresh the whole page.
You cant update server side php sessions using javascript, you should be using cookies for that type of functionality

Categories