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.
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)
The 3 different equals
(5 answers)
Closed 2 years ago.
I have this code in my login.hmtl in s section with ajax method:
$.ajax ({
type:'post',
url:'Controladores/login.php',
data:{do_login:"do_login", nick:nick, pass:pass },
success:function(response) {
alert($.trim(response));
if ($.trim(response) ==='Correcto'){
window.location.href="intranet.php?ruta=inicio";
return false;
}
else{
alert('Datos incorrectos. Volver a intentar.');
}
}
});
This redirects me to login.php where I connect to the database and then check if the username and password are correct. If they are, they return an answer Correct or Incorrect.
Here is the main part where I start my session, and also if username and password match, set the username 'NICK' to the session.
session_start();
$db = new Conexion();
$conn = $db->connect( '00.00.00.00', 'Seguridad');
$param = [$nick, $pass];
$query = "exec Seguridad.dbo.autenticacion ?, ?";
$result= $db->rows($conn, $query, $param);
$obj = json_decode($result);
//echo $obj[0]->resultado;
if( $obj[0]->resultado ='Correcto' ){
$_SESSION['nick']= $nick;
$_SESSION['nombreusuario'] = $obj[0]->nombreusuario;
$_SESSION['agencia'] = $obj[0]->CodigoAgencia;
echo ("Correcto");
}else{
if ($obj[0]->resultado =='Incorrecto'){
echo ("Incorrecto");
}
}
Then, In my home page called inicio.php I called again the session but the message is:
Notice: Undefined index: nick in C:\xampp\htdocs\intranetv3\Vistas\modulos\inicio.php on line 3
<?php
session_start();
$userName = $_SESSION['nick'];
echo "$userName";
?>
I have read other article but have not found anything.
I checked if the answer Correct or Incorrect returns the answer and it returns.
I printed the user name and password. It worked.
I also printed the $_SESSION['nick'], I see it has the username.
Apparently when I go to my inicio.hmtl the session just unssets or something, I have no idea what might be wrong. Please HELP!!
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.
So I am a beginner in PHP and am using MySQL to make a simple CRUD application. I am done with making the CRUD application table and also have successfully connected it with my MySQL database. However, when I load the page on the server, I am facing errors which I am unable to debug. Here is part of code for my CRUD table in which I am facing the error (lines 57-74 of my code):
// Attempt select query execution my
$sql = "SELECT * FROM students";
if($result = $mysqli->query($sql)){
if($result->num_rows > 0){
echo "<table class='table table-bordered table-striped'>";
echo "<thead>";
echo "<tr>";
echo "<th>id</th>";
echo "<th>Name</th>";
echo "<th>Father's Name</th>";
echo "<th>Mother's Name</th>";
echo "<th>Email</th>";
echo "<th>Contact</th>";
echo "<th>Alt. Contact</th>";
echo "<th>Alt. Contact</th>";
echo "</tr>";
echo "</thead>";
echo "<tbody>";
Here is the error I am getting:
Notice: Undefined variable: mysqli in /Applications/XAMPP/xamppfiles/htdocs/student/options2.php on line 59
Fatal error: Uncaught Error: Call to a member function query() on null in /Applications/XAMPP/xamppfiles/htdocs/student/options2.php:59 Stack trace: #0 {main} thrown in /Applications/XAMPP/xamppfiles/htdocs/student/options2.php on line 59
You're missing the most important line for database connection between PHP and MySQL. The mysqli class, Represents a connection between PHP and a MySQL database. Also, enable mysqli error reporting so you can easily find out the error. See more: https://www.php.net/manual/en/mysqli.query.php
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli("your_host", "your_user", "your_password", "your_database");
// Change character set to utf8
$mysqli->set_charset("utf8mb4");
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.
Actually, I'm doing mini project on "faculty feedback system".
Following is the code that inserts the form entries into the database.
include.php file is for database connectivity
<?php
require "../includes/include.php";
$name=$_POST['name'];
if($name="")
echo "Name field can't be empty !!!<br>";
$faculty=$_POST['faculty'];
if($faculty="")
echo "Please select your faculty !!!<br>";
$rating=$_POST['rating'];
if($rating="")
echo "Please select the respective rating !!!<br>";
$response=$_POST['response'];
$name=mysqli_real_escape_string($con, $name);
$faculty=mysqli_real_escape_string($con, $faculty);
$rating=mysqli_real_escape_string($con, $rating);
$response=mysqli_real_escape_string($con, $response);
$insert_query="insert into feedback(name, faculty, rating, response) values ('$name', '$faculty', '$rating', '$response')";
$query_result=mysqli_query($con, $insert_query);
?>
It shows the error while inserting values
Notice: Undefined index: name in /var/www/html/Mini_Project/php/feedback_script.php on line 12
.
Please solve this error
Your assignment of $name, $faculty, $rating, and $response expects your $_POST array to have certain keys in it. If you cannot garantee your $_POST to have all that, it's best to use the null coalesce operator (i.e. double question mark ??) to define a fallback value for assignment:
$name=$_POST['name'] ?? "";
if($name="")
echo "Name field can't be empty !!!<br>";
$faculty=$_POST['faculty'] ?? "";
if($faculty="")
echo "Please select your faculty !!!<br>";
$rating=$_POST['rating'] ?? "";
if($rating="")
echo "Please select the respective rating !!!<br>";
$response=$_POST['response'] ?? "";
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);
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.
Am using PDO method to get data from SQL But am unable to get data from SQL database since am new to PDO its little hard to understand
but i did the following code but Doesn't work can someone help me
CODE
<?php
$servername = "localhost";
$username = "sanoj";
$password = "123456";
$dbname = "test";
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$stmt = $conn->prepare("SELECT * FROM members");
$stmt->execute();
// set the resulting array to associative
$membid=($_GET['memberID']);
$email=($_GET['email']);
$state=($_GET['username']);
}
catch(PDOException $e) {
echo "Error: " . $e->getMessage();
}
echo $membid;
echo $email;
echo $state;
$conn = null;
echo "</table>";
?>
ERROR I GET
Notice: Undefined index: memberID in C:\Users\sanoj\Documents\NetBeansProjects\godaddy optimized\data.php on line 14
Call Stack
# Time Memory Function Location
1 0.0020 132392 {main}( ) ..\data.php:0
( ! ) Notice: Undefined index: email in C:\Users\sanoj\Documents\NetBeansProjects\godaddy optimized\data.php on line 15
Call Stack
# Time Memory Function Location
1 0.0020 132392 {main}( ) ..\data.php:0
( ! ) Notice: Undefined index: username in C:\Users\sanoj\Documents\NetBeansProjects\godaddy optimized\data.php on line 16
Call Stack
# Time Memory Function Location
1 0.0020 132392 {main}( ) ..\data.php:0
Using $_GET variable won't make you get your data from your database using PDO. You were doing right up until $stmt->execute();. What you need to do next is:
while($result = $stmt->fetch(PDO::FETCH_ASSOC))
{
foreach($result as $key => $value)
echo $key.': '.$value.'<br/>';
echo '<hr/>';
}
To get your data.
$_GET array is to fetch your data from the URL or from a GET Ajax call.