This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 7 years ago.
I get this error:
Notice: Undefined index: gender \form.php on line 26.please help..how solve this error..?
how get from form employee gender male or female and insert MySQL database
**my html code**
<form action="form.php" method="post">
<table border="1">
<tr>
<td align="center">Form Input Employees Data</td>
</tr>
<tr>
<td>
<table>
<form method="post" action="form.php">
<tr>
<td>Name</td>
<td><input name="employe_name" type="text" id="employe_name" size="20">
</td>
</tr>
<tr>
<td>Address</td>
<td><input name="employe_add" type="text" id="employe_add" size="40">
</td>
</tr>
<tr>
<td height="23">Gender</td>
<td>Male<input type="radio" name="gender"value="m" />
Female<input type="radio" name="gender"value="f"/>
</td></tr>
<td>
<td align="right"><input type="submit"
name="submit" value="Submit"> add</td>
</tr>
</table>
</td>
</tr>
</table></form>
And
my PHP code is...
<?php
//Open a new connection to the MySQL server
$mysqli = new mysqli('localhost','root','','employe');
//Output any connection error
if ($mysqli->connect_error) {
die('Error : ('. $mysqli->connect_errno .') '. $mysqli->connect_error);
}else{
echo"connection good";
}
if(isset($_POST['gender']))
{
$gender=$_POST['gender'];
echo("your are select" .$gender);
}
//get values from form
$employe_name=$_POST['employe_name'];
$employe_add=$_POST['employe_add'];
$gender=$_POST['gender'];
//insert data into mysql
$sql = "INSERT INTO employe_detail(employe_name,employe_add,gender) VALUES('$employe_name','$employe_add','$gender')";
print '<h3>Insert a record</h3>';
if($sql)
{
print 'Success! ID of last inserted record is : ' .$mysqli->insert_id .'<br />';
}else{
die('Error : ('. $mysqli->errno .') '. $mysqli->error);
}
$mysqli->close();
?>
please help me............
Change your code from
if(isset($_POST['gender']))
{
$gender=$_POST['gender'];
echo("your are select" .$gender);
}
//get values from form
$employe_name=$_POST['employe_name'];
$employe_add=$_POST['employe_add'];
$gender=$_POST['gender'];
To
$gender = "";
if(isset($_POST['gender']))
{
$gender=$_POST['gender'];
echo("your are select" .$gender);
}
//get values from form
$employe_name=$_POST['employe_name'];
$employe_add=$_POST['employe_add'];
gender will be assigned or will be left blank
Related
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
[Errors][1]
I posted the code that I edited, and there are new errors w/c i didn't understand
Notice: Undefined index: product_brand in
C:\xampp\htdocs\JGMarketing\Admin_Area\insert_product.php on line 152
Notice: Undefined index: product_price in
C:\xampp\htdocs\JGMarketing\Admin_Area\insert_product.php on line 153
Notice: Undefined index: product_images in
C:\xampp\htdocs\JGMarketing\Admin_Area\insert_product.php on line 158
Notice: Undefined index: product_images in
C:\xampp\htdocs\JGMarketing\Admin_Area\insert_product.php on line 159
<!DOCTYPE>
<?php
include("includes/db.php");
?>
<html>
<head>
<title>Inserting Product</title>
<script src="https://cloud.tinymce.com/stable/tinymce.min.js"></script>
<script>tinymce.init({ selector:'textarea' });</script>
</head>
<body bgcolor="red">
<form action="insert_product.php"method="post"enctype=
"multipart/form/data">
<table align="center" width="700" border="5" bgcolor="white" >
<tr>
<td colspan="8"><h2 style="text-align:center;font-family:Arial;">
Insert New Post Here</h2></td>
</tr>
<tr>
<td align="right"><b>Product Name:</b></td>
<td><input type="text" name= "product_name" size="60" required />
</td>
</tr>
<tr>
<td align="right"><b>Product Category:</b></td>
<td>
<select name="product_cat" required>
<option>Select Category</option>
<?php
global $con;
$get_cat = "select * from categories";
$run_cat = mysqli_query($con, $get_cat);
while ($row_cat=mysqli_fetch_array($run_cat)){
$cat_id= $row_cat['cat_id'];
$cat_title= $row_cat['cat_title'];
echo "<li><option>$cat_title</option></li>";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Brand:</b></td>
<td>
<select name="product_brand" required>
<option>Select Brand</option>
<?php
global $con;
$get_brands = "select * from brands";
$run_brands = mysqli_query($con, $get_brands);
while ($row_brands=mysqli_fetch_array($run_brands)){
$brands_id= $row_brands['brand_id'];
$brands_title= $row_brands['brand_title'];
echo "<li><option>$brands_title</option></li>";
}
?>
</select>
</td>
</tr>
<tr>
<td align="right"><b>Product Image:</b></td>
<td><input type="file" name= "product_image" required /></td>
</tr>
<tr>
<td align="right"><b>Product Price:</b></td>
<td><input type="currency" name= "product_price" required/></td>
</tr>
<tr>
<td align="right"><b>Product Description:</b></td>
<td><textarea name="product_desc" cols="20" rows="10"></textarea>
</td>
</tr>
<tr>
<td align="right"><b>Product Keywords:</b></td>
<td><input type="text" name= "product_keywords" size="50"
required /></td>
</tr>
<tr align="center">
<td colspan="7"><input type="Submit" name= "insert_post" value="Insert
Product Here"/></td>
</tr>
</table>
</body>
</html>
<?php
if(isset($_POST['insert_post'])){
//getting text data
$product_name = $_POST['product_name'];
$product_cat = $_POST['product_cat'];
$product_brands = $_POST['product_brand'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_desc'];
$product_keywords = $_POST['product_keywords'];
//getting image data
$product_image = $_FILES['product_images']['name'];
$product_image_tmp = $_FILES['product_images']['tmp_name'];
move_uploaded_file($product_image_tmp,"products_images/$product_image");
$insert_product = "insert into products
(product_cat,product_brands,product_name,product_price,product_desc,
product_img,product_keyword) values ('$product_cat','$product_brands'
,'$product_name','$product_price','$product_desc','$product_image'
,'$product_keywords')";
$run_product = mysqli_query($con,$insert_product);
if($run_product){
echo"<script>alert('Product Has been inserted')</script>";
echo"<script>window.open('insert_product.php','_self')</script>";
}
}
?>
You define your submit button name as "insert_post". But in if condition you take another name (if(isset($_POST['insert_product']))). Change the name.
try use like this...please make sure check your db connection and form action properly.
if(isset($_POST['insert_post'])){
//getting text data from fields
$product_name = $_POST['product_name'];
$product_cat = $_POST['product_cat'];
$product_brand = $_POST['product_brand'];
$product_price = $_POST['product_price'];
$product_desc = $_POST['product_desc'];
$product_keywords = $_POST['product_keywords'];
//getting image from fields
$product_image = $_FILES['product_image']['name'];
$product_image_tmp = $_FILES['product_image']['tmp_name'];
move_uploaded_file($product_image_tmp,"product_images/$product_image");
$insert_product="insert into products (product_cat,product_brand,product_name,product_price,product_desc,product_image,product_keywords) values('$product_cat','$product_brand','$product_name','$product_price','$product_desc','$product_image','$product_keywords')";
$insert_pro = mysqli_query($con, $insert_product);
if($insert_pro){
echo"alert('Product Has been inserted')";
echo"window.open('insert_product.php','_self')";
}
else{
echo "Something has gone wrong";
}
}
Your Query IS wrong:
$insert_product="insert into products
(product_cat,product_brand,product_name,product_price,product_desc,product_image,product_keywords) values('$product_cat','$product_brand','$product_name','$product_price',','$product_desc','$product_image','$product_keywords')";
Insert This Query:
$insert_product="insert into products
(product_cat,product_brand,product_name,product_price,product_desc,product_image,product_keywords) values('$product_cat','$product_brand','$product_name','$product_price','$product_desc','$product_image','$product_keywords')";
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I am trying to make a login session with PHP and I recieve and error at "username" field of my database.
My source code is here:
index.php
<?php
session_start();
if(isset($_POST['bttLogin'])){
require 'connect.php';
$username = $_POST['username'];
$password = $_POST['password'];
$result = mysqli_query($con, 'SELECT * FROM account WHERE username=" '.$username.' " AND password="'.$password.' " ');
if (mysqli_num_rows($result)==1){
$_SESSION['username'] = $username;
header('Location: welcome.php');
}
else
echo "Account invalid";
}
if(isset($_POST['username'])){
$filename = $_POST['username'];
}
if (isset($_GET['logout'])){
session_unregister('username');
}
?>
<form method="post">
<table cellpadding="2" cellspacing="2" border="0">
<tr>
<td> Username </td>
<td><input type = "text" name="usermame"></td>
</tr>
<tr>
<td> Password </td>
<td><input type = "password" name="password"></td>
</tr>
<tr>
<td> </td>
<td><input type = "submit" name="bttLogin" value="Login"></td>
</tr>
</table>
</form>
I made my connection with my database, but it keeps saying that username is undefined. Please help. (Solved)
Seems you just don't have index 'username' in $_SESSION var.
You need to check and initialize it if needed -- something like this:
if (!isset($_SESSION['username'])) {
if (!is_array($_SESSION)) {
$_SESSION = [];
}
$_SESSION['username'] = $username;
}
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 5 years ago.
I have an issue inserting data to table in php,the problem I am facing maybe it is not difficult to solve but Im new in php development and could not solve it, here is my code
<table class="table-fill">
<tbody class="table-hover">
<tr>
<td class="text-left"><input type = "text" class = "form-control"
name = "name"></td>
<td class="text-left">name</td>
</tr>
<tr>
<td class="text-left"><input type = "text" class = "form-control"
name = "Quantity" id="QTY"></td>
<td class="text-left">QTY</td>
</tr>
<tr>
<td class="text-left"><input type = "text" class = "form-control"
name = "Type" id="type"></td>
<td class="text-left">type</td>
</tr>
<tr>
<td class="text-left"><input type = "text" class = "form-control"
name = "price" id="prs"></td>
<td class="text-left">price</td>
</tr>
<tr>
<td class="text-left"><input type="file" name="fileToUpload" id="fileToUpload"></td>
<td class="text-left">image</td>
</tr>
<tr>
<td class="text-left"> <input type="submit" value="Submit1" name="Submit1"></td>
<td class="text-left">Register</td>
</tr>
</tbody>
</table>
</div>
<div id="footer">
<div>
</div>
</div>
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn ) {
die('Could not connect: ' . mysql_error());
}
if (isset($_post('Submit1')))
{
$name=filter_input(INPUT_POST,'name');
$QTY=filter_input(INPUT_POST,'Quantity');
$type=filter_input(INPUT_POST,'Type');
$sql = "INSERT INTO products (ProductName, Quantity,Price)
VALUES ('$name','$QTY','$type')";
//('$_POST['name']','$_POST['Quantity']','$_POST['Type']')";
mysql_select_db('pharmacynew');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not enter data: ' . mysql_error());
}
echo "Entered data successfully\n";
mysql_close($conn);
} // put your code here
?>
</body>
</html>
the problem Iam facing when I run the page ,it shows me this error
"Fatal error: Cannot use isset() on the result of a function call (you can use "null !== func()" instead) "
Can any body tell me why is this problem appearing and how to overcome it,
many thanks
The problem is in this line:
if (isset($_post('Submit1')))
By using regular brackets ( and ) you're trying to use $_post as a function instead of a variable. That's not allowed within isset().
You need to use angled brackets [ and ] to read values from the $_post array (which should be $_POST in uppercase, by the way).
Simply change that line to
if (isset($_POST["Submit1"]))
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 6 years ago.
This is my form in HTML.
<html>
<head>
</head>
<body>
<table width="400" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC">
<tr>
<form id="form" name="form" method="POST" action="add_new_topic.php">
<td>
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF">
<tr>
<td colspan="3" bgcolor="#E6E6E6"><strong>Create New Topic</strong> </td>
</tr>
<tr>
<td width="14%"><strong>Topic</strong></td>
<td width="2%">:</td>
<td width="84%"><input name="topic" type="text" id="topic" size="50" /></td>
</tr>
<tr>
<td valign="top"><strong>Detail</strong></td>
<td valign="top">:</td>
<td><textarea name="detail" cols="50" rows="3" id="detail"></textarea></td>
</tr>
<tr>
<td><strong>Name</strong></td>
<td>:</td>
<td><input name="name" type="text" id="name" size="50" /></td>
</tr>
<tr>
<td><strong>Email</strong></td>
<td>:</td>
<td><input name="email" type="text" id="email" size="50" /></td>
</tr>
<tr>
<td> </td>
<td> </td>
<td><input type="submit" name="Submit" value="Submit" />
<input type="reset" name="Submit2" value="Reset" /></td>
</tr>
</table>
</td>
</form>
</tr>
</table>
</body>
</html>
This is add_new_topic.php code.
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="myforum"; // Database name
$tbl_name="fquestions"; // Table name
// Connect to server and select database.
$con = mysqli_connect("$host", "$username", "$password" , "$db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"$db_name")or die("cannot select DB");
// get data that sent from form
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
$datetime=date("d/m/y h:i:s"); //create date time
$sql="INSERT INTO '$tbl_name'('topic',' detail', 'name', 'email', 'datetime')VALUES('topic', 'detail', 'name', 'email', 'datetime')";
$result=mysqli_query($con,$sql);
if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {
echo "ERROR";
}
mysqli_close($con);
?>
It shows the error :
Notice: Undefined index: topic in
C:\xampp\htdocs\myforum\add_new_topic.php on line 18
Notice: Undefined index: detail in
C:\xampp\htdocs\myforum\add_new_topic.php on line 19
Notice: Undefined index: name in
C:\xampp\htdocs\myforum\add_new_topic.php on line 20
Notice: Undefined index: email in
C:\xampp\htdocs\myforum\add_new_topic.php on line 21 ERROR
HERE,on line 18,19,20 and 21 the following code lies.
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
Now what is the error in this POST method?
before getting post value add a condition if post value is not empty like this:
<?php
$host="localhost"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="myforum"; // Database name
$tbl_name="fquestions"; // Table name
if(isset($_POST) && !empty($_POST)){
// Connect to server and select database.
$con = mysqli_connect("$host", "$username", "$password" , "$db_name");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
mysqli_select_db($con,"$db_name")or die("cannot select DB");
// get data that sent from form
$topic=$_POST['topic'];
$detail=$_POST['detail'];
$name=$_POST['name'];
$email=$_POST['email'];
$datetime=date("d/m/y h:i:s"); //create date time
$sql="INSERT INTO '$tbl_name'('topic',' detail', 'name', 'email', 'datetime')VALUES('$topic', '$detail', '$name', '$email', '$datetime')";
if($result){
echo "Successful<BR>";
echo "<a href=main_forum.php>View your topic</a>";
}
else {
echo "ERROR";
}
mysqli_close($con);
}
?>
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Closed 8 years ago.
I'm sorry for taking a bit of your time i've been trying to figure out this error for hours
I have the following error:
Notice: Undefined index: Action in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 71
Notice: Undefined index: BUSSINESSID in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: ID in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: Action in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: BUSSINESSID in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: ID in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Notice: Undefined index: Action in C:\xampp\htdocs\Earl.com\Earl.com\EditEverything.php on line 129
Can't seem to work out whats wrong Here's the code:
<html>
<head>
</head>
<body>
<?php
$user="root";
$pass="";
$database="epages";
$localhost="localhost";
$conn=mysql_connect($localhost,$user,$pass);
mysql_select_db("epages") or die(mysql_error());
//*** Add Condition ***//
if($_POST["hdnCmd"] == "Add") {
$strSQL = "INSERT INTO BUSINESS ";
$strSQL .="(BUSINESSID,BUSINESSNAME,SERVICES) ";
$strSQL .="VALUES ";
$strSQL .="('".$_POST["txtAddBUSINESSID"]."','".$_POST["txtAddName"]."' ";
$strSQL .=",'".$_POST["txtAddService"]."') ";
$objQuery = mysql_query($strSQL);
if(!$objQuery) {
echo "Error Save [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
//*** Update Condition ***//
if($_POST["hdnCmd"] == "Update") {
$strSQL = "UPDATE BUSINESS SET ";
$strSQL .="BUSINESSID = '".$_POST["txtEditBUSINESSID"]."' ";
$strSQL .=",BUSINESSNAME = '".$_POST["txtEditName"]."' ";
$strSQL .=",SERVICES = '".$_POST["txtEditService"]."' ";
$strSQL .="WHERE BUSINESSID = '".$_POST["hdnEditBUSINESSID"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery) {
echo "Error Update [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
//*** Delete Condition ***//
if($_GET["Action"] == "Del") {
$strSQL = "DELETE FROM BUSINESS WHERE BUSINESSID ='".$_GET["ID"]."' ";
$objQuery = mysql_query($strSQL);
if(!$objQuery) {
echo "Error Delete [".mysql_error()."]";
}
//header("location:$_SERVER[PHP_SELF]");
//exit();
}
$strSQL = "SELECT * FROM BUSINESS";
$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); ?>
<form name="frmMain" method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
<input type="hidden" name="hdnCmd" value="">
<table width="600" border="1">
<tr>
<th width="91"> <div align="center">BUSINESSID</div></th>
<th width="98"> <div align="center">BusinessName</div></th>
<th width="198"> <div align="center">Service</div></th>
<th width="30"> <div align="center">Edit</div></th>
<th width="30"> <div align="center">Delete</div></th>
</tr>
<?php
while($objResult = mysql_fetch_array($objQuery)) {
if($objResult["BUSSINESSID"] == $_GET["ID"] and $_GET["Action"] == "Edit") { ?>
<tr>
<td><div align="center">
<input type="text" name="txtEditBUSINESSID" size="5" value="<?php echo $objResult["BUSINESSID"];?>">
<input type="hidden" name="hdnEditBUSINESSID" size="5" value="<?php echo $objResult["BUSINESSID"];?>">
</div></td>
<td><input type="text" name="txtEditName" size="20" value="<?php echo $objResult["BUSINESSNAME"];?>"></td>
<td><input type="text" name="txtEditService" size="20" value="<?php echo $objResult["SERVICES"];?>"></td>
<td colspan="2" align="right"><div align="center">
<input name="btnAdd" type="button" id="btnUpdate" value="Update" OnClick="frmMain.hdnCmd.value='Update';frmMain.submit();">
<input name="btnAdd" type="button" id="btnCancel" value="Cancel" OnClick="window.location='<?php echo $_SERVER["PHP_SELF"];?>';">
</div></td>
</tr>
<?php
} else { ?>
<tr>
<td><div align="center"><?php echo $objResult["BUSINESSID"];?></div></td>
<td><?php echo $objResult["BUSINESSNAME"];?></td>
<td><?php echo $objResult["SERVICES"];?></td>
<td align="center">Edit</td>
<td align="center">Delete</td>
</tr>
<?php
}
} ?>
<tr>
<td><div align="center"><input type="text" name="txtAddBUSSINESSID" size="5"></div></td>
<td><input type="text" name="txtAddName" size="20"></td>
<td><input type="text" name="txtAddService" size="20"></td>
<td colspan="2" align="right"><div align="center"><input name="btnAdd" type="button" id="btnAdd" value="Add" OnClick="frmMain.hdnCmd.value='Add';frmMain.submit();"></div></td>
</tr>
</table>
</form>
<?php mysql_close($conn); ?>
</body>
</html>
`
The lines that gives me the errors is this if($_GET["Action"] == "Del")
and this if($objResult["BUSSINESSID"] == $_GET["ID"] and $_GET["Action"] == "Edit").
Thanks in advance if you can help me out.
You need to take whole code using $_POST or $_GET arrays inside if block and check for any of the main $_GET/$_POST variable that is set by (isset() method) if it is set then do that stuff in the if block else don't.
Check whether the index is set in the arrays before using them, like so:
if (isset($_GET["Action"]) {
if($_GET["Action"] == "Del") {
// Do stuff
}
}
As #A.B mentioned in the other answer, you should always do this check.
The reason you're getting this error is because the GET/POST requests don't have a value set for this index on the client side.