Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
trying to insert and display images the db using php and i have three errrors.

error no 1.
PHP
 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp64\www\luana_itec244\php\dashboard.php on line 32
Call Stack


error no 2:
PHP
Warning: move_uploaded_file(images/bhaji_n_pigtails.jpg): failed to open stream: No such file or directory in C:\wamp64\www\luana_itec244\php\dashboard.php on line 80



please help and much appreciated

What I have tried:

PHP
<pre><?php

		
		$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));
		if(isset($_POST['submit']) && isset($_GET['img_id']))
		{
			$sql= "SELECT * FROM tbl_images  WHERE img_id={$img_id}";
			$result=mysqli_query($con, $sql) or die("Error:" .mysql_error($con));
			$rowcount=mysqli_num_rows($result);
		}
		
			
?>
<html>
<body>	

	
		<div id="content">
			<?php
				$db_host='localhost';
				$db_username='root';
				$db_password="";
				$con=mysqli_connect($db_host,$db_username, $db_password) or die (mysqli_connect_error());
				
				$sql ="SELECT * FROM tbl_images";
				$result = mysqli_query($con, $sql);
				while($result = mysqli_fetch_array($result))
				{
					echo "<div id=img_div";
						echo "<img src='images/" .$row['image']. "'>";
							echo "<p>" .$row['text']. "</p>";
					echo "<div>";
					
					
				}
			?>
		</div>
		<form  method="post" enctype="multipart/form-data" >
			<br/>
				<input type="hidden" name="size" value="100000">
				<br/><br/>
				<div>
					<input type="file" name="image" value="Upload">
				</div>
				<div>
					<textarea name="text" cols="40" rows="4" placeholder="say something"></textarea>
				</div>
				<div>
					<input type="submit" name="upload" value="Upload" />
				</div>
		</form>
			
	

<?php
		$msg="";
		if(isset($_POST['upload']))
		{
			
			$image_text = mysqli_real_escape_string($con,$_POST['text']);
			
			$target="images/" .basename($_FILES['image']['name']);
			
			$db_host='localhost';
			$db_username='root';
			$db_password="";
			$con = mysqli_connect($db_host, $db_username, $db_password);
			
			$image = $_FILES['image']['name'];
			$text = $_POST['text'];
			
			$sql = "INSERT INTO tbl_images ('image', 'text') values($text, $image)";
			mysqli_query($con, $sql);
			
			if(move_uploaded_file($_FILES['image']['tmp_name'], $target))
			{
				$msg ="Image upload successfully";
			}
			else
			{
				$msg = "Error uploading image";
			}
			 $result = mysqli_query($con, "SELECT * FROM tbl_images");
			
			
		}

	
?>		
		
</body>
</html>
Posted
Updated 27-May-18 21:58pm
Comments
Kornfeld Eliyahu Peter 27-May-18 5:40am    
Debug your code!!!
http://php.net/manual/en/mysqli.query.php

"Returns FALSE on failure. For successful SELECT, SHOW, DESCRIBE or EXPLAIN queries mysqli_query() will return a mysqli_result object. For other successful queries mysqli_query() will return TRUE."
Richard MacCutchan 27-May-18 7:11am    
1. Please read the MySQL documentation, where it explains what values may be returned on each request.

2. Either the file name is incorrect or the path does not exist. Only you can check both.

Hello Desperate,

The problem is caused by missing database;

You can check mysqli_error after executing the query. if you do, you will see that your database is not selected. But you will say that you have selected. The answer is yes and no. You have made two connections, which was unnecessary. For the first connection, in var $con, you have selected db. But in the second connection, which you also named $con, did not select db

Solutions:
Solution 1. add mysqli_select_db command after the second connection
Solution 2. Remove the second mysqli_connection
 
Share this answer
 
You are using the variable $row within your fetch loop but has assigned the mysqli_fetch_array() return value to $result:
PHP
$result = mysqli_query($con, $sql);
while($result = mysqli_fetch_array($result))
The above will also fail when the query fails.

Because a query may fail, you should alse check for that (then $result is a boolean as indicated in the error message).

So your code should be
PHP
/* Always check for success before using $result as object */
if ($result = mysqli_query($con, $sql)) {
    /* Use a new variable here for the fetch result */
    while ($row = mysqli_fetch_array($result)) {
        /* Use $row here */
    }
}

I can't help regarding the file move problem. For some reason the file images/bhaji_n_pigtails.jpg does not exist or the directory specified in the $target path does not exist. Solving that requires some investigation on your system (PHP and web server setup) and / or debugging.
 
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