Trying to make a simple Sql query in PHP [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 5 years ago.
Hello can you help me with this script?
Im trying to pull information on my slider from database.
$query = "SELECT * FROM `slider1`";
$select_from_slider1 = mysqli_query($connection, $query);
while ($row = mysqli_fetch_assoc($select_from_slider1)){
$slider1_title = $row['slider1_title'];
$slider1_content = $row['slider1_content'];
$slider1_moreinfo = $row['slider1_moreinfo'];
?>
<h2><?php echo $slider1_title ?></h2>
<p><?php echo $slider1_content ?></p>
<?php echo $slider1_moreinfo ?>
<?php } ?>
This is the error i get:
Lol im sorry i figured it out myself, the reason for the error was that i dublicated many times include "database"....
Thanks guys for the fast replies.!

Looks like the var $connection isn't a mysqli connection. You'll need the following somewhere this script can access it.
$connection = mysqli_connect('localhost', 'my_user', 'my_password', 'my_db');
http://php.net/manual/en/mysqli.construct.php

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);

username doesn't show when we login [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.
login code
<?php
include("koneksi.php");
$email=$_POST['email'];
$password=md5($_POST['password']);
$q="SELECT * FROM `user` WHERE `user`.`email`='$email' AND `user`.`password`='$password'";
$qe=mysql_query($q);
while ($de=mysql_fetch_array($qe)) {
$id_user=$de['id_user'];
}
if (mysql_num_rows($qe)>0) {
session_start();
$_SESSION['x']=$id_users;
header('location:home.php');
exit;
} else{
header('location:login_user.php');
exit;
}
?>
after login i wanna show or echo the username with this code
<?php
session_start();
$id_user=$_SESSION['x'];
$q="SELECT * FROM `user` WHERE `user`.`id_user`='$id_user'";
$qe=mysql_query($q);
$de=mysql_fetch_array($qe);
$username=$de['username'];
echo "
<li>$username</li>
";
?>
and the problem is the username doesn't show..
whats wrong.. help me ..
You have a typo in your login search query. Change to $_SESSION['x'] = $id_user;
Not $id_users
A piece of advice, use an IDE such as netbeans or eclipse or phpstorm. They help in identifying unused variables and other minor syntax errors.

Call PHP function from html form with parameters [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 7 years ago.
I am trying to run a simple select on the DB from a HTML form
HTML snippet is
<form Name ="form1" Method ="post" ACTION = "get341Usage.php">
<input type="submit" name="Submit1" value="3 Months" >
</form>
PHP file get341Usage.php
<?php
function get341Usage($org_id,$usage_mnth ) {
$conn = oci_connect('user', 'pass', '//server/ora_instance');
$query = "SELECT usage.* from usage_table usage
where customer_number = ' . $cust_id . '
AND usage_date >= (select add_months(sysdate ,' . $usage_mnth . '))";
$stid = oci_parse($conn, $qryStr);
oci_execute($stid);
oci_free_statement($stid);
}
if(isset($_POST['submit']))
{
getUsage($org_id,$usage_mnth);
}
?>
the reason for the 2 paramters was I wanted to create 3 buttons 3,6,12 monnths where the user to clicks and it prompts to auto save teh data to a csv file (haven't even got to this pary yet!)
any points would be great ...I suspect I'm miles off
if(isset($_POST['Submit1']))
{
getUsage($org_id,$usage_mnth);
}

Notice: Undefined index: id [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 7 years ago.
I have used a prepared statement to define the id index however, it is telling me that it is undefined for some reason, where and what do i change for this to work?
<?php
$db_username='student';
$db_password='student';
$db = new PDO ('mysql:host=192.168.56.2;dbname=Assessment', $db_username, $db_password);
$result = $db ->prepare("SELECT * FROM Jobs WHERE jobname='".$_GET['id']."' ");
$result->execute();
Try this:
<?php
$db_username='student';
$db_password='student';
$db = new PDO ('mysql:host=192.168.56.2;dbname=Assessment', $db_username, $db_password);
if(isset($_GET['id'])) {
$result = $db ->prepare("SELECT * FROM Jobs WHERE jobname=?");
$result->execute(array($_GET['id']);`enter code here
}
else {echo('$_GET["id"] not set');}
First, verify that $_GET['id'] has a value. Second, for security, change some lines:
$result = $db ->prepare("SELECT * FROM Jobs WHERE jobname=:id");
$result->execute(array(':id' => $_GET['id']));

Return SQL query errors in PHP file [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 7 years ago.
UPD: CLOSED. Duplicate found and typos in my original question
I am using this code and I want to get a message if there is an error with my SQL query :
$Db = mysqli_init();
$Db->options(MYSQLI_OPT_LOCAL_INFILE, true);
$Db->real_connect($servername, $username, $password, $dbname, 3306);
// Creation of first SQL query
$sql = ('select sum('.$metric1.') as t1metric from '.$table1.' WHERE '.$date1.' between "'.$start_date.'" AND "'.$end_date.'"');
$query = $Db->query($sql);
if ($Db->error)
{
printf("Errormessage: %s\n", $Db->error);
}
and I receive this error when I run the php file :
Call to a member function query() on a non-object
use the following
$Db->query($sql);
instead of
$mysqli->query($query);
$mysqli->query($query);
Replace with:
$Db->query($query);

Categories