Click here to Skip to main content
15,881,455 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
hi
I am trying to insert and display images in my database but in the insert file I am getting this error

is there anyway that i can fix this. can some one point me in the direction. i am not asking no one to do this assignment just looking for some explaining and some resolution to it. this is the error

PHP
Notice: Undefined variable: img_id in C:\wamp64\www\luana_itec244\php\upload.php on line 15


this is what I have tried.

What I have tried:

PHP
<pre><?php

		session_start();
		
	if(isset($_POST['submit']))
	{

		$db_host='localhost';
		$db_username='root';
		$db_password="";
		
		$con=mysqli_connect($db_host,$db_username,$db_password) or die(mysqli_connect_error());
		
			mysqli_select_db($con, 'food') or die(mysqli_error($con));
			$sql="SELECT * FROM tbl_images WHERE img_id='$img_id'";
			$result=mysqli_query($con, $sql) or die("Error:" .mysqli_error($con));
			$rowcount=mysqli_num_rows($result);
			
			if($rowcount >=1)
			{
				echo"<script type=\"text/javascript\";
						alert('files not uploaded');
						window.location=\"login.html\";
					</script>";
			}
				else
				{
					//insert images into table
					
					$sql = "INSERT INTO tbl_images
					VALUES('img_id','name','image')";
				
					if(mysqli_query($con, $sql))
					{
						mysqli_close($con);
						header("location:dashboard.php");
					}
					else
					{
						echo "Error inserting images";
					}
				}
	}
	
	
?>
Posted
Updated 16-Oct-21 3:46am

Looks like the variable $img_id do not exist. You have to create a variable befire using it.
PHP
$sql="SELECT * FROM tbl_images WHERE img_id='$img_id'";

Another problem you have on same line.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]

This one is weird too:
PHP
$sql = "INSERT INTO tbl_images VALUES('img_id','name','image')";
 
Share this answer
 
v2
"$img_id" variable does not exist. You have to declare a variable before using it. using it.
You have to store your image id into $img_id variable like: $img_id=5;
Below updated code you can use, i think it will be help of you.
PHP
<?php
	session_start();		
	if(isset($_POST['submit']))
	{

		$db_host='localhost';
		$db_username='root';
		$db_password="";
		$img_id=5;
		
		$con=mysqli_connect($db_host,$db_username,$db_password) or die(mysqli_connect_error());
		
			mysqli_select_db($con, 'food') or die(mysqli_error($con));
			$sql="SELECT * FROM tbl_images WHERE img_id='$img_id'";
			$result=mysqli_query($con, $sql) or die("Error:" .mysqli_error($con));
			$rowcount=mysqli_num_rows($result);
			
			if($rowcount >=1)
			{
				echo"<script type=\"text/javascript\";
						alert('files not uploaded');
						window.location=\"login.html\";
					</script>";
			}
				else
				{
					//insert images into table
					
					$sql = "INSERT INTO tbl_images
					VALUES('img_id','name','image')";
				
					if(mysqli_query($con, $sql))
					{
						mysqli_close($con);
						header("location:dashboard.php");
					}
					else
					{
						echo "Error inserting images";
					}
				}
	}	
?>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900