PHP - Create check box by using the records from MySQL datebase as values

前端 未结 3 1175
旧时难觅i
旧时难觅i 2021-01-28 04:34

i am a newbie in php programming and i cant figure out where i have gone wrong as my php code wont execute.

As the title says i am trying to create check boxes in my sit

相关标签:
3条回答
  • 2021-01-28 04:45

    Welcome to PHP!

    An error is that you're missing the semicolon that's needed after any php function (such as echo)

    <?php echo $line['room']; ?>
    

    And there's the missing PHP tags around the closing }

    A third error is that you're not telling mysqli which connection to run the query on it should have:

    mysqli_query($dbCon, $sql);
    

    Apart from that it looks good, personally I prefer to use a PDO connection but mysqli is still good, but there are a few formatting tricks that can help prevent problems.

    For example it's always a good idea to use back-ticks (`)

    So:

    $sql = "SELECT `room` FROM `campus`";
    

    However, for this it might be best to use the * query. Which selects everything from the column so:

    $sql = "SELECT * FROM `campus`";
    

    The reason is how you're getting the data, you're telling PHP to create an array using the results.. but you've only given it one piece of data for each row. So if you give it all of the data it just makes it a little easier to use.

    Here's the full code:

    <?php $dbCon = mysqli_connect("localhost", "root", "root", "my computer");
    // Check connection
    if (mysqli_connect_errno()){
        echo "Failed to connect to MySQL: " . mysqli_connect_error();
    }?>    
    <html>  
        <body>
            <form name="aform">
                Choose a room:
                <?php
                $sql = "SELECT * FROM `campus`";
                $result = mysqli_query($dbCon, $sql);
    
                while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) { ?>
                    <input type="checkbox" name="car" value="<?php echo $line['room']; ?>"
                <?php } ?>
            </form>
        </body>
    </html>
    

    Also, if you're interested, here's how it'd be done in PDO:

    <?php
    try{
        $con = new \PDO("mysql:host=" . 'localhost' . ";dbname=" . 'My Computer', 'root', 'root');
    }catch(PDOException $e){
        echo "Connection Failed";
        die();
    } ?>
    <html>  
        <body>
            <form name="aform">
                Choose a room:
                <?php
                $result = $con->prepare("SELECT * FROM `campus`")
                $result->execute();
                while ($row = $result->fetch()) { ?>
                    <input type="checkbox" name="car" value="<?php echo $row['room']; ?>"
               <?php } ?>
            </form>
        </body>
    </html>
    

    Still not working? Feel free to comment and I'll see what's up :)

    Thanks, P110

    0 讨论(0)
  • 2021-01-28 04:50

    Try with this

    <?php
    
    $sql = "SELECT room FROM campus";
    $result = mysqli_query($sql);
    
    $campusArray = mysqli_fetch_array($result, MYSQLI_ASSOC);
    foreach ($campusArray as $campus): ?>
        <input type="checkbox" name="car" value="<?php echo $campus['room'];?>" />
    <?php endforeach; ?>
    

    I hope with this you can solve your problem.

    alternative syntax is excellent for improving legibility (for both PHP and HTML!) in situations where you have a mix of them.

    http://ca3.php.net/manual/en/control-structures.alternative-syntax.php

    0 讨论(0)
  • 2021-01-28 04:58

    You're not closing the while loop properly. Close the while loop as follow.

    <?php
    
        $sql = "SELECT room FROM campus";
        $result = mysqli_query($sql);
    
        while ($line = mysqli_fetch_array($result, MYSQL_ASSOC)) {
    
        ?>
    
        <input type="checkbox" name="car" value="<?php echo $line['room']?>" />
    
       <?php
        }
        ?>
    
    0 讨论(0)
提交回复
热议问题