从mysql表获取值到下拉菜单(未定义变量)

时间:2015-11-28 15:49:15

标签: php html mysqli

这可能是一个非常简单的问题需要解决,但是我已经坚持了一段时间,我无法弄清楚我的生活中我的代码有什么问题。这可能只是一个语法错误,但我从其他问题中收集了这些代码,它应该可以工作,但我不断得到错误: - 未定义的变量:mysqli和 - 调用非对象的成员函数query() ,两行都在" $ result = $ mysqli->查询($ sql);"。

这是我的代码片段,我在其中设置了下拉菜单。



<label class="control-label" for="formInput85">Professor</label>
                                           
<?php

$sql = "SELECT name FROM professores";
$result = $mysqli->query($sql);

echo "<select class=".'"form-control"'.">";
while ($row = $result->fetch_assoc()) {
echo "<option value='" . $row['name'] . "'>" . $row['name'] . "</option>";
}
echo "</select>";

?>
&#13;
&#13;
&#13;

这是我的代码在页面的最开头,即在phpmyadmin中连接到我的数据库。

&#13;
&#13;
<?php
	session_start();
	echo $_SESSION['name'];
	
	$servername = "localhost"; 
	$username = "root"; 
	$password = ""; 
	$dbname = "teste"; 

	$conn = new mysqli($servername,$username,$password,$dbname); 
?>
&#13;
&#13;
&#13;

感谢您的帮助!如果您有关于如何将所选值发送到表格的另一行的任何提示,我很高兴地欣赏它,因为这将是我的下一步:)

1 个答案:

答案 0 :(得分:1)

在这种情况下,

GPE2不是正确的变量。所以这一行:

#include <stdio.h>
#define g 9.81

void main(void)
{
    // step 1: variable declaration
    float GPE1, GPE2, h;
    int m;
    // step 2: input
    printf("Mr Lim: The higher the object, the greater the Gravitational Potential Energy. \n");
    printf("Let me prove it to you with this simple physics question! \n\n");
    printf("An object is located at the surface of the earth. \n");
    printf("Calculate the Gravitational Potential Energy (in J) the body possesses. \n\n\n");
    printf("Enter the mass of the body (in kg): ");
    scanf("%d", &m);
    printf("Enter the height of the body (in m): ");
    scanf("%f", &h);

    if (h > 0) {
        // step 3: calculation
        GPE1 = m * g * h;
        // step 4: output
        printf("The Gravitational Potential Energy, GPE (in J) = %.2f joules. \n\n", GPE1);
    }
    else {
        printf("The Gravitational Potential Energy is undefined! \n");
    }

    printf("Mr Lim: Now, to prove my statement is valid \n");
    printf("Find the GPE a body possesses, when its height is greater. \n");
    printf("Enter the mass of the body (in kg): ");
    scanf("%d", &m);
    printf("Enter the height of the body (in m): ");
    scanf("%f", &h);

    if (GPE2 > GPE1) {
        GPE2 = m * g * h;
        printf("The Gravitational Potential Energy, GPE (in J) = %.2f joules. \n\n", GPE2);
        printf("Mr Lim: See class, I told you! \n");
    }
    else {
        printf("Class is not impressed. \n");
        printf("Mr Lim should retire! \n");
    }
}

必须成为

$mysqli