0
<?php 
    if(isset($_POST['search']))
    {
        $department = $_POST['department'];
        $class = $_POST['class'];
        $i = 1;
        $sql = "SELECT `student_id` , `name`, `htno`, `department`, `class` , `image_name` FROM student_detail where department=$department && class=$class"; 
        $query = mysqli_query($conn, $sql);
        if (!$query) {
            die ('SQL Error: ' . mysqli_error($conn));
        }
        while ($row = mysqli_fetch_array($query)) {?>

<tr>
    <td><?php echo $i++; ?></td>
    <td><?php echo $row['name']; ?></td>
    <td><?php echo $row['htno']; ?></td>
    <td><?php echo $row['department']; ?></td>
    <td><?php echo $row['class']; ?></td>
    <td>
        <button data-toggle="modal" data-target="#view-modal" data-id="<?php echo $row['student_id']; ?>" id="getStudent" class="btn cur-p btn-info"><i class="glyphicon glyphicon-eye-open"></i> View</button>                                             
        <a href="<?php echo BASE_URL; ?>/controller/delete.php?student_id=<?php echo $row['student_id']; ?>" class="btn cur-p btn-danger" onClick="return confirm('Are you sure you want to delete?')">Delete</a></button>
    </td>
</tr>

<?php }
    if ($result === false) {
    echo "No Data Found";
    }
}
?>

Getting error while using this code

SQL Error: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'A' at line 1

What is wrong?

James Z
  • 12,209
  • 10
  • 24
  • 44

3 Answers3

3

You should use prepared statements.

if(isset($_POST['search']))
    {
        $department = $_POST['department'];
        $class = $_POST['class'];
        $i = 1;
        $query = "SELECT 
            `student_id`,
            `name`,
            `htno`,
            `department`,
            `class`
            FROM student_detail 
            WHERE department = ? && class = ? ";

            $stmt = $conn->prepare($query);
            $stmt->bind_param('ss', $department, $class);  
            $stmt->execute();
            $result = $stmt->get_result();

        if (!$result) {
            die ('SQL Error: ' . mysqli_error($conn));
        }
        while ($row = $result->fetch_assoc()) { ?>

<tr>
    <td><?php echo $i++; ?></td>
    <td><?php echo $row['name']; ?></td>
    <td><?php echo $row['htno']; ?></td>
    <td><?php echo $row['department']; ?></td>
    <td><?php echo $row['class']; ?></td>
    <td>
        <button data-toggle="modal" data-target="#view-modal" data-id="<?php echo $row['student_id']; ?>" id="getStudent" class="btn cur-p btn-info"><i class="glyphicon glyphicon-eye-open"></i> View</button>                                             
        <a href="<?php echo BASE_URL; ?>/controller/delete.php?student_id=<?php echo $row['student_id']; ?>" class="btn cur-p btn-danger" onClick="return confirm('Are you sure you want to delete?')">Delete</a></button>
    </td>
</tr>

<?php }
    if ($result === false) {
    echo "No Data Found";
    }
}
Joseph_J
  • 3,654
  • 2
  • 13
  • 22
2

In your SQL, you're not enclosing the variables within quote marks:

where department=$department && class=$class

this will translate to

where department=My Department && class=My Class

(depending on the data in your variables of course).

You need to put quotes around the variables, and then you'll stop seeing this particular error:

where department='$department' AND class='$class'

also, use AND rather than &&

Finally - and most importantly, this is not good SQL practise, as you are open to SQL injection attacks, ie. where someone passes a string into the variable to try and break the query, or worse, edit/delete your data.

You would do well looking at how to construct SQL queries using prepared statements. There's a great guide here:

https://websitebeaver.com/prepared-statements-in-php-mysqli-to-prevent-sql-injection

Matthew Knight
  • 631
  • 5
  • 13
0
$sql = "SELECT `student_id` , `name`, `htno`, `department`, `class` , `image_name` FROM student_detail where department=$department && class=$class"; 

try changing that to

$sql = "SELECT student_id , name, htno, department, class , image_name FROM student_detail WHERE department= " . $department . " AND class=" . $class . ";"; 

Edit: As Aniket Sahrawat suggested, prepared statements is something you need to implement.

$sth = $dbh->prepare('SELECT name, colour, calories, fruit WHERE calories < ? AND  colour= ?');
$sth->execute(array(150, 'red'));

Edit2: Thanks Nigel Ren for noticing my mistake on the prepared statement ;p

Charis Moutafidis
  • 363
  • 1
  • 4
  • 17