-2

I'm currently learning how to use PHP and SQL in tandem. I'm trying to use PHP to display data from my database straight onto my website. I know it's probably rudamentary, but I have scoured the internet for the last two days and nothing has worked for me. I have even tried asking chatGPT but that thing has only driven me more insane.

<?php include "head.php";
$currentPage = 'contact';
require 'db_class.php';
$db = new sql_class(); ?> 
    <header>
    <div id="banner">
        <?php include "header.php";
        
        $result = $db->execute("SELECT subject_name FROM subjects WHERE id = 1");
        // Fetch the row from the result
        $row = mysqli_fetch_assoc($result);
        
        // Store the subject name in a variable
        $subject_name = $row['subject_name'];
        ?>
    </div>
    </header>
    <body>
        <h1>Contact Us</h1>
        <form method="post">
            <input type="text" id="firstname">
            <label for="firstname">First name</label><br>
            <input type="text" id="lastname">
            <label for="lastname">Last Name</label><br>
            <input type="text" id="email" required>
            <label for="email">Email</label><br>
            <input type="text" id="phone" required>
            <label for="phone">Phone</label><br>
            <select id="subject">
                <option value="comment">Comment</option>
                <option value="doctrine">Doctrine</option>
                <option value="So"><?php echo $result;?></option>
            </select><br>
            <textarea name="" id="message" cols="30" rows="10">Your Message</textarea><br>
            <input type="submit">
        </form>
    </body>
    <?php include 'footer.php'?>
</html>

So, in conclusion, my main problem is; I want to display the word "comment" from my SQL database table named "subjects." If you're wondering why I'm not having the entire dropdown menu be displayed from my database it's because I'm just learning right now and if I can get this to work, then setting up a while loop to display the rest of the menu should not be too hard.

Thank you for any criticism or advice you may have for me.

I have went onto youtube and chatGPT. No responses from them have worked. The comments you see are actually from chatGPT's code.

9
  • For those wondering what this is for, I'm making a website for my church, I'm experimenting here for now. Commented Apr 6 at 3:09
  • 1
    What do you mean by "without all the array jargon"?
    – Barmar
    Commented Apr 6 at 4:54
  • What are you expecting <?php echo $result;?> to show? I think you want <?php echo $subject_name; ?>
    – Barmar
    Commented Apr 6 at 4:57
  • @Barmar By array Jargon I mean it displays the table name and other stuff that I don’t need. I put $result because if I put $subject_name nothing displays in the spot in the drop down menu. Using $result has been the closest thing I’ve gotten to what I wanted. Commented Apr 6 at 5:11
  • $result isn't a string, it's a mysqli_result object. You can only use it in mysqli functions like mysqli_fetch_assoc(). If you're getting an empty string when you use $subject_name, that's the value that's in the database, it's not a problem with the code.
    – Barmar
    Commented Apr 6 at 5:27

1 Answer 1

0

Looking at the above code there are a few obvious mistakes which I have attempted to correct in the following code, notably the errant HTML content that existed outwith the body and the misuse of the $result variable from the sql operation. That sql operation uses the word execute yet the following commands suggest a regular query rather than a prepared statement was used but without details from the sql_class class it's not clear. Perhaps this might help however?!

<?php

    include 'head.php';
    $currentPage = 'contact';
    
    /*
        this is a little confusing - the file and the class
        have completely different names.
    */
    require 'db_class.php';
    $db = new sql_class(); 
    
    
    /*
    $result = $db->execute('SELECT subject_name FROM subjects WHERE id = 1');
    $row = mysqli_fetch_assoc($result);
    $subject_name = $row['subject_name'];
    */
    
    
    /*
        As no details of this class have been given it is 
        not 100% clear what your custom methods do, hence 
        using vanilla mysqli below.
    */
    $sql='SELECT `id`, `subject_name` FROM `subjects`';
    $stmt=$db->prepare( $sql );
    $stmt->execute();
    # associate fields in sql results with bound variables for later use.
    $stmt->bind_result( $id, $subject );
    
?> 
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>Contact Us</title>
    </head>
    <body>
        <!-- move HTML content to within BODY --> 
        <header>
            <div id='banner'>
            <?php 
                include 'header.php';
            ?>
            </div>
        </header>
        
        <h1>Contact Us</h1>
        
        <!--
            Whilst not completely necessary
            form elements should have `name` attributes
            so that they exist within a regular form 
            submission.
        -->
        <form method='post'>
            <input type='text' name='firstname' id='firstname'>
            <label for='firstname'>First name</label>
            <br>
            
            <input type='text' name='lastname' id='lastname'>
            <label for='lastname'>Last Name</label>
            <br>
            
            <input type='text' name='email' id='email' required>
            <label for='email'>Email</label>
            <br>
            
            <input type='text' name='phone' id='phone' required>
            <label for='phone'>Phone</label>
            <br>
            
            <select name='subject' id='subject'>
                <option value='comment'>Comment
                <option value='doctrine'>Doctrine
                <?php
                    /*
                        iterate through results returned by sql query, using
                        bound variables described above. The variable name 
                        does not need to actually match the db column name
                        it is the order in which they are defined that is 
                        important.
                        
                        In this dropdown the ID is used for the value rather
                        than the actual text of the comment - this is easily
                        changed.
                    */
                    while( $stmt->fetch() ){
                        printf('<option value="%d">%s', $id, $subject );
                    }
                    $stmt->free_result();
                    $stmt->close();
                ?>
            </select>
            <br>
            
            <textarea name='message' id='message' cols='30' rows='10'>Your Message</textarea><br>
            
            <input type='submit'>
        </form>
        
        <!-- move FOOTER to within BODY -->
        <?php include 'footer.php'?>
    </body>
</html>
3
  • Your coding skills are evident. I also appreciate your answer and the time you took to respond. However, when I put your code in place of my own and I refresh my website, I only get a blank screen. The only bug I seem to get is from "$stmt= $db-> prepare($sql);" My editor says that the "prepare" function has not been defined yet. I may need to speak to the person that setup my database and server for me. In the meantime is there anything I can do? Commented Apr 6 at 16:39
  • prepare is a native method for the mysqli api (also for PDO) - your sql_class is not defined so it's methods are unknown, hence using vanilla mysqli. I suspect there is a conflict with the sql_class methods - if you have error reporting switched on there is likely a meaningful error Commented Apr 7 at 7:06
  • Incidentally but importantly I used prepare method because it looks likely that you will wish to use alternate values for id in the sql query at a later stage and to do so you are effectively using user supplied data in the sql which is extremely dangerous unless you use a prepared statement with bound parameters. Commented Apr 7 at 7:19

Not the answer you're looking for? Browse other questions tagged or ask your own question.