3

picture explanation I am trying to display the option_value(3rd colum) value into my code. I just tried testing mysqli_fetch_array method but it showed error undefined offset 1 and 2. Can you suggest any method to display the value separately into the page?

  <?php
$connection = mysqli_connect("localhost", "root","","adminpanel");

$query = "SELECT option_value FROM settings";
$query_run = mysqli_query($connection,$query);
if(mysqli_num_rows($query_run)>0)
{
    $row = mysqli_fetch_array($query_run)
    ?>
    <form action="code.php" method="POST">
     <div class="form-group">
        <label>Fine rate :</label>
        <input type="text" name="fine_rate" value="<?php  echo $row[0]; ?>" class="form-control">
    </div> 

    <div class="form-group">
        <label>Email template for issued books:</label>
        <textarea name="issue_template" class="form-control"><?php echo $row[1]; ?></textarea>
    </div>
    <div class="form-group">
        <label>Email template for return books:</label>
        <textarea name="return_template" class="form-control"><?php  echo $row[2]; ?></textarea>
    </div>  
    </form>
<?php 
} 
?>
Carol H
  • 37
  • 5
  • Does this answer your question? ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – El_Vanja Mar 26 '20 at 11:14

2 Answers2

3

When you do

$row = mysqli_fetch_array($query_run)

It creates an array for your result. Considering you are only selecting the option_value that array has only one element. Thus you can only access row[0]. As the wording implies, the $row variable only contains one row so you will have to fetch the next row using fetch array again. Something like this

 <?php
$connection = mysqli_connect("localhost", "root","","adminpanel");

$query = "SELECT option_value FROM settings";
$query_run = mysqli_query($connection,$query);
if(mysqli_num_rows($query_run)>0)
{
    $row = mysqli_fetch_array($query_run)
    ?>
    <form action="code.php" method="POST">
     <div class="form-group">
        <label>Fine rate :</label>
        <input type="text" name="fine_rate" value="<?php  echo $row[0]; ?>" class="form-control">
    </div> 
<?php
   $row = mysqli_fetch_array($query_run)
    ?>
    <div class="form-group">
        <label>Email template for issued books:</label>
        <textarea name="issue_template" class="form-control"><?php echo $row[0]; ?></textarea>
    </div>
<?php
   $row = mysqli_fetch_array($query_run)
    ?>
    <div class="form-group">
        <label>Email template for return books:</label>
        <textarea name="return_template" class="form-control"><?php  echo $row[0]; ?></textarea>
    </div>  
    </form>
<?php 
} 
?>
Gabriel Durac
  • 2,610
  • 1
  • 12
  • 13
1

This is not a good aproach as you can't be shure that the rows are tetched in that order every time. You can get all columns from that table and then sort the data for your needs:

$rows;
$finerate;
$email_temp_issue;
$email_temp_return;

$query = "SELECT * FROM settings";
$query_run = mysqli_query($connection,$query);
if(mysqli_num_rows($query_run)>0){
    $rows = mysqli_fetch_array($query_run);
}
foreach ($rows as $row){
    switch ($row['option_name']) {
    case "finerate":
       $finerate = $row['option_value'];
       break;
    case "email_temp_issue":
       $email_temp_issue= $row['option_value'];        
       break;
    case "email_temp_return":
        $email_temp_return= $row['option_value'];
        break;   
   }
}
    ?>
    <form action="code.php" method="POST">
     <div class="form-group">
        <label>Fine rate :</label>
        <input type="text" name="fine_rate" value="<?php  echo $finerate ; ?>" class="form-control">
    </div> 
<?php
   $row = mysqli_fetch_array($query_run)
    ?>
    <div class="form-group">
        <label>Email template for issued books:</label>
        <textarea name="issue_template" class="form-control"><?php echo email_temp_issue; ?></textarea>
    </div>
<?php
   $row = mysqli_fetch_array($query_run)
    ?>
    <div class="form-group">
        <label>Email template for return books:</label>
        <textarea name="return_template" class="form-control"><?php  echo $email_temp_return; ?></textarea>
    </div>  
    </form>
<?php 

This way it dosen't matter the id in the table or even if you have more records in that table

  • Hi, it shows Warning: Illegal string offset 'option_name'. Which error I'm in? – Carol H Mar 26 '20 at 12:40
  • You have to be more specific, at wich line it trows the error? Also, try to print_r the $rows and see if it's really an array. – Leonida-Alexandru Diaconu Mar 26 '20 at 13:04
  • At line: switch ($row['option_name']) { //error and yes the array shows: Array ( [0] => 1 [id] => 1 [1] => finerate [option_name] => finerate [2] => 1 [option_value] => 1 ) – Carol H Mar 26 '20 at 13:18