Jump to content

Recommended Posts

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 by requinix
please use [code] tags when posting code
Link to comment
https://forums.phpfreaks.com/topic/287068-undefined-variable-and-parameters/
Share on other sites

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

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

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 by mac_gyver
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.