我遇到了将mysqli查询的结果转换为可以填充文本区域的变量的问题。我一直在尝试在网络上搜索这个例子,我似乎无法找到解决我的问题的东西。
我沿着这些行的最后一个项目使用的是mysql,而不是mysqli,所以它不完全相同,而且我的项目变化很大,不允许进行代码复制粘贴。
我正在尝试建立与数据库的连接,将post元素中使用的信息作为搜索元素,并使用结果填充文本字段。我还没有去查看文本区域的内容,因为我无法弄清楚如何返回我可以添加到变量/数组中的值。
我对PHP不是很有经验,所以我不知道每一种语言的内外都是如此。任何帮助将不胜感激。
代码如下:
<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
关于我在这里做错了什么的想法......?我知道连接信息是正确的,因为它之前是从页面复制粘贴的,并且在那里工作得很好。