0

I've got the following markup and PHP. It's for a gallery - to upload and display images. I followed through this tutorial, but for some reason the mysqli_connect_db can't find the DB. what am I missing?

<?php
include('upload.php');
?>

<form action="index.php" method="POST" enctype="multipart/form-data">

Choose File: <input type="file" name="file">
Title: <input type="text" name="nam">
<input type="submit" name="submit">

</form>

and the PHP:

<?php

$con = mysqli_connect("localhost","Melvin","") or die ("could not connect to DB");
mysqli_select_db($con, "galerie") or die ("no database");

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

$name = $_FILES['file']['name'];
$tmp_name = $_FILES['file']['tmp_name'];
$location = 'uploads/';
$target = 'uploads/' .$name;

	if(move_uploaded_file($tmp_name,$location.$name)){
		
		echo "file uploaded";
		
		$nam = $_POST['nam'];
		$query = mysqli_query($con , "INSERT INTO images(img_name,img_title)VALUES('".$target."','$nam')");
		
	} else {
		
		echo "file not uploaded";
			
	}

}

$result = mysqli_query($con, "SELECT FROM images");
while($row = mysqli_fetch_array($result)){
	
	echo "<img src=".$row['img_name']." &nbsp; class='addClass'>";
		
}

?>

10
  • 2
    don't output fixed/unchanging error messages. they're useless. have the DB TELL you why things failed: or die(mysqli_eror($con)). and note that you're simply ASSUMING nothing could ever go wrong with the queries, are simply assuming that uploads never fail, and are also vulnerable to sql injection attacks Commented Jun 7, 2016 at 19:38
  • What exact message are you seeing? Commented Jun 7, 2016 at 19:39
  • 2
    What is the problem exactly? Can you not insert or not view? This is incorrect $result = mysqli_query($con, "SELECT FROM images"); it should be $result = mysqli_query($con, "SELECT * FROM images"); Commented Jun 7, 2016 at 19:39
  • show the mysqli error messages - see: php.net/manual/en/mysqli.error.php. and display the error number ;-/ Commented Jun 7, 2016 at 19:41
  • Thanks! So now it says: Access denied for user ''@'localhost' to database 'galerie' Commented Jun 7, 2016 at 19:42

2 Answers 2

1

Also, this is missing what you are selecting

$result = mysqli_query($con, "SELECT FROM images");

Should be (add *):

$result = mysqli_query($con, "SELECT * FROM images");
Sign up to request clarification or add additional context in comments.

1 Comment

You're right about the SELECT * FROM part, @nerdlyist -- but OP is selecting the DB with mysqli_select_db($con, "galerie") or die ("no database"); so that 4th parameter isn't needed, because of that.
0

Creating a new user with all the rights in mySQL specifically for this database solved the problem for me.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.