PHP. undefined index [duplicate] - php

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.
Here is the php connectivity code:
<?php
$con=mysql_connect("127.0.0.1", "root", "");
mysql_select_db("society");
if(!$con)
{
echo "Failed to Connect to MySql".mysqli_connect_error();
}
else
{
echo"success";
}
$query="INSERT INTO users (fname, lname, email, password)
VALUES('".$_POST['fname']."','".$_POST['lname']."','".$_POST['email']."','".$_POST['password']."')";
mysql_query($query,$con);
?>
This is the error. Tried, but I am not able to solve the error:
Notice: Undefined index: fname in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\scripts\html\connect.php on line 13
Notice: Undefined index: lname in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\scripts\html\connect.php on line 13
Notice: Undefined index: email in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\scripts\html\connect.php on line 13
Notice: Undefined index: password in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC9\data\localweb\scripts\html\connect.php on line 13

Try this
<?php
$con=mysql_connect("127.0.0.1", "root", "");
mysql_select_db("society");
if(!$con)
{
echo "Failed to Connect to MySql".mysql_connect_error();
}
else
{
echo"success";
}
if(isset($_POST['fname']) && isset($_POST['lname']) && isset($_POST['email'])) {
$query="INSERT INTO users (fname, lname, email, password)
VALUES('".$_POST['fname']."','".$_POST['lname']."','".$_POST['email']."','".$_POST['password']."')";
mysql_query($query,$con);
}
?>

Check if there is values for the POST your using in the query. If it is returning undefined_index is because the variables of the post that you are using isn't set. They don't even exist at the moment.
For instance:
If you debug $_POST['fname'] it will return false or empty. Try it.
Resolution:
Right now I think you're running this code without validating if the form has been submited or not.
So, to start you should do this:
if(isset($_POST) && count($_POST) > 0){
$con=mysql_connect("127.0.0.1", "root", "");
mysql_select_db("society");
if(!$con)
{
echo "Failed to Connect to MySql".mysqli_connect_error();
}
else
{
echo"success";
}
$query="INSERT INTO users (fname, lname, email, password) VALUES('".$_POST['fname']."','".$_POST['lname']."','".$_POST['email']."','".$_POST['password']."')";
mysql_query($query,$con);
}

Related

