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 
} 
?>
1

2 Answers 2

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 
} 
?>
Sign up to request clarification or add additional context in comments.

1 Comment

May I know how about updating the column value, how do I write in SQL?
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

3 Comments

Hi, it shows Warning: Illegal string offset 'option_name'. Which error I'm in?
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.
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 )

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.