对mysqli_fetch_assoc()进行故障排除 [英] Troubleshooting mysqli_fetch_assoc()

查看:153
本文介绍了对mysqli_fetch_assoc()进行故障排除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在将mysqli查询的结果转换为可以填充文本区域的变量时,我遇到了问题.我一直在尝试在网络上搜索所有此类示例,但似乎找不到解决我问题的方法.

I've got an issue with turning a mysqli query's results into variables that I can populate text areas with. I've been trying to search for examples of this all around the web and I cannot seem to find something that resolves my issue.

我沿着这些路线的最后一个项目使用mysql,而不是mysqli,因此它并不完全相同,而且我的项目变化很大,不允许复制粘贴代码.

My last project along these lines used mysql, not mysqli, so it's not exactly the same, plus my project is varied enough to not allow for a code copy-paste.

我试图与数据库建立连接,提取post元素中用作搜索元素的信息,并用结果填充文本字段.我什至还没有选择文本区域的内容,因为我还无法弄清楚如何返回可以添加到变量/数组中的值.

I'm trying to make a connection to a database, pull information used in a post element as a search element, and populate text fields with the results. I haven't even gotten to chancing text areas' contents yet, as I haven't been able to figure out how to return a value I can add into a variable/array.

我对PHP不太了解,所以我不了解每种语言.任何帮助将不胜感激.

I'm not very experienced with PHP and so I don't know every in-and-out of the language. Any assistance will be greatly appreciated.

下面列出了代码:

<html>
<head></head>

<script language="javascript" type="text/javascript">
function populateForm(jack) {
    document.getElementById('id_cubenum').value = jack;
    document.getElementById('id_firstname').value = resultArray[1];
    document.getElementById('id_lastname').value = resultArray[2];
    document.getElementById('id_username').value = resultArray[3];
}
</script>

<body>
<form>
ID: <input type="text" name="elem_cubenum" id="id_cubenum" maxlength="3" size="25" disabled><br>
<br>
First Name: <br>
<input type="text" name="elem_firstname" id="id_firstname"><br>
Last Name: <br>
<input type="text" name="elem_lastname" id="id_lastname"><br>
Username: <br>
<input type="text" name="elem_username" id="id_username" maxlength="8" size="8">    <br><br>
<hr>
Department:<br>
    <select name="elem_dept" id="id_dept">
        <option value="a">A</option>
        <option value="d">D</option>
        <option value="r">R</option>
        <option value="m">M</option>
        <option value="o">O</option>
    </select><br><br>
<hr>
Machine Name: <br>
<input type="text" name="elem_machname" id="id_machname" maxlength="16"><br>
Machine Inv#: <br>
<input type="text" name="elem_machnum" id="id_machnum" maxlength="5"><br>
Monitor Inv#: <br>
<input type="text" name="elem_monnum" id="id_monnum" maxlength="5"><br><br>
<hr>
Operating System:<br>
<select name="elem_os" id="id_os">
    <option value="winxp">Windows XP</option>
    <option value="win7">Windows 7</option>
    <option value="other">Other</option>
</select>
<br><hr>
</form>

<?php
$dbhost = "xxxxxxxxxxx";
$dbname = "xxxxxxxxxxx";
$dbuser = "xxxxxxxxxxx";
$dbpass = "xxxxxxxxxxx";
$p_getJack = $_POST['elem_seatNum'];

$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbnme);
$query = "SELECT * FROM locations WHERE locateid = '$p_getJack'";

echo '<img src="check.jpg" alt="Database Connection Made">' . 'Connection to the server was successfully made. <br>Jack Number: ' . $p_getJack . '<br><hr><br>';

if (mysqli_connect_errno()) {
    echo "Failed to connect! " . mysqli_connect_error();
}

$result = mysqli_query($conn, $query);
$row = mysqli_fetch_assoc($result);
printf("The results of %u and %s are listed.",$row["firstname"],$row["username"]);

mysqli_close($conn);
?>
</body>
</html>

这是结果:

Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/a7060806/public_html/form.php on line 66

关于我在这里做错什么的任何想法...?我知道连接信息是正确的,因为它是以前从页面复制粘贴的,并且在那儿工作得很好.

Any ideas on what I'm doing wrong here...? I know the connection information is correct because it is copy-pasted from the page before and it works just fine over there.

推荐答案

$dbname = "xxxxxxxxxxx";
$conn = mysqli_connect($dbhost,$dbuser,$dbpass,$dbnme);

注意如何将$dbname定义为数据库名称,而改用$dbnme

Notice how you defined $dbname as your database name and you used $dbnme instead

此外,您的代码容易受到SQL Injection的攻击,请确保您验证 SANITIZE 用户输入.请访问此处有关如何防止SQL注入或防止SQL注入的指南此处

Also, your code is vulnerable to SQL Injection please make sure you VALIDATE and SANITIZE user input. Visit a question here on how to prevent SQL Injection or a guide to prevent SQL Injection here

这篇关于对mysqli_fetch_assoc()进行故障排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