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

牧云@^-^@ 提交于 2019-12-20 07:24:26

问题


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 site however the values will come from the mysql database.

I have a table named “campus” in MySQL database and it has 2 coloumns called id and room.

database [![Database][1]][1] http://i.imgur.com/uLP6niJ.png

current output [![Current Output][2]][2] http://i.imgur.com/cSOYPme.png

below is my code:

<?PHP

$hostname = "localhost";
$username = "root";
$password = "root";
$databaseName = "my computer";

$connect = mysqli_connect($hostname, $username, $password, $databaseName);


// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }



?>


<html>  
<body>


<form name="aform">


Choose a room:


<?php
  $s = '';
  $j = 0;
  if ($q = $connect->query("SELECT * FROM `campus`")) {
    while ($line = $q->fetch_assoc()) {
      $s.= '<input type="checkbox" name="car'.$j.'" value="'.$line['room'].'">';
    }
  }
  echo $s;
?>

</form>


</body>
</html>

回答1:


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



回答2:


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




回答3:


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



来源:https://stackoverflow.com/questions/36513009/php-create-check-box-by-using-the-records-from-mysql-datebase-as-values

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!