ITNerd1002 Posted March 18, 2014 Share Posted March 18, 2014 (edited) I am having trouble with my php code and end up getting the following two problems. Below are the two problems I keep getting which are marked with asterisks. **Warning: mysqli_query() expects parameter 1 to be mysqli, null given in **Notice: Undefined variable: con in This is my php code: <?php /** * WARNING! * This EXAMPLE code contains severe SECURITY ISSUES * which should be addressed before real-life usage!! * * These issues have been ignored intentionally to * simplify the code so we can focus on the topic * in discussion. */ // Lets test if the form has been submitted if(isset($_POST['SubmitCheck'])) { // The form has been submited // Check the values! // if($_POST['Username'] == "John" and $_POST['Password'] == "Doe") { // User validated! //Variable declaration //$username = $_POST["Username"]; //$first = $_POST["First"]; $dev_id = $_POST["dev_id"]; $dev_name = $_POST["dev_name"]; //$term_id = $_POST["term_id"]; $cust_id = $_POST["cust_id"]; $cust_fname = $_POST["cust_fname"]; $cust_lname = $_POST["cust_lname"]; //$date_of_loan = $_POST["date_of_loan"]; //$date_returned = $_POST["date_returned"]; $result = mysqli_query($con, "SELECT '".$dev_id."', ta.TERM_ID, '".$cust_id."', '".$dev_name."', '".$cust_fname."', '".$cust_lname."', DATE_OF_LOAN, DATE_RETURNED from device d join abc_audit au on d.DEV_ID = '".$dev_id."' join term_agreement ta on au.TERM_ID = ta.TERM_ID join customer cu on ta.CUST_ID = '".$cust_id."' where au.abc_status = 'O'"); echo "<table border='1'> <tr> <th>DEV_ID</th> <th>DEV_NAME</th> <th>TERM_ID</th> <th>CUST_ID</th> <th>CUST_FNAME</th> <th>CUST_LNAME</th> <th>DATE_OF_LOAN</th> <th>DATE_RETURNED</th> </tr>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['DEV_ID'] . "</td>"; echo "<td>" . $row['DEV_NAME'] . "</td>"; echo "<td>" . $row['TERM_ID'] . "</td>"; echo "<td>" . $row['CUST_ID'] . "</td>"; echo "<td>" . $row['CUST_FNAME'] . "</td>"; echo "<td>" . $row['CUST_LNAME'] . "</td>"; echo "<td>" . $row['DATE_OF_LOAN'] . "</td>"; echo "<td>" . $row['DATE_RETURNED'] . "</td>"; echo "</tr>"; } mysqli_close($con); //$con=mysqli_connect("localhost", "root", "", "abc"); //mysqli_query($con,"INSERT INTO Customer (CUST_ID, CUST_FNAME) VALUES ('".$username."', '".$first."' ) "); //mysqli_close($con); // } // else { // User info invalid! // echo "Sorry mate, try again!"; // } } else { // The form has not been posted // Show the form ?> <form id="Form1" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> Device_ID: <input type="integer" name="dev_id"><br> Device_Name: <input type="text" name="dev_name"><br> Customer_ID: <input type="integer" name="cust_id"><br> Customer_Firstname: <input type="text" name="cust_fname"><br> Customer_Lastname: <input type="text" name="cust_lname"><br> <br> <input type="hidden" name="SubmitCheck" value="sent"><input type="Submit" name="Form1_Submit" value="Submit"> </form> <?php } ?> Edited March 18, 2014 by requinix please use [code] tags when posting code Quote Link to comment https://forums.phpfreaks.com/topic/287068-undefined-variable-and-parameters/ Share on other sites More sharing options...
Psycho Posted March 18, 2014 Share Posted March 18, 2014 Both errors appear to be due to the same problem, I don't see where $con is defined before this line is executed: $result = mysqli_query($con, "SELECT '".$dev_id."', . . . Quote Link to comment https://forums.phpfreaks.com/topic/287068-undefined-variable-and-parameters/#findComment-1473051 Share on other sites More sharing options...
Ch0cu3r Posted March 19, 2014 Share Posted March 19, 2014 (edited) This line is responsible for connecting to mysql. //$con=mysqli_connect("localhost", "root", "", "abc"); However it is commented out (meaning it will be ignored) and secondly you need to be connected to mysql before performing any database operations, such as a query. To solve the error, you need to first uncomment it (by deleting the // infront of it), then make sure you give it the correct database credentials (hostname, username, password and database name) and then move it so it is a line before performing a query on the database. Edited March 19, 2014 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/287068-undefined-variable-and-parameters/#findComment-1473099 Share on other sites More sharing options...
ITNerd1002 Posted March 19, 2014 Author Share Posted March 19, 2014 Alright I uncommented out the line and I cannot believe that I missed that. Even though that is working now it stil doesnt pull the data back but just shows the column titles of the table. Quote Link to comment https://forums.phpfreaks.com/topic/287068-undefined-variable-and-parameters/#findComment-1473224 Share on other sites More sharing options...
mac_gyver Posted March 19, 2014 Share Posted March 19, 2014 most of the things you have selected in your query are literal string values. each item in the select list that are between single-quotes will be the literal strings those variables contain, not the values from the corresponding columns. i recommend that you form the sql query statement in a php variable, then echo out that variable so that you can see what it actually is and so that you can copy/paste it to run it directly against your database using your favorite database management tool (phpmyadmin or similar.) Quote Link to comment https://forums.phpfreaks.com/topic/287068-undefined-variable-and-parameters/#findComment-1473226 Share on other sites More sharing options...
ITNerd1002 Posted March 19, 2014 Author Share Posted March 19, 2014 Yeah dude I do not really understand what that means I am pretty new to php and I have been teaching myself. I appreciate the help but I really have no idea what you mean sorry. Quote Link to comment https://forums.phpfreaks.com/topic/287068-undefined-variable-and-parameters/#findComment-1473228 Share on other sites More sharing options...
mac_gyver Posted March 19, 2014 Share Posted March 19, 2014 (edited) this is real basic stuff and means you skipped over the first few php chapters where it talks about variables, assigning values to them, and echoing them. // build the sql query in a php variable $query = "SELECT '".$dev_id."', ta.TERM_ID, '".$cust_id."', '".$dev_name."', '".$cust_fname."', '".$cust_lname."', DATE_OF_LOAN, DATE_RETURNED from device d join abc_audit au on d.DEV_ID = '".$dev_id."' join term_agreement ta on au.TERM_ID = ta.TERM_ID join customer cu on ta.CUST_ID = '".$cust_id."' where au.abc_status = 'O'"; // echo the sql query for debugging purposes echo $query; // run the query $result = mysqli_query($con,$query); Edited March 19, 2014 by mac_gyver Quote Link to comment https://forums.phpfreaks.com/topic/287068-undefined-variable-and-parameters/#findComment-1473229 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.