繁体   English   中英

更新到MySQLi并遇到一些麻烦

[英]Updating to MySQLi and having some trouble

因此,我花了一些时间查看MySQLi,但在使用新功能更新脚本时遇到了麻烦。 该脚本用于动态下拉列表,使用通过JS发送给它的数据。 您可以在此处找到该脚本的实时版本,以了解我的想法。 我查看了我的代码,并将其与其他MySQLi示例进行了比较,但是我不确定自己哪里出错了。

现在,第一个下拉列表甚至都不会启动查询,PHP所做的只是返回预定义的结果,因为对于第一个选项而言,它更简单。 对我来说,很奇怪的是,即使完全不依赖MySQLi连接,即使第一个下拉列表现在也不起作用。 所有这些在更新之前都是有效的,仅供参考。

这是我的脚本:

$db = new mysqli($dbHost, $dbUser, $dbPass, $dbDatabase);

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

//prevents injections
//any order
isset($_GET['type'])?$type = urldecode($_GET['type']):"";
isset($_GET['source'])?$source = $db->real_escape_string(urldecode($_GET['source'])):"";
isset($_GET['range'])?$power = $db->real_escape_string(urldecode($_GET['range'])):"";
isset($_GET['setpoint'])?$setpoint = $db->real_escape_string(urldecode($_GET['setpoint'])):"";

//forms the query depending on what data is recieved through GET
//first option on the bottom; last option on the top to avoid conflicts 
if (isset($_GET['setpoint'])) {
    $query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' AND stp='$setpoint' ORDER BY model";
} elseif (isset($_GET['power'])) {
    $query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' ORDER BY model";
} elseif (isset($_GET['range'])) {
    $query = "SELECT DISTINCT pso FROM meters WHERE sio='$range' ORDER BY model";
} elseif (isset($_GET['source'])) {
    $query = "SELECT DISTINCT sir FROM meters WHERE sio LIKE '%$source%' ORDER BY sir";
}

//creates a result array from query results
isset($query)?$result = $db->query($query):"";

//outputs dropdown options dependent on what GET variables are set
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
    while ($row = $result->fetch_assoc()) {
        echo "<option value='" . $row['stp'] . "'>" . $row['stp'] . "</option>";
    $result->free();
    }
} elseif (isset($_GET['power'])) {
    echo "<option>Please Choose Setpoint Options</option>";
    while ($row = $result->fetch_assoc()) {
        $row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
        echo "<option value='" . $row['stp'] . "'>" . $row['stp'] . "</option>";
    $result->free();
    }
} elseif (isset($_GET['source'])) {
    echo "<option>Please Choose Input Range</option>";
    while ($row = $result->fetch_assoc()) {
        echo "<option value='" . $row['sir'] . "'>" . $row['sir'] . "</option>";
    $result->free();
    }
} elseif (isset($_GET['type']) && $_GET['type'] == "Digital") {
    echo "<option>Please Choose Input Source</option>";
    echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
    $result->free();
} elseif (isset($_GET['type']) && $_GET['type'] == "Analog") {
    echo "<option>Please Choose Input Source</option>";
    echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
    $result->free();
}

编辑:这是我不赞成使用的旧代码。

$con = mysql_connect($dbHost, $dbUser, $dbPass) or trigger_error("Failed to connect to MySQL Server. Error: " . mysql_error());

mysql_select_db($dbDatabase) or trigger_error("Failed to connect to database {$dbDatabase}. Error: " . mysql_error());


//prevents injections
//any order
isset($_GET['type'])?$type = urldecode($_GET['type']):"";
//$type = mysql_real_escape_string(urldecode($_GET['type']));
isset($_GET['source'])?$source = mysql_real_escape_string(urldecode($_GET['source'])):"";
isset($_GET['range'])?$power = mysql_real_escape_string(urldecode($_GET['range'])):"";
isset($_GET['setpoint'])?$setpoint = mysql_real_escape_string(urldecode($_GET['setpoint'])):"";

//forms the query depending on what data is recieved through GET
//first option on the bottom; last option on the top to avoid conflicts 
if (isset($_GET['setpoint'])) {
    $query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' AND stp='$setpoint' ORDER BY model";
} elseif (isset($_GET['power'])) {
    $query = "SELECT DISTINCT stp FROM meters WHERE sio='$range' AND pso='$power' ORDER BY model";
} elseif (isset($_GET['range'])) {
    $query = "SELECT DISTINCT pso FROM meters WHERE sio='$range' ORDER BY model";
} elseif (isset($_GET['source'])) {
    $query = "SELECT DISTINCT sir FROM meters WHERE sio LIKE '%$source%' ORDER BY sir";
}

//creates a result array from query results
isset($query)?$result = mysql_query($query):"";

//outputs dropdown options dependent on what GET variables are set
//first option on the bottom; last option on the top to avoid conflicts
if (isset($_GET['setpoint'])) {
    while ($row = mysql_fetch_array($result)) {
        echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
    }
} elseif (isset($_GET['power'])) {
    echo "<option>Please Choose Setpoint Options</option>";
    while ($row = mysql_fetch_array($result)) {
        $row{'stp'} = ucfirst($row{'stp'}); //capitalizes the first letter; necessary?
        echo "<option value='" . $row{'stp'} . "'>" . $row{'stp'} . "</option>";
    }
} elseif (isset($_GET['source'])) {
    echo "<option>Please Choose Input Range</option>";
    while ($row = mysql_fetch_array($result)) {
        echo "<option value='" . $row{'sir'} . "'>" . $row{'sir'} . "</option>";
    }
} elseif (isset($_GET['type']) && $_GET['type'] == "Digital") {
    echo "<option>Please Choose Input Source</option>";
    echo "<option value='RS232C'>RS232C</option><option value='RS422'>RS422</option><option value='RS485'>RS485</option><option value='current loop'>current loop</option>";
} elseif (isset($_GET['type']) && $_GET['type'] == "Analog") {
    echo "<option>Please Choose Input Source</option>";
    echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
}

您将在while循环中释放$result 这将导致循环在第二次迭代中失败。

既然您要在所有if中释放结果,为什么不只在最后一次执行一次呢?

...
} elseif (isset($_GET['type']) && $_GET['type'] == "Analog") {
    echo "<option>Please Choose Input Source</option>";
    echo "<option value='DC current'>DC Current</option><option value='DC voltage'>DC Voltage</option><option value='AC current'>AC Current</option><option value='AC voltage'>AC Voltage</option><option value='process'>Process</option><option value='thermocouple'>Thermocouple</option><option value='RDT'>rdt</option>";
}

$result->free();

但是,这并不能解释为什么模拟和数字仍然无法工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM