0

How do i go about this

i have been able to get this to work good but i still have something not working perfectly. I want to be able to query the database from the respective ID but its not working

this is the db query i use

  <?php
  require_once('inc/config.php');
  $con = mysqli_connect($host, $user, $pass, $db) or die('Cannot Connect, Reason: '.mysqli_connect_error());

  $sql = "select * from crime_db_table";
  $result = $con->query($sql);
  if($result->num_rows > 0){
    while($row = $result-> fetch_assoc()){
      $id = $row['id'];
      $case_number = $row['case_number'];
    }
  }
  
  mysqli_close($con);
  
  ?>

this retrieves the id and the case number and i want to use the case number retrieved from here and pass the data to another url. i am using a Javascript function for this like this

<script language="javascript" type="text/javascript">
  function handleSelection(selectElement){
    var selectedOption = selectElement.value;
    if (selectedOption === 'modify') {
      //window.location.href = 'ModifyReport.php';
      window.open('ModifyReport.php?case_number=<?php echo $case_number;?>', '_blank'); 
    }else if(selectedOption === 'delete'){
      window.location.href = 'ModifyReport.php';
    }
  }
</script>

It passes, but it does not pass for the respective table

And here is the source code for the table

<?php
require_once('inc/config.php');
$con = mysqli_connect($host, $user, $pass, $db) or die ('Cannot Connect , Reason: '.mysqli_connect_error());
$sql = "select * from crime_db_table";
$result = $con->query($sql);
if($result->num_rows>0){
  while($row = $result->fetch_assoc()){
    $result_table = '<TR valign=top>
    <TD width=127 align=center><div class=wpmd>
    <div align=center><font size=2>'.$row['case_number'].'</font></div>
    </div>
    </TD>
    <TD width=109 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>'.$row['crime_type'].'</font></div>
    </div>
    </TD>
    <TD width=90 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>'.$row['fullname'].'</font></div>
    </div>
    </TD>
    <TD width=122 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>'.$row['address'].'</font></div>
    </div>
    </TD>
    <TD width=118 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>'.$row['telephone'].'</font></div>
    </div>
    </TD>
    <TD width=157 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>'.$row['incident_date'].'</font></div>
    </div>
    </TD>
    <TD width=103 height=26 align=center><div class=wpmd>
    <div align=center><a href=http://google.com title=><font size=2>Image.jpg</font></a></div>
    </div>
    </TD>
    <TD width=138 height=26 align=center><div class=wpmd>
    <div align=center><font size=2>'.$row['crime_desc'].'</font></div>
    </div>
    </TD>
    <TD width=138 height=26 align=center><font size=2><select name=dropdown id=dropdown onchange=handleSelection(this)><option>Please Select</option><option value=modify>Modify</option><option value=delete>Delete</option></select></font></TD>
    </TR>';

    echo $result_table;
  }
}else{
  echo "<div><font face=Verdana><B>No Results Found</B></font></div>";
}
  $con->close();
?>

What could possibly be wrong?

How can i pass the data from the php code to ModifyReport.php?case_number=<?php echo $case_number;?>'=

If I have 2 rows in my database or 3 for instance, when i click the dropdown, I have this URL

http://localhost/crimereport/reportsData.php?data=1

When i click the second, rather than change to 2, i still have it as thus http://localhost/crimereport/reportsData.php?data=1

Why?

James Z
  • 12,209
  • 10
  • 24
  • 44
  • 1
    After the loop is done, `$id` and `$case_number` are the values from the last row that was selected. Since you're not echoing any HTML inside the loop, you're ignoring all the other rows. – Barmar Jun 18 '23 at 22:14
  • 1
    You should loop through the rows and generate ` – Barmar Jun 18 '23 at 22:15
  • Could you be kind to show me what you mean , I am a bit lost here – Gilmeri Golano Jun 18 '23 at 23:33
  • See https://stackoverflow.com/questions/18953688/select-option-menu-read-from-database-and-use-its-values for example – Barmar Jun 19 '23 at 15:38
  • The loop for the db can fetch multiple rows. How is it supposed to launch several urls ? Or are you trying to generate a dropdown picklist? – Rohit Gupta Jun 20 '23 at 13:21

0 Answers0