I got undefined index while upload file - php

Where is my mistake in php code or html? I want upload file .jpeg or .pdf in my db. All field in form were saved except the ile that i want to upload.
So here is my php code,
<?php
if(isset($_POST['new']))
{
$host="xxx";//host name
$username="xxx"; //database username
$word="";//database word
$db_name="xxx";//database name
$tbl_name="doc"; //table name
$con=mysqli_connect("$host", "$username", "$word","$db_name")or die("cannot connect");//connection string
$title =$_REQUEST['title'];
$date = $_REQUEST['date'];
$from_to = $_REQUEST['from_to'];
$details = $_REQUEST['details'];
$d_location = $_REQUEST['d_location'];
$d_stat = $_REQUEST['d_stat'];
$upfile = $_REQUEST['upfile'];
$in_ch=mysqli_query($con,"insert into doc(`title`,`date`,`from_to`,`details`,`d_location`,`d_stat`,`upfile`) values ('$title','$date','$from_to','$details','$d_location','$d_stat','$upfile')");
if($in_ch==1)
{
echo'<script>alert("Inserted Successfully")</script>';
}
else
{
echo'<script>alert("Failed To Insert")</script>';
}
}
?>
and this is my html form
<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="new" value="1" />
<p><input type="text" name="title" placeholder="Fail Title" required />
<input type="date" name="date" placeholder="Date" required /></p>
<p><input type="text" name="from_to" placeholder="From/To by" required /></p>
<p> Details: <br><textarea name="details" required></textarea></p>
<p>Location: <select name="d_location">
<option value="Local A">Local A</option>
<option value="local B">Local B</option>
<option value="local C">Local C</option>
</select></p>
<p>Status: <select name="d_stat">
<option value="Active">Active</option>
<option value="Inactive">Inactive</option>
</select></p>
<p>Choose file to upload: <input type="file" name ="upfile" required/></p>
<input name="submit" type="submit" value="Create new" />

Related

Fill HTML5 input field with current date + 14 days

