Jimmy85 Posted May 10, 2019 Share Posted May 10, 2019 I have a html/php i am creating but getting the following error: [10-May-2019 09:42:13 UTC] PHP Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\inetpub\wwwroot\SupportDesk\php\Add_New_Engineer.php:88 Stack trace: #0 C:\inetpub\wwwroot\SupportDesk\php\Add_New_Engineer.php(5): create_eng() #1 {main} thrown in C:\inetpub\wwwroot\SupportDesk\php\Add_New_Engineer.php on line 88 Here is my code: <?php $server = htmlentities($_SERVER['PHP_SELF']); if (isset($_POST['submit'])) { $result = create_eng(); } ?> <!DOCTYPE html> <html lang="en"> <head> <title>Add A New Engineer | DDS Dashboard</title> <meta charset="utf-8" /> <link rel="icon" type="image/png" href="../images/favicon-16x16.png"/> </head> <body> <?php include('nav.php'); include('ConnectionDB.php'); ?> <div class="jumbotron"> <div class="container"> <h1><br><img src="../images/clipboard-customer.png"/> Add New Engineer:</br></h1> </div> </div> <form class="form-horizontal" role="form" id="add_eng" action="<?php $server ?>" method = "post"> <div class="form-group"> <!--<?php //display_msg($msg);?>--> <label for="EngineerName" class="col-sm-2 control-label">Engineer Name:</label> <div class="col-sm-6"> <input type="text" class="form-control" id="EngName" name="EngineersName" placeholder="Enter Engineer's Name"></input> <small class="form-text text-muted"> Enter the new engineer's name above to enter them into the database. </small> </div> <input name="submit" type="submit" value="Create Engineer" class="btn btn-primary default"></input> <input name="reset" type="Reset" value="Reset Form" class="btn btn-primary default"></input> </div> </form> <br><br> </body> </html> <?php $sql = "Select * from [Engineers] where [Engineer] not like '%\_%' Escape '\' order by [Engineer] asc"; $stmt = $conn->prepare($sql); $stmt->execute(); $results=$stmt->fetchAll(PDO::FETCH_ASSOC); ?> <div class="containerenglist"> <div class="col-sm-5"> <table id="alleng" class="table table-bordered"> <tr> <th>Active Engineer's Names:</th> </tr> <?php foreach ($results as $row) { echo "<tr><td>"; echo $row['Engineer']; echo "</td>"; echo "</tr>"; } ?> </table> </div> </div> </body> </html> <?php Function create_eng(){ global $msgID; global $msg, $conn; include('ConnectionDB.php'); $conn = null; $msgID = 0; //Get POST from submit $eng_name = $_POST['EngineersName']; //SQL Query $sql_count = "select count(*) as num from [Engineers] where [Engineer] = :eng_name"; //Query SQL Statement //echo $eng_name; $stmt = $conn->prepare($sql_count); $stmt->bindValue(':eng_name', $eng_name); //Execute SQL Statement $stmt->execute(); $results=$stmt->fetchAll(PDO::FETCH_ASSOC); If($results['num'] > 0){ echo $msg = "Engineer $eng_name exist's in the database"; $msgID = 1; }else{ $sql_insert = "insert into [Engineers](Engineer) values '$eng_name'"; //Prepare SQL Statement $stmt = $conn->Prepare($sql_insert); //Execute SQL Statement $stmt->execute(); $results=$stmt->fetchAll(PDO::FETCH_ASSOC); echo $msg = "Engineer $eng_name added to the database"; $msgID = 2; } } ?> Everything works correctly until i try submitting a name to the DB the line that is being pulled is $stmt = $conn->prepare($sql_count); Quote Link to comment https://forums.phpfreaks.com/topic/308688-assistance-required/ Share on other sites More sharing options...
gw1500se Posted May 10, 2019 Share Posted May 10, 2019 You are setting $conn to null then trying to use it as a connection object. As a guess, 'ConnectionDB.php' is where you instantiate the $conn object but then set it to null. Why? Probably you just need to remove that statement. Quote Link to comment https://forums.phpfreaks.com/topic/308688-assistance-required/#findComment-1566568 Share on other sites More sharing options...
Jimmy85 Posted May 10, 2019 Author Share Posted May 10, 2019 (edited) Whoops sorry meant to remove this Function create_eng(){ global $msgID; global $msg, $conn; include('ConnectionDB.php'); $conn = null; Should read Function create_eng(){ global $msgID; global $msg, $conn; $msgID = 0; Which it does now even after removing these lines i still get the same error [10-May-2019 14:02:00 UTC] PHP Fatal error: Uncaught Error: Call to a member function prepare() on null in C:\inetpub\wwwroot\SupportDesk\php\Add_New_Engineer.php:88 Stack trace: #0 C:\inetpub\wwwroot\SupportDesk\php\Add_New_Engineer.php(5): create_eng() #1 {main} thrown in C:\inetpub\wwwroot\SupportDesk\php\Add_New_Engineer.php on line 88 Edited May 10, 2019 by Jimmy85 spelling Quote Link to comment https://forums.phpfreaks.com/topic/308688-assistance-required/#findComment-1566570 Share on other sites More sharing options...
ginerjm Posted May 10, 2019 Share Posted May 10, 2019 Try adding some logic to test the results of the things you do, such as a query call, or a prepare call. You are coding in such a way as to ignore errors that can happen and then your script dies because you aren't handling the situation. And be sure that you have php error checking turned on and display it on your client while in development so that you can see errors quickly. PS - not a good idea to be inserting blocks of php code inside blocks of html. Separate your logic from your output/display to make it easier to follow and easier to code. Don't start your html page and then start doing php stuff and then back to html stuff. If you need to place some php results into the midst of your html (which we all have to do) you simply store the generated results into a variable and then place that var into the html where it belongs. Use of the heredocs construct will vastly help you to combine these vars with your blocks of html. Quote Link to comment https://forums.phpfreaks.com/topic/308688-assistance-required/#findComment-1566571 Share on other sites More sharing options...
gw1500se Posted May 10, 2019 Share Posted May 10, 2019 Show where you instantiate $conn and check the results. The error implies that $conn is not set. Quote Link to comment https://forums.phpfreaks.com/topic/308688-assistance-required/#findComment-1566572 Share on other sites More sharing options...
Jimmy85 Posted May 10, 2019 Author Share Posted May 10, 2019 My $conn is called from include('ConnectionDB.php'); <?php $user ="username"; $pass ="password"; $conn = new PDO("sqlsrv:server=JAMESPC\SUPPORTDB; database=SUPPORT_DB",$user, $pass); ?> Quote Link to comment https://forums.phpfreaks.com/topic/308688-assistance-required/#findComment-1566573 Share on other sites More sharing options...
gw1500se Posted May 10, 2019 Share Posted May 10, 2019 But you said you removed that include???????? Quote Link to comment https://forums.phpfreaks.com/topic/308688-assistance-required/#findComment-1566575 Share on other sites More sharing options...
Jimmy85 Posted May 10, 2019 Author Share Posted May 10, 2019 Yes from the function which i didnt think was required but not the <!DOCTYPE html> <html lang="en"> <head> <title>Add A New Engineer | DDS Dashboard</title> <meta charset="utf-8" /> <link rel="icon" type="image/png" href="../images/favicon-16x16.png"/> </head> <body> <?php include('nav.php'); include('ConnectionDB.php'); ?> As i use the PDO connection populate the current engineers name in the DB: ?php $sql = "Select * from [Engineers] where [Engineer] not like '%\_%' Escape '\' order by [Engineer] asc"; $stmt = $conn->prepare($sql); $stmt->execute(); $results=$stmt->fetchAll(PDO::FETCH_ASSOC); ?> <div class="containerenglist"> <div class="col-sm-5"> <table id="alleng" class="table table-bordered"> <tr> <th>Active Engineer's Names:</th> </tr> <?php foreach ($results as $row) { echo "<tr><td>"; echo $row['Engineer']; echo "</td>"; echo "</tr>"; } ?> </table> </div> </div> Quote Link to comment https://forums.phpfreaks.com/topic/308688-assistance-required/#findComment-1566577 Share on other sites More sharing options...
ginerjm Posted May 10, 2019 Share Posted May 10, 2019 (edited) Do a test of the connection call and the prepare call to be sure they both are succeeding. If you RTFM you will see that they each return a boolean result so all you need is to add an if statement after each call to determine if you have a problem. This kind of coding is essential to writing good scripts. And move the connection logic up to the top after you do your input validations and grab that input. Then use your php code to do the lookup and only THEN start sending out your html.. What is the point of sending out the beginning of an html page and then stopping and going backwards to do your db connection? And if you have to post again, it's a good time to show us your updated code, although we don't need to see your html code.... Edited May 10, 2019 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/308688-assistance-required/#findComment-1566578 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.