0

guys. I'm having trouble with obtaining data from my database in order to display it in a textbox. It gives me an error which says Undefined variable: test on line 22

Below is my HTML code snippet that has the error

<tr>
    <td width="230">Item Name</td>
    <td width="10">:</td>
    <td width="294"><input id="item" name= "Item" type="text" class="resizedTextbox" value = "<?php echo $test['item'];?>" required></td>
</tr>

This is my php code snippet.

include("config.php");

if (isset($_GET['item_id']))
    {
    $sql = "SELECT * FROM inventory WHERE item_id  =" .$_GET['item_id'];
    $query = mysqli_query($db, $sql);
    $test = mysqli_fetch_array($query);
    }

I believe I don't have any syntax errors so I'm not sure what is causing the problem. Thanks in advance.

SPN
  • 1
  • First, you need to post enough of a code sample and enough information about your database table structure that we can diagnose the problem. This is not enough. Second, you are wide open to [**SQL injection**](https://www.owasp.org/index.php/SQL_Injection). You need to use prepared statements, rather than concatenating variables into your query. See [How can I prevent SQL injection in PHP?](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php?rq=1). – elixenide Sep 20 '17 at 02:53
  • You should put complete code with line numbers for clarity in question. I recommend modify it as mentioned by Ed. – SACn Sep 20 '17 at 03:16

4 Answers4

0

First of all fetch_array is not going to give you the associative array that you are currently trying to echo. ($test['item']), it will index it first. Try this:

$row = mysqli_fetch_assoc($result);

Loop through then use the index of the array like you were trying to do before

Read here for more information about the difference: Mysqli fetch_assoc vs fetch_array

http://php.net/manual/en/mysqli-result.fetch-assoc.php

http://php.net/manual/en/mysqli-result.fetch-assoc.php

Enoch
  • 921
  • 6
  • 15
  • I tried this out but it still gives me the undefined variable error when it is clearly defined. – SPN Sep 24 '17 at 13:18
0

Print $test in if condition and check all data displaying or not

Arzoo
  • 106
  • 5
  • Welcome to SO, this should be in comments not an answer. I know you don't have the comment privilege yet so please refrain from creating answers like this. Only thing going to happen is that community will down vote these answers which will render your account unable to ask questions. So best practice is to wait until you get the comment rights and post actuall answers to question to get up votes. – S4NDM4N Sep 20 '17 at 03:19
0

You must loop the query result

while ($test = mysqli_fetch_array($query))
{    
    echo "<tr>";
    foreach ($test as $key => $value) { ?>
        <tr>
<td width="230">Item Name</td>
<td width="10">:</td>
<td width="294"><input id="item" name= "Item" type="text" class="resizedTextbox" value = "<?php echo $value['item'];?>" required></td>
</tr>
   <?php  }
    echo "</tr>";
}
Walter
  • 81
  • 1
  • 9
0

Please drop using mysqli like the way you're using it's not safe. If you learned to use it like that then the person told you to code like that should be kicked. Most people use mysqli just like they used mysql they need to understand why the community introduced an improved version.

If you're using mysqli use it with prepared statements stop using it security holes by coding it in by your self.

To answer you problem try this, PHP

include("config.php");

if (isset($_GET['item_id'])){
  $sql = "SELECT * FROM inventory WHERE item_id  =?";
  $query = $db->prepare($sql);
  $query -> bind_param("i", $_GET['item_id'],PDO::PARAM_INT);
  $query -> execute();
  $getRow = $query -> fetch(PDO::FETCH_ASSOC);
}

HTML

<tr>
 <td width="230">Item Name</td>
 <td width="10">:</td>
 <td width="294"><input id="item" name= "Item" type="text" class="resizedTextbox" value = "<?php echo $getRow['item'];?>" required></td>
</tr>

If you're creating more than one then loop the fetch data using a while loop. PHP

include("config.php");

if (isset($_GET['item_id'])){
  $sql = "SELECT * FROM inventory WHERE item_id  =?";
  $query = $db->prepare($sql);
  $query -> bind_param("i", $_GET['item_id'],PDO::PARAM_INT);
  $query -> execute();  
}

HTML

<?php while($getRow = $query -> fetch(PDO::FETCH_ASSOC);){?>
<tr>
 <td width="230">Item Name</td>
 <td width="10">:</td>
 <td width="294"><input id="item" name= "Item" type="text" class="resizedTextbox" value = "<?php echo $getRow['item'];?>" required></td>
</tr>
<?php } ?>
S4NDM4N
  • 904
  • 2
  • 11
  • 26