The user is making a coin submission and when they go to do that I want the date to load into a input field that I'm using to track the time the submission was made. That date will be recorded in a mySQL database. The cut off date is set as the coming bimonthly Friday (eg cut off dates are Jun 14 and Jun 28. if the submission is done today, then cut off date is Jun 28. if the submission is done on Jun 30, the cut off date would be Jul 12. I included the entire form so you could get a bigger picture. Feel free to make adjustments. Thanks any help is greatly appreciated.
I'm able to call a javascript function and have the user select the date from a calendar, but that's not what I need. Javascript - get a date from an HTML input but I'm not sure how to concatenate the +14 days for the next cut off date.
CoinSubmission.html
<form action="AdminCoinSub_Code.php" method="POST">
<h1 id="litheader">Coin Submission</h1>
<div class="inset">
<input type="text" list="Store" name="Store" placeholder="Store">
<datalist id="store">
<option value="Causeway Bay">
<option value="Wan Chai">
<option value="Lai Chi Kok">
<option value="Tai Po">
</datalist>
<input type="text" list="Position" name="Position" placeholder="Position">
<datalist id="position">
<option value="1">
<option value="2">
<option value="3">
<option value="4">
</datalist>
<p>
<input type="text" name="Nickame" id="Nickname" placeholder="Nickname">
</p>
<p>
<input type="text" name="Contact" id="Contact" placeholder="Contact">
</p>
<p>
<input type="text" name="MachineCount" id="Machine Count" placeholder="Machine Count">
</p>
<p>
<input type="text" name="CutOffDate" id="CutOffDate" placeholder="Cut Off Date">
</p>
<p>
<input type="text" name="Coins" id="Coins" placeholder="Coins">
</p>
<p>
<input type="file" type="text" name="location" accept="image/*">
<div class="btnConfirm">
<input class="loginLoginValue" type="hidden" name="" value="" />
</div>
</div>
<div class="btnConfirm">
<input type="submit" onclick="location.href='CoinSubmission.php';" name="Submit" value="Confirm">
</div><br><br>
<div class="wrapper2">
<nav>
<ul>
<li>SUBMISSION</li>
<li>OCCUPANCY</li>
<li>ANALYTICS</li>
<li>SEARCH</li>
</ul>
</nav>
</div>
</form>
AdminCoinSub_Code.php
<?php {
$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "administrator_logins";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// prepare sql and bind parameters
$stmt = $conn->prepare("INSERT INTO admincoinsubmission (Store, Position, Nickname, Contact, MachineCount, CutOffDate, Coins, location)
VALUES ('$_POST[Store]','$_POST[Position]','$_POST[Nickame]','$_POST[Contact]','$_POST[MachineCount]','$_POST[CutOffDate]','$_POST[Coins]','$_POST[location]')");
$stmt->bindParam(':Store', $Store);
$stmt->bindParam(':Position', $Position);
$stmt->bindParam(':Nickname', $Nickname);
$stmt->bindParam(':Contact', $Contact);
$stmt->bindParam(':MachineCount', $MachineCount);
$stmt->bindParam(':CutOffDate', $CutOffDate);
$stmt->bindParam(':Coins', $Coins);
$stmt->bindParam(':location', $location);
$stmt->execute();
echo "Success";
}
catch(PDOException $e)
{
echo "Error: " . $e->getMessage();
}
$conn = null;
}
?>
When the page loads the date row in the coin submission form should display the cutoffdate.
date = (current date + cutoffdate)
You can achieve the following task using similar code -
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>Date +14 days</title>
</head>
<body>
<input type="date" name="date1" id="date1" value="" />
<input type="text" name="date2" id="date2" value="" />
<script type="text/javascript">
var date1 = document.getElementById("date1");
date1.addEventListener('change', function(){
tempDate = new Date(date1.value);
finalDate = tempDate.setDate(tempDate.getDate() + 14);
console.log(new Date(finalDate));
});
</script>
</body>
</html>
To achieve expected result, use below option of add time with getTime() method and add 14 days
var currentDate = new Date(new Date().getTime()+(14*24*3600000))
document.getElementById('CutOffDate').value = (currentDate.getDate()) +'/' + (currentDate.getMonth()+1) +'/'+ currentDate.getFullYear()
Working code sample for reference
var currentDate = new Date(new Date().getTime()+(14*24*3600000))
document.getElementById('CutOffDate').value = (currentDate.getDate()) +'/' + (currentDate.getMonth()+1) +'/'+ currentDate.getFullYear()
<form action="AdminCoinSub_Code.php" method="POST">
<h1 id="litheader">Coin Submission</h1>
<div class="inset">
<input type="text" list="Store" name="Store" placeholder="Store">
<datalist id="store">
<option value="Causeway Bay">
<option value="Wan Chai">
<option value="Lai Chi Kok">
<option value="Tai Po">
</datalist>
<input type="text" list="Position" name="Position" placeholder="Position">
<datalist id="position">
<option value="1">
<option value="2">
<option value="3">
<option value="4">
</datalist>
<p>
<input type="text" name="Nickame" id="Nickname" placeholder="Nickname">
</p>
<p>
<input type="text" name="Contact" id="Contact" placeholder="Contact">
</p>
<p>
<input type="text" name="MachineCount" id="Machine Count" placeholder="Machine Count">
</p>
<p>
<input type="text" name="CutOffDate" id="CutOffDate" placeholder="Cut Off Date">
</p>
<p>
<input type="text" name="Coins" id="Coins" placeholder="Coins">
</p>
<p>
<input type="file" type="text" name="location" accept="image/*">
<div class="btnConfirm">
<input class="loginLoginValue" type="hidden" name="" value="" />
</div>
</div>
<div class="btnConfirm">
<input type="submit" onclick="location.href='CoinSubmission.php';" name="Submit" value="Confirm">
</div><br><br>
<div class="wrapper2">
<nav>
<ul>
<li>SUBMISSION</li>
<li>OCCUPANCY</li>
<li>ANALYTICS</li>
<li>SEARCH</li>
</ul>
</nav>
</div>
</form>
codepen - https://codepen.io/nagasai/pen/VJWmNE?editors=1010

inserting data to MySQL using a form and php

I am currently trying to get form data to insert to a MySQL database using a form and php. The form is not submitting the data and I am not sure if there is an issue with my code or there is something in my database. I have checked numerous times that all the code matches correctly in the database as well as validating my code with no errors. Is there something simple that i have missed?
<?php
$mysqli = new mysqli("localhost", "root", "", "etrading");
/* check connection */
if ($mysqli->connect_errno) {
printf("Connect failed: %s\n", $mysqli->connect_error);
exit();
}
if(isset($_POST['submit'])) {
$key=$_POST['ItemID'];
$name= $_POST['Name'];
$description= $_POST['Description'];
$img_path= $_POST['img_path'];
$quantity= $_POST['Quantity'];
$category= $_POST['Category'];
$location= $_POST['Location'];
$saletype= $_POST['Saletype'];
$price= $_POST['Price'];
$duration= $_POST['Duration'];
$payment= $_POST['Payment'];
$query = "INSERT INTO item (ItemID, Name, Description,img_path, Quantity, Category, Location, Saletype, Price,Duration,Payment) VALUES ('$key','$name','$description','$img_path','$quantity','$category','$location','$saletype','$price','$duration','$payment',)";
if (mysqli_query($mysqli, $query)) {
echo "New record created successfully";
} else {
echo "Error: " . $query . "<br>" . mysqli_error($mysqli);
}
}
/* close connection */
$mysqli->close();
?>
I have also set the ItemID to auto increment in the database
And this is my form code that i am using.
<form id="sellitem" action="sellitem.php" method="POST" >
<fieldset>
<h4>Sell Your Item</h4>
<p><label class="title" for="Name">Name:</label>
<input type="text" placeholder="Enter item name" name="Name" id="Name" title="Please enter item name"
><br />
<label class="title" for="Description">Description:</label>
<textarea name="Description" rows="5" cols="33" placeholder="Please describe your item" id="Description" title="Please describe your item" ></textarea><br />
Select image to upload:
<input type="file" name="img_path" id="img_path" ><br>
<label class="title" for="Quantity">Quantity:</label>
<input type="text" placeholder="Number of items" name="Quantity" id="Quantity" title="Number of items" ><br />
<label class="title" for="Category">Category:</label>
<select name="Category" id="Category">
<option value="clothes">Clothes</option>
<option value="books">Books</option>
<option value="electronics">Electronics</option>
<option value="sport">Sport</option>
</select></p>
<label class="title" for="Location">Location:</label>
<input type="text" placeholder="Item Location" name="Location" id="Location" title="Enter item location" ><br />
<label class="title" for="Saletype">Sale Type:</label>
<select name="Saletype" id="Saletype" >
<option value="Auction">Auction</option>
<option value="BuyNow">Buy Now</option>
</select>
<label class="title" for="Price">Price: $</label>
<input type="text" placeholder="00.00" name="Price" id="Price" title="Please enter your name" ><br />
<label class="title" for="Duration">Duration:</label>
<input type="text" placeholder="End date" name="Duration" id="Duration" title="End Date" ><br />
<label class="title" for="Payment">Payment Type:</label>
<select name="Payment" id="Payment" >
<option value="PayPal">PayPal</option>
<option value="Bank Deposit">Bank Deposit</option>
<option value="Card">Credit Card</option>
</select><br>
<div class="submit"><input type="submit" value="submit" /></div>
<div class="reset"><input type="reset" /></div>
</fieldset>
</form>
Change this line of your code from
<input type="submit" value="submit" />
to
<input type="submit" value="submit" name="submit" />
You are not entering if(isset($_POST['submit'])) {
Add the name attribute to the submit button
<input type="submit" value="submit" name="submit" />
1.Every input field should have name attribute to POST/GET data.
<input type="submit" value="submit" />
to
<input type="submit" value="submit" name="submit" />
2.Due to obscene of name attribute of type="submit" field form posting only
Array
(
[Name] => dsfdsf
[Description] => dfdsf
[img_path] =>
[Quantity] =>
[Category] => clothes
[Location] =>
[Saletype] => Auction
[Price] =>
[Duration] =>
[Payment] => PayPal
)
3.Below query added one extra comma
$query = "INSERT INTO item (ItemID, Name, Description,img_path, Quantity, Category, Location, Saletype, Price,Duration,Payment) VALUES ('$key','$name','$description','$img_path','$quantity','$category','$location','$saletype','$price','$duration','$payment',)";

select tag name not working while using it in php

I have a form in html and I am saving it as search.php:
<form name="myform" action="" method="POST" onsubmit="search_clicked(); return false;">
Keyword<input type="text" name="Keyword" id="Keyword" value="XYZ" required/><!-- value is the default name that appears in the text box-->
<br>
Type
<select name="sel" id="sel" class="form-control" onchange="checkcolors(this.value)">
<option selected value="Users">Users</option>
<option value="Pages">Pages</option>
<option value="Events">Events</option>
<option value="Places">Places</option>
<option value="Groups">Groups</option>
</select>
</form>
<div id="loc_dist_displayarea" style="display:none;">
Location<input type="text" name="Location" value="90007" required/> Distance(meters)<input type="text" name="distance" value="10000" required/>
</div>
<br><br>
<input type="submit" name="Search"/>
<input type="submit" value="clear" id="clear" onclick="return clearclicked()"/>
</form>
and my php script is in the same file:
<div id="body_area" style="display:none">
<?php
echo "hi I am searching ";
if($_SERVER["REQUEST_METHOD"]=="POST")
{
//echo "yes value is selected";
//echo $_POST["Keyword"];
if (isset($_POST['sel'])) {
$selectedval= $_POST["sel"];
echo "$selectedval";
}
//echo $_POST["Location"];
}
echo "no value is selected";
?>
</div>
I am not able to display the $_POST['sel'] while $_POST['Keyword'] is echoed.Please help.
First of all, you arent using good programming practices, you use quotation marks (these " and these ') Indiscriminately. You should only alternate between them when you have them nested.
Next, on the action paramenter you should put the name of the file, even if it's the same.

why am i getting undefined indexs when i use enctype="multipart/form-data"

why am I getting undefined index's with my form is it because of the encoding type I am using, if so what can I do to fix this to properly post my variables
<form enctype="multipart/form-data" name="pmForm" id="pmForm" method="post" action="personalspage.php"><br>
<b>Age</b> <input type="text" name="age" id="age" cols="4"><br><br>
<b>University</b> <select name="university" id="university" onfocus="emptyElement('status')">
<option disabled selected>select one...</option>
<option value="Algoma">Algoma University</option>
<option value="york">York University</option>
</select><br><br>
<b>Headline</b> <input type="text" name="headline" id="headline"><br><br>
<b>Message</b> <textarea name="message" id="message" rows="6" cols="50"></textarea><br><br>
<b>Add a picture</b> <input type="file" name="photo" id="photo" accept="image/*"><br><br>
<input type="hidden" name="mysex" id="mysex" value="<?php echo $_POST["mysex"]; ?>">
<input type="hidden" name="lookingfor" id="lookingfor" value="<?php echo $_POST["lookingfor"]; ?>">
<center><input type="submit" name="adSubmit" id="adSubmit" value="Post It"></center>
</form>
I know that the variables being posted from say page1 to this form are coming through because I have an if statement with an isset() for the variables making it header to another page if there not set. this form code is from page2
im using this code on page3 to recieve the form data
$mysex = $_POST['mysex'];
$lookingfor = $_POST['lookingfor'];
$uni = $_POST['university'];
So when I post all the variable from this form to another page I get
Notice: Undefined index: mysex in C:\xampp\htdocs\Website\personalspage.php on line 4
Notice: Undefined index: lookingfor in C:\xampp\htdocs\Website\personalspage.php on line 5
Notice: Undefined index: university in C:\xampp\htdocs\Website\personalspage.php on line 6
I double checked and made sure that all my methods are using post, the only thing I can think of why this isnt working is because of some sort of combination of echoing input values and the enctype. If anyone could help me out it would be greatly appreciated.
Undefined variable university means you didnt select any options to fix that you can set any of the ptions to selected and for hidden variables problem is value is not there. fixed code is here
<form enctype="multipart/form-data" name="pmForm" id="pmForm" method="post" action="personalspage.php"><br>
<b>Age</b> <input type="text" name="age" id="age" cols="4"><br><br>
<b>University</b> <select name="university" id="university" onfocus="emptyElement('status')">
<option disabled selected value="" selected>select one...</option>
<option value="Algoma">Algoma University</option>
<option value="york">York University</option>
</select><br><br>
<b>Headline</b> <input type="text" name="headline" id="headline"><br><br>
<b>Message</b> <textarea name="message" id="message" rows="6" cols="50"></textarea><br><br>
<b>Add a picture</b> <input type="file" name="photo" id="photo" accept="image/*"><br><br>
<input type="hidden" name="mysex" id="mysex" value="<?php if(isset($_POST["mysex"])) echo $_POST["mysex"];else echo ""; ?>">
<input type="hidden" name="lookingfor" id="lookingfor" value="<?php if(isset($_POST["lookingfor"]))echo $_POST["lookingfor"];else echo ""; ?>">
<center><input type="submit" name="adSubmit" id="adSubmit" value="Post It"></center>
</form>
I Have Any Two option
First:
error_reporting(0);
Second:
<input type="hidden" name="mysex" id="mysex"
value="<?php if(isset($_POST["mysex"])){ echo $_POST["mysex"]; }?>">
<input type="hidden" name="lookingfor" id="lookingfor"
value="<?php if(isset($_POST["lookingfor"])){ echo $_POST["lookingfor"]; }?>">

Code works fine on it's own, but not when included in my form

I have 2 files: add.php (a form to add a recipe into a database, and numbers.php (a list for displaying ingredient options).
Both scripts/pages work separately...but when I include ('numbers.php') the onchange code does not work.
//numbers.php
<form name="numbers" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<select name="select" onchange="javascript: document.numbers.submit();">
<option value=0>-</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
</select>
</form>
<?php
$num_ingredients = $_REQUEST["select"];
$count = 0;
while ($count < $num_ingredients) {
$count++;
include ('../ingredients/list.php');
echo '<br />';
}
?>
//add.php
<form enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
Recipe Title:
<input type="text" name="recipe_name" maxlength="30" value="<?php echo $recipe_name; ?>" /><br />
Ingredients:
<?php include ('numbers.php'); ?><br />
<input type="submit" value="Add Recipe" />
<input type="button" onclick="history.go(0)" value="Clear" /><br />
</form>
Any help is greatly appreciated, as always :)
------- EDIT --------------------------------------
Here is the page source from add.php as requested
<!DOCTYPE html>
<html lang="en-US">
<head>
<meta http-equiv="Content Type" content="text/html; charset=UTF-8" />
<title>POWERCHEF</title>
<meta name="robots" content="noindex, nofollow">
<meta name="distribution" content="iu">
<meta name="format-detection" content="telephone=yes">
<meta name="copyright" content="Copyright 2012">
</head>
<body>
<!-- ON WITH THE SHOW --><h3>ADD RECIPE</h3><html>
<form enctype="multipart/form-data" action="/kitchen/recipes/add.php" method="POST">
Recipe Title:
<input type="text" name="recipe_name" maxlength="30"
value="" /><br />
Subtitle:
<input type="text" name="recipe_subtitle" maxlength="50"
value="" /><br />
Category:
<input type="text" name="recipe_category" maxlength="50"
value="" /><br />
Subcategory:
<input type="text" name="recipe_subcategory" maxlength="50"
value="" /><br />
<br />
Photo:<br />
<input type="hidden" name="MAX_FILE_SIZE" value="200000" />
<input type="file" name="recipe_photo" value="" /><br />
<br />
Ingredients:<br />
<form name="numbers" method="post"
action="/kitchen/recipes/add.php">
<select name="select" onchange="javascript: document.numbers.submit();">
<option value=0>-</option>
<option value=1>1</option>
<option value=2>2</option>
<option value=3>3</option>
<option value=4>4</option>
<option value=5>5</option>
<option value=6>6</option>
<option value=7>7</option>
<option value=8>8</option>
<option value=9>9</option>
<option value=10>10</option>
<option value=11>11</option>
<option value=12>12</option>
<option value=13>13</option>
<option value=14>14</option>
<option value=15>15</option>
<option value=16>16</option>
<option value=17>17</option>
<option value=18>18</option>
<option value=19>19</option>
<option value=20>20</option>
<option value=21>21</option>
<option value=22>22</option>
<option value=23>23</option>
<option value=24>24</option>
<option value=25>25</option>
<option value=26>26</option>
<option value=27>27</option>
<option value=28>28</option>
<option value=29>29</option>
<option value=30>30</option>
</select>
</form>
<br />
<br />
Prep Instructions:<br />
<textarea name="recipe_prep_instructions" class="" cols="88" rows="15"
value="" /></textarea><br />
<br />
Cooking Instructions:<br />
<textarea name="recipe_prep_instructions" class="" cols="88" rows="15"
value="" /></textarea><br />
<br />
<input type="submit" value="Add Recipe" />
<input type="button" onclick="history.go(0)" value="Clear" /><br />
<br />
</form>
</html>
<footer>
<p>
<small><strong>© 2012 </strong> - All Rights Reserved</small>
</p>
</footer>
<!-- THANKS FOR STOPPING BY! -->
</body>
</html>
You have a typo:
<input type="text" name="recipe_name" maxlength="30" value="<?php echo $recipe_name"; ?>" /><br />
Should be: (notice the removal of the quote in the php block)
<input type="text" name="recipe_name" maxlength="30" value="<?php echo $recipe_name; ?>" /><br />
The quote could possibly be messing up the other script.
EDIT:
Ok, was this also just a copy paste typo?
<form name="numbers" method="post action="<?php echo $_SERVER['PHP_SELF']; ?>">
Should be:
<form name="numbers" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
EDIT2:
Ok, the problem is that the form "numbers" is inside the other form, so it's having trouble accesing it with document.numbers. To fix it you are going to need to access it a different way, such as giving it an id and using getElementById.
the form on add.php did not have a name

Categories