-2

In our website, we have textbox to ask input values from user. If user input the data Q54497899, rest of the results must be shown from database. Rightnow, I want to add checkbox on each result. I tried to add with foreach but it still doesn't work for me. I will provide codes below which were written in the correlationwafer_result.php. Want to add checkbox beside each Q54497899

    <?php  
// ini_set("memory_limit","512M");
include("_dbconn.php");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/db_config.inc");
include("//sgewsnant21.amk.st.com/ewsweb/wwwroot/library/common7/standard_defines.inc");
session_start();

$productlotid = isset ($_GET['productlotid'])? $_GET['productlotid']:'';

$sql = "SELECT * FROM productdb.tbl_correlationwafer WHERE `lotid` = '$productlotid'";
    $result1 = mysqli_query($conn,$sql);
$cnt = 0;   
echo "<table id='corwafer'>";


    while ($row = mysqli_fetch_assoc($result1)) {
        echo "<tr>";
        echo "<th colspan='2'>Lot ID:</th>";
        echo "<th colspan='2'>Product:</th>";
        echo "<th colspan='4'>EWSFLOW </th>";
        echo "<th>Zone</th>";
        echo "</tr>";
        
        $field1name = $row["lotid"];
        $field2name = $row["product"];
        $field3name = $row["ewsflow"];
        $field4name = $row["zone"];

        echo '<tr> 
                  <td colspan="2">'.$field1name.'</td> 
                  <td colspan="2">'.$field2name.'</td> 
                  <td colspan="4">'.$field3name.'</td> 
                  <td >'.$field4name.'</td> 
                   
              </tr>';
              
        foreach($productlotid as $k => $v){

            if($k == $row["lotid"]){
                echo "<input type=\"checkbox\" id=\"chkproductlotid" . $cnt . "\" name=\"chkproductlotid\" value=\"$v\"><font face=\"arial\" size=\"2\" color=\"#3C5F84\">". $k . "</font>";
            }
        }
        $cnt++;   
    }

echo "</table>";


flush();
mysqli_close($conn);
?>
hhhz
  • 1
  • 2
  • I recommend you don't name your variables `$field1name`, `$field2name`, etc has they are hard to read, consider `$lotid`, `$product`. – Jacob Mulquin Aug 04 '22 at 02:33
  • **Warning:** You are wide open to [SQL Injections](https://php.net/manual/en/security.database.sql-injection.php) and should use parameterized **prepared statements** instead of manually building your queries. They are provided by [PDO](https://php.net/manual/pdo.prepared-statements.php) or by [MySQLi](https://php.net/manual/mysqli.quickstart.prepared-statements.php). Never trust any kind of input! Even when your queries are executed only by trusted users, [you are still in risk of corrupting your data](http://bobby-tables.com/). [Escaping is not enough!](https://stackoverflow.com/q/32391315) – Dharman Aug 04 '22 at 08:05

3 Answers3

1

Try :

$('chkproductlotid').val();
Mhmmad Rehan
  • 101
  • 1
  • 3
0
$productlotid = isset ($_GET['productlotid'])? $_GET['productlotid']:'';

Your $productlotid is more like a string not an array, does it?

MiYaMya
  • 24
  • 2
  • $productlotid = isset ($_GET['productlotid'])? $_GET['productlotid']:''; is that I need to get user input data from input="text". – hhhz Aug 04 '22 at 02:30
  • Yes, but what I mean is you can't use foreach on it because it's type is string not array. foreach($productlotid as $k => $v) <= it doesn't work – MiYaMya Aug 04 '22 at 02:33
  • So, that ($productid as $k => $v ) is not needed? And, How can I make checkbox for each? – hhhz Aug 04 '22 at 02:36
  • Could you explain what does $row["lotid"] work in checkbox? What do you want to print in "". $k . "" – MiYaMya Aug 04 '22 at 02:42
  • I suggest you use html to simulate what you want, it will be easier for us to help you. – MiYaMya Aug 04 '22 at 02:44
  • I copied that line from the other file and tbh, I'm new to this field and I tried to understand how to do that's why. Literally, I'm confused that how can I add those checkbox on each row. – hhhz Aug 04 '22 at 02:46
  • You could just add one more line under echo "Zone"; like checkbox; put your checkbox into a then put this under '.$field4name.' delete the tag because you don't need it. – MiYaMya Aug 04 '22 at 02:52
  • Noted, I will give a try – hhhz Aug 04 '22 at 02:54
0

You should put your checkbox inside td, and I think your checkbox value should be able to take from the first While Loop, no need that Foreach..

kelvin
  • 1
  • 1