Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

php source code Review and re-write with accuracy. Then explain how errors was c

ID: 3736428 • Letter: P

Question

php source code

Review and re-write with accuracy. Then explain how errors was corrected. Is it working now...

ensure code can work error free when saved as a php file in notepad++

<?php

session_start();

if($_GET){

            $author = $_GET['author'];

            $title = $_GET['title'];

            $select = $_GET['select'];

           

            $connect = mysqli_connect("localhost", "root", "","mysql") or die(mysql_error());

           

            if($connect)

            {

                        $query = "SELECT * FROM bookstore WHERE author='" . $author . "' or Title='" . $title . "' ";

                       

                        $query2 = "SELECT * FROM bookstore WHERE Year = '" . $select ."' ";

                       

                        $result = mysqli_query($connect,$query) or die(mysql_error());

                        $result2 = mysqli_query($connect,$query2) or die(mysql_error());

                       

                        while($row = mysqli_fetch_array($result)){

           

                                    echo $row['ISBN'] . "<br/>" . $row['Title'] . "<br/>" . $row['Author'] . "<br/>" . $row['Publisher'] . "<br/>" . $row['Year'] . "<br/>";

                                   

                        }

                        while($row2 = mysqli_fetch_array($result2)){

                                    echo $row2['ISBN'] . "<br/>" . $row2['Title'] . "<br/>" . $row2['Author'] . "<br/>" . $row2['Publisher'] . "<br/>" . $row2['Year'] . "<br/>";

                        }

            } else {

                        die(mysql_error());

            }

            mysqli_free_result($result);

            mysqli_close($connect);

}

?>

Explanation / Answer

Following is the answer :

I added the comment where i changed the code

<?php

session_start();

if(isset($_GET['submit'])){ // only checks if form is submited.

$author = $_GET['author'];

$title = $_GET['title'];

$select = $_GET['select'];

$connect = mysqli_connect("localhost", "root", "","mysql") or die(mysql_error());

if($connect)

{

$query = "SELECT * FROM bookstore WHERE author='" . $author . "' or Title='" . $title . "' ";

$query2 = "SELECT * FROM bookstore WHERE Year = '" . $select ."' ";

$result = mysqli_query($connect,$query) or die(mysql_error());

$result2 = mysqli_query($connect,$query2) or die(mysql_error());

if (mysqli_num_rows($result) > 0) { // checks if result is 0 or not

while($row = mysqli_fetch_assoc($result)){// mysqli_fetch_assoc instead of mysqli_fetch_array because if you use array then you have to use id not name like $row[0]

echo $row['ISBN'] . "<br/>" . $row['Title'] . "<br/>" . $row['Author'] . "<br/>" . $row['Publisher'] . "<br/>" . $row['Year'] . "<br/>";

}

} else {

echo "0 results";

}

if (mysqli_num_rows($result2) > 0) {

while($row2 = mysqli_fetch_assoc($result2)){ // mysqli_fetch_assoc instead of mysqli_fetch_array because if you use array then you have to use id not name like $row2[0]

echo $row2['ISBN'] . "<br/>" . $row2['Title'] . "<br/>" . $row2['Author'] . "<br/>" . $row2['Publisher'] . "<br/>" . $row2['Year'] . "<br/>";

}

} else {

echo "0 results";

}

} else {

die(mysqli_connect_error());

}

mysqli_free_result($result);

mysqli_close($connect);

}

?>