How can I fix an undefined index? [duplicate]

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 3 years ago.
I am connected to my local host and am trying to use a GET method on line $. I am getting the Notice: Undefined index: deleteid in C:\xampp\htdocs\webd153\delete.php on line 4.
<?php
include 'connection.php';
$deleteid = $_GET['deleteid'];
if (isset($deleteid)) {
$deletesql = $dbh->prepare("DELETE FROM users WHERE id = '$deleteid'");
$deletesql->execure();
echo "record has been deleted!<br>";
I am trying to delete names that I have entered in my databases using a form that is connected from my local host to myphpadmin database.
Right way is:
<?php
include 'connection.php';
if(isset($_GET['deleteid']) {
$deleteid = $_GET['deleteid'];
$deletesql = $dbh->prepare("DELETE FROM users WHERE id = '$deleteid'");
$deletesql->execute();
echo "record has been deleted!<br>";
}
But this is VERY INSECURE! When I send request with URL ending ?deleteid=1'+OR+1=1+OR+id=', your database will be deleted all rows. I suggest to change query building as:
$deletesql = $dbh->prepare("DELETE FROM users WHERE id = (?)");
$deletesql->bind_param('i', $deleteid);

mysql database search remote host or page name [duplicate]

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.
The purpose of my lab is to have the user search through a database called "pageVistis" from what they enter in the field. They can search through the remote host or page name. I have Been having trouble fixing an error that i am recieving saying "Notice: Undefined variable: searchtype in C:\xampp\htdocs\pageVisitsLab\DataBC.php on line 23
Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in C:\xampp\htdocs\pageVisitsLab\DataBC.php:25 Stack trace: #0 {main} thrown in C:\xampp\htdocs\pageVisitsLab\DataBC.php on line 25". Also the code is form a file called "DataBC.php". If any extra information is needed please let me know and i will edit this post and include it thank you.
include "interface.php";
if(isset($_POST['submit'])) {
$sName = $_POST['sN'];
$sHost = trim($_POST['sH']);
if(!$sName || ! $sHost) {
echo "<p> Please enter a search </p>";
exit;
}
/*switch ($sName) {
case 'sName':
case 'sHost':
break;
default:
echo "<p> Invalid search type and please try again </p>";
}
*/
$query = "SELECT page_name, visit_date, previous_page, request_method, remote_host, remote_port FROM visitInfo
WHERE $searchtype= ?";
$stmt = $databse -> prepare($query);
$stmt-> bind_param('s',$sName);
$stmt-> execute();
$stmt->store_result();
$stmt->bind_result($pageName,$visitDate,$previouPage,$requestMethod,$remoteHost,$remotePort);
echo "<p> The number of items found: .$stmt->num_rows</p>";
while ($stmt->fetch()) {
echo "<p><strong>page Name: ".$pageName."</strong></p>";
echo "Visit Date: ".$visitDate."<br>";
echo "Previous Page: ".$previouPage. "<br>";
echo " Request Method: ".$requestMethod. "<br>";
echo "Remote Host: ".$remoteHost. "<br>";
echo "Remote Port: ".$remotePort. "<br>";
}
$stmt->free_result();
$databse->close();
}
?>
I think your problem is in your query, is $searchtype a variable you define in php? maybe in that interface.php.
If you want to use a php variable in a mysql query then that is not the correct way of doing it.

I am facing problems in the register page [duplicate]

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.
<?php
require('includes/core.inc.php');
require('includes/database/connect.db.php');
session_start();
if(isset($_POST['Register'])) {
$username=$_POST['Username'];
$password=$_POST['Password'];
$query= "INSERT into users (Username,Password) VALUES ('$username','$password')";
$res = mysql_query($query);
if($res){
header("Location:index.php");
}
}
?>
I am facing problems while inserting data in the database don't know why the query is correct this problem is while selecting from database
Notice: Undefined index: Username in C:\xamp\htdocs\chatbox\index.php on line 4
<?php
require('includes/core.inc.php');
session_start();
echo "Welcome".$_SESSION['Username'];
if(isset($_POST['send'])){
if(send_msg($_POST['sender'],$_POST['message'])){
//echo "Message sent ...";
}else{
//echo "failed to sent ";
}
}
?>
try this remember you have to set session variable before using it.
<?php
require('includes/core.inc.php');
require('includes/database/connect.db.php');
session_start();
if(isset($_POST['Register'])) {
$username=$_POST['Username'];
$password=$_POST['Password'];
$_SESSION['Username'] = $username;
$query= "INSERT into users (Username,Password) VALUES ('$username','$password')";
$res = mysql_query($query);
if($res){
header("Location:index.php");
}
}
?>
also to get rid of undefine index issue you should always use
echo "Welcome".isset($_SESSION['Username'])?$_SESSION['Username']:"";
hope your issue will get resolved.

PHP error - Undefined index: userId [duplicate]

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'm doing project and it needs android-PHP-MySQL connection.
I programmed PHP code, but it doesn't work. The error code says 'Undefined index: userId...'
I don't know how to solve it. I googled for get some solutions but I couldn't find the right answer. Other PHP code with another android project is working. Why..?
Following is My PHP code.
<?php
error_reporting(E_ALL);
ini_set("display_errors", 1);
require "init.php";
$userId = $_POST["userId"];
$username = $_POST["username"];
$userphone = $_POST["userphone"];
$usermail = $_POST["usermail"];
$userpw = $_POST["userpw"];
$sql = "INSERT INTO register_info VALUES ('$userId', '$username', '$userphone', '$usermail', '$userpw' );";
if(mysqli_query($con, $sql))
{
var_dump($userId,$username,$userphone,$usermail, $userpw );
}
?>
And this is the error code.
Notice: Undefined index: userId in /var/www/html/website/web/webapp/insertUser.php on line 9
Notice: Undefined index: username in /var/www/html/website/web/webapp/insertUser.php on line 10
Notice: Undefined index: userphone in /var/www/html/website/web/webapp/insertUser.php on line 11
Notice: Undefined index: usermail in /var/www/html/website/web/webapp/insertUser.php on line 12
Notice: Undefined index: userpw in /var/www/html/website/web/webapp/insertUser.php on line 13
string(0) "" string(0) "" string(0) "" string(0) "" string(0) ""
Query mysql have solution.
try
$sql = "INSERT INTO register_info (userId, username, userphone, usermail, userpw) VALUES ('$userId', '$username', '$userphone', '$usermail', '$userpw' );";
or see
https://www.w3schools.com/php/php_mysql_insert.asp

Notice: Undefined index: $username [duplicate]

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.
Hello i'm new to php and i wanted to create a admin panel and i got an error with my register page.
This is the error :
Notice: Undefined index: username in C:\wamp\www\Website2 - Copy\register.php on line 18
if (isset($_POST['submit'])){
$username = $_POST['username']; // error here
$password = md5($_POST['password']);
if(empty($username)or empty($password)){
echo"There is an empty space";
}else{
mysql_query("INSERT INTO users VALUES('', '$username', '$password')");
}
}
This happens because the empty function checks if the variables $username and $password has no value or set equal to zero. (0, null, false,''). As the empty function does not check if the variable exists, php throws this error.
In this case it would be correct to use the isset function of php.
http://br1.php.net/isset

Categories