0

hi all

presently am doing an assignment. for the past hour or have been experiencing one error to the next with it. it is website am doing where a user can register, login, view their profile etc.
one part of the assignment is where u have to insert and display images on the site using php.

I get thru in some aspect of the assignment concerning the inserting and displaying images. i cant get the images to display, dont know if there is something wrong with the code or what. I am currently getting this errors. have tried everything that I can think of. search on the net, got a few ideas, tried it out but nothing seem to be working. I have only 2 more day to finish this assignment. I am on crunch time here. so any solution would be greatly appreciated.
these are the two errors
error no. 1: Notice: Undefined variable: sql in C:\wamp64\www\luana_itec244\php\upload.php on line 22
error no. 2: Notice: Undefined variable: sql in C:\wamp64\www\luana_itec244\php\upload.php on line 22

they are both on the same line. here is my codes. I need some answers not people just viewing my questions and passing it str8. much thanks in advance.

<?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));
            if(isset($_GET["img_id"]))
            {

                $sql="SELECT * FROM tbl_images WHERE img_id='$img_id'";
            }
            if(isset($_POST['submit']))
            {
                $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";
                    }
                }
    }

?>
3
Contributors
2
Replies
36
Views
2 Months
Discussion Span
Last Post by EvolutionFallen
2

I dont speak php, but since nobody else has answered...
is php like oher languages where the scope of a variable is its immediately containing block? Because if so $sql is only in scope thru lines 16-19. If not, please ignore this.

Votes + Comments
If I recall, PHP does not hoist like JavaScript. This is the problem.
0
  1. As far as I recall, IF statements don't have their own scope in PHP. What is the URL you are visiting? If you are getting this error, chances are $_GET["img_id"]) isn't set.
  2. You're referencing $img_id on line 18, but you never initialized this variable
  3. You're including the text $img_id in your string, instead of the value stored by the $img_id variable
  4. It also doesn't really make sense to set $sql on line 18 if $_POST['submit'] wasn't set, as you won't need the SQL statement if you're not connecting to the DB through the IF statement on 20. So combine the conditions into one as follows.

Try

if(isset($_POST['submit'] && isset($_GET['img_id']))
{
    $img_id = $_GET['img_id'] // you probably want to sanitize this too
    $sql="SELECT * FROM tbl_images WHERE img_id={$img_id}"; // note -- must use double quotes here to print var value
    $result=mysqli_query($con,$sql) or die("Error:" .mysqli_error($con));
    $rowcount=mysqli_num_rows($result);
}

Edited by EvolutionFallen: Typo on "print"

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.