Troubleshooting mysqli_fetch_assoc()

橙三吉。 提交于 2019-12-25 02:22:52

问题


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.

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.

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.

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.

Code is listed below:

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

This is the result:

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.


回答1:


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

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

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



来源:https://stackoverflow.com/questions/20555061/troubleshooting-mysqli-fetch-assoc

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