This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 3 years ago.
<?php
$name_user = $_POST['name'];
$city_user = $_POST['the_city'];
$conn = mysqli_connect('127.0.0.1','root','123','people');
mysqli_query($conn, "INSERT INTO people_data (name, the_city) VALUES ('$name_user','$city_user')");
echo "Data has been added:";
echo "<br/>";
echo "Name : $name_user";
echo "<br/>";
echo "The city ";
switch ($city_user)
{
case "01":
echo "London";
break;
case "02":
echo "New York";
break;
case "03":
echo "Bali";
break;
default:
echo "Unknown City";
}
?>
I have run the code, but it returns error:
Notice: Undefined index: the_city in /opt/lampp/htdocs/add.php on line 7
I see the name is added, but the city is still blank in the table people_data.
I guess the error is in switch statement, but I do not know where's the error.
Expected result in table people_data:
the_city contains value like 01 or 02 or 03
I forgot to add the html file, the html file:
<!DOCTYPE HTML>
<HTML>
<body>
<p>
Add Data
<br/>
<form action="add.php" method="post">
Name : <input type="text" name="name" /><br/>
The city :
<select>
<option value = "01">London</option>
<option value = "02">New York</option>
<option value = "03">Bali</option>
</select>
<input type="submit" value="Send">
</form>
</p>
</body>
</HTML>
I run the html file..
The problem is that your $_POST['the_city'] is NULL because you don't send it via POST. Your <select> tag has no reference name. The correct code would look like this.
<select name="the_city">
...
</select>
Furthermore, please consider using isset() method in PHP.
if (isset($_POST['the_city]))
{
//your variable has been initialized
}
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 2 years ago.
<?php include 'connection.php'; ?>
<?php
if (isset($_GET['consultation']))
{
echo "Consultation Details";
$no=$_GET['consultation'];
?>
<form class="" method="get">
<textarea name="details" rows="8" cols="80" placeholder="enter consultation details"></textarea><br>
<button type="submit" name="c">submit</button>
</form>
<?php
if (isset($_GET['details'])) {
$details=$_GET['details'];
}
//$details= $_GET['details'];
$insertQuery="INSERT INTO redo (consultation) VALUES ($details) WHERE no=$no;";
$insert = mysqli_query($conn,$insertQuery);
if ($insert) {
echo "recorded";
?>
Back to Total Patients
Back to Registration
<?php
}
else {
echo "record couldnt be inserted";
}
}
?>
No $_GET['details'], you sure you're using a GET method in the form? Change $_GET['details'] to $_REQUEST['details'].
To be exact, this one fails:
if (isset($_GET['details'])) {
$details=$_GET['details'];
}
I mean, the if condition is not met, so $details is not defined, as you don't define it anywhere in the code outside that if.
Other solution would be adding:
$details = '';
in front of that if.
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 3 years ago.
I am trying to run my PHP code, but I am getting an undefined error for the $name variable. The error will show only when it runs for the first time only, but after the second run, it didn't show any error.
<?php
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$name = $nameerr = "";
if(empty($_POST["name"])){
$nameerr = "This Field is Required";
}
else{
$name = test_run($_POST['name']);
}
}
function test_run($data){
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form action="<?php htmlspecialchars($_SERVER['PHP_SELF']) ?>" method="POST">
<label for="name">Name: </label>
<input type="text" name="name" value="<?php echo $name;?>"><span>* <?php echo $nameerr ?></span><br><br>
<input type="submit" value="Submit">
</form>
<h1>Output</h1>
<?php
echo $name."<br><br>";
?>
This is because you are not setting the $name variable initially. You are only setting it when a post request is made. So the first time you fetch your script with a GET request, PHP shows you the notice. The second time ( I presume is after you submit your form which initiates a POST request ) the $name variable is set, thus, you do not receive the notice. To resolve this, you can set your $name variable to an empty string above your first if statement.
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 4 years ago.
My Code Looks Like:
<?php
echo "
<form action='form.php'>
<input type='text' name='value'>
<input type='submit'>
</form>
";
?>
And form.php is
<?php
echo $_POST['value'];
?>
Error Code is:
Notice: Undefined index: value in C:\xampp\htdocs\dynamic\form.php on line 2
Is it possible to get the value of field created using echo command?I don't want to use database,jQuery etc.
You need to set the form method to post
<?php
echo "
<form action='form.php' method='post'>
<input type='text' name='value'>
<input type='submit'>
</form>
";
?>
you are missing method="post" in form tag
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.
I am begineer in php . as this code i have been practising from tutorialspoint.com. in the site this code has produced the actual result but while running in my localhost it says undefined index : name in C:\xampp\htdocs\php\second.php on line 2.
<?php
if( $_POST["name"] || $_POST["age"] ) {
if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
die ("invalid name and name should be alpha");
}
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
}
?>
<!------------Form----------------->
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
</body>
</html>
use isset function to avoid undefined index error
Use
if( isset($_POST["name"]) || isset($_POST["age"]))
instead of
if( $_POST["name"] || $_POST["age"] )
Hello you have written html and php both in same page so first when you load page at that time you will not get any value in $_POST so you can try like below code then you will not get error.
<?php
if(isset($_POST))
{
if( $_POST["name"] || $_POST["age"] ) {
if (preg_match("/[^A-Za-z'-]/",$_POST['name'] )) {
die ("invalid name and name should be alpha");
}
echo "Welcome ". $_POST['name']. "<br />";
echo "You are ". $_POST['age']. " years old.";
exit();
}
}
?>
<!------------Form----------------->
<html>
<body>
<form action = "<?php $_PHP_SELF ?>" method = "POST">
Name: <input type = "text" name = "name" />
Age: <input type = "text" name = "age" />
<input type = "submit" />
</form>
</body>
</html>
in this when first without submit page load it checks if $_POST isset then run php code otherwise not. actually php script run line by line so without $_POST if you are try to access then you will get error try this code may it helps you.
This question already has answers here:
"Notice: Undefined variable", "Notice: Undefined index", "Warning: Undefined array key", and "Notice: Undefined offset" using PHP
(29 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 6 years ago.
<!doctype html>
<html>
<head>
<title></title></head>
<body>
<form action="#" method="POST" enctype="multipart/form-data">
<input type="text" name="usname" value = "<?=$user_name;?>"/>
<input type="password" name="psd" value = "<?=$pass_word;?>" />
<input type="textarea" name="addrs" value = "<?=$address;?>">
<input type="hidden" name="id" value = "<?=$id?>"/>
<input type="submit" name="submit" value="submit"/>
</form>
<?php
if(isset($_POST['submit'])){
$sql= "Update form set user_name='".$_POST['usname']."',
pass_word='".$_POST['psd']."',
address='".$_POST['addrs']."'
where id='".$_GET['id']."'
";
$result=mysql_query($sql);
if($result)
{
echo"data is succesfully data insert";
header("location:login.php");
}else
{echo"";}
}
$id='';
$user_name='';
$pass_word='';
$address='';
$host="localhost";
$username="root";
$password="";
$db="form2";
$con=mysql_connect($host,$username,$password);
if(isset($_GET['id']))
{
echo $query = "select id,user_name,pass_word,address from form where id=".$_GET['id'];
mysql_select_db($db,$con);
echo $result=mysql_query($query);
if($result){
while($row= mysql_fetch_array($result))
$id=$row['id'];
$user_name=$row['user_name'];
$pass_word=$row['pass_word'];
$address=$row['address'];
}
}
?>
</body>
</html>
Notice: Undefined variable: user_name in C:\xampp\htdocs\form2\edit.php on line 7
•••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••••
Notice: Undefined variable: address in C:\xampp\htdocs\form2\edit.php on line 9
submit
i am create a edit.php and update the values
I'm running a PHP script, and maintain getting errors like:
If you are using some variable then please check if its set or not
<?=$user_name;?> you need to chnage to <?=(isset($user_name)?$user_name:""?> and same for others.