Markob1984 Posted November 28 Share Posted November 28 Hey Guys I'm having problems inserting customer name in to my database it isn't showing any errors on my site just the site looks all messed but like there is something wrong. there is other fields but im not using them at the moment? <?php include('styles/top.php'); ?> <div id="left"> <?php if ($user_level !=1){ } else { $sql = "SELECT SUM(status='Pending')as pending , SUM(status='Layon') as layon , SUM(status='Late') as late , SUM(status='Paid')as paid FROM orders"; $result = mysqli_query($con, $sql); $row = mysqli_fetch_assoc($result); echo " <form action='pending.php'> <input type=\"submit\" class=\"button\" value=\"Pending Payments ( {$row['pending']} )\"> </form> <form action='total-layons.php'> <input type=\"submit\" class=\"button\" value=\"Total Layons ( {$row['layon']} )\"> </form> <form action='total-late.php'> <input type=\"submit\" class=\"button\" value=\"Total Late Payments ( {$row['late']} )\"> </form> <form action='total-paid.php'> <input type=\"submit\" class=\"button\" value=\"Total Paid Payments ( {$row['paid']} )\"> </form> <form action='add-customer.php'> <input type=\"submit\" class=\"button2\" value=\"Add Customer\"> </form> "; ?> <?php } ?> </div> <div id="right"> <div id="view_box"> <ul> <li><img src="images/1.png" /></li> <!-- <li><img src="pics/2.jpg" /></li> --> <!-- <li><img src="pics/3.jpg" /></li> --> </ul> </div> <div id="button"> <ul> <!-- <li><button class="button" scroll_value="0">*</button></li> -- > <!-- <li><button class="button" scroll_value="600">*</button></li> --> <!-- <li><button class="button" scroll_value="1200">*</button></li> --> </ul> </div> <hr /> <?php if ($user_level !=1){ echo "No Access Allowed Admin Only "; } else { ?> <?php $form = " <form action='add-customer.php' method='post'> <table cellspacing='10' cellpadding='10'> <tr> <td>Customer Name</td> <td><input type='text' name='customer_name' /></td> </tr> <tr> <td></td> <td><input type='submit' name='submit' class='button' value='Add New Order To The system'/></td> </tr> </table> </form>"; if (isset($_POST['submit'])) { $name = $_POST['customer_name']; if ($name) { incude (''); $query = "INSERT INTO customers (customer_name) VALUES ('$name')"; $result = mysqli_query($con, $query); echo "<font color='Yellow'>Thank you, Information has been saved to the database <a href='index.php'>Click Here</a> To Return to Previous Page!.</font> <br>"; } else echo "Please enter customers name. <br> $form"; } else echo $form; ?> <?php } ?> </div> <?php include('styles/bottom.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/325890-inserting-problem/ Share on other sites More sharing options...
Olumide Posted November 28 Share Posted November 28 1 hour ago, Markob1984 said: Hey Guys I'm having problems inserting customer name in to my database it isn't showing any errors on my site just the site looks all messed but like there is something wrong. there is other fields but im not using them at the moment? <?php include('styles/top.php'); ?> <div id="left"> <?php if ($user_level !=1){ } else { $sql = "SELECT SUM(status='Pending')as pending , SUM(status='Layon') as layon , SUM(status='Late') as late , SUM(status='Paid')as paid FROM orders"; $result = mysqli_query($con, $sql); $row = mysqli_fetch_assoc($result); echo " <form action='pending.php'> <input type=\"submit\" class=\"button\" value=\"Pending Payments ( {$row['pending']} )\"> </form> <form action='total-layons.php'> <input type=\"submit\" class=\"button\" value=\"Total Layons ( {$row['layon']} )\"> </form> <form action='total-late.php'> <input type=\"submit\" class=\"button\" value=\"Total Late Payments ( {$row['late']} )\"> </form> <form action='total-paid.php'> <input type=\"submit\" class=\"button\" value=\"Total Paid Payments ( {$row['paid']} )\"> </form> <form action='add-customer.php'> <input type=\"submit\" class=\"button2\" value=\"Add Customer\"> </form> "; ?> <?php } ?> </div> <div id="right"> <div id="view_box"> <ul> <li><img src="images/1.png" /></li> <!-- <li><img src="pics/2.jpg" /></li> --> <!-- <li><img src="pics/3.jpg" /></li> --> </ul> </div> <div id="button"> <ul> <!-- <li><button class="button" scroll_value="0">*</button></li> -- > <!-- <li><button class="button" scroll_value="600">*</button></li> --> <!-- <li><button class="button" scroll_value="1200">*</button></li> --> </ul> </div> <hr /> <?php if ($user_level !=1){ echo "No Access Allowed Admin Only "; } else { ?> <?php $form = " <form action='add-customer.php' method='post'> <table cellspacing='10' cellpadding='10'> <tr> <td>Customer Name</td> <td><input type='text' name='customer_name' /></td> </tr> <tr> <td></td> <td><input type='submit' name='submit' class='button' value='Add New Order To The system'/></td> </tr> </table> </form>"; if (isset($_POST['submit'])) { $name = $_POST['customer_name']; if ($name) { incude (''); $query = "INSERT INTO customers (customer_name) VALUES ('$name')"; $result = mysqli_query($con, $query); echo "<font color='Yellow'>Thank you, Information has been saved to the database <a href='index.php'>Click Here</a> To Return to Previous Page!.</font> <br>"; } else echo "Please enter customers name. <br> $form"; } else echo $form; ?> <?php } ?> </div> <?php include('styles/bottom.php'); ?> In your form handling code, you're calling `incude('');`, which is a typo. It should be `include('');` but I don't think it's meant to be there. This line is not needed, so you should just remove it. Quote Link to comment https://forums.phpfreaks.com/topic/325890-inserting-problem/#findComment-1643929 Share on other sites More sharing options...
Olumide Posted November 28 Share Posted November 28 This should work for you <?php include('styles/top.php'); ?> <div id="left"> <?php if ($user_level != 1) { // You might want to show a message if the user is not admin } else { $sql = "SELECT SUM(status='Pending') as pending , SUM(status='Layon') as layon , SUM(status='Late') as late , SUM(status='Paid') as paid FROM orders"; $result = mysqli_query($con, $sql); $row = mysqli_fetch_assoc($result); echo " <form action='pending.php'> <input type=\"submit\" class=\"button\" value=\"Pending Payments ( {$row['pending']} )\"> </form> <form action='total-layons.php'> <input type=\"submit\" class=\"button\" value=\"Total Layons ( {$row['layon']} )\"> </form> <form action='total-late.php'> <input type=\"submit\" class=\"button\" value=\"Total Late Payments ( {$row['late']} )\"> </form> <form action='total-paid.php'> <input type=\"submit\" class=\"button\" value=\"Total Paid Payments ( {$row['paid']} )\"> </form> <form action='add-customer.php'> <input type=\"submit\" class=\"button2\" value=\"Add Customer\"> </form> "; } ?> </div> <div id="right"> <div id="view_box"> <ul> <li><img src="images/1.png" /></li> </ul> </div> <hr /> <?php if ($user_level != 1) { echo "No Access Allowed Admin Only "; } else { $form = " <form action='add-customer.php' method='post'> <table cellspacing='10' cellpadding='10'> <tr> <td>Customer Name</td> <td><input type='text' name='customer_name' /></td> </tr> <tr> <td></td> <td><input type='submit' name='submit' class='button' value='Add New Order To The system'/></td> </tr> </table> </form>"; if (isset($_POST['submit'])) { $name = $_POST['customer_name']; if ($name) { // Prepared statement for better security if ($stmt = mysqli_prepare($con, "INSERT INTO customers (customer_name) VALUES (?)")) { mysqli_stmt_bind_param($stmt, "s", $name); if (mysqli_stmt_execute($stmt)) { echo "<font color='Yellow'>Thank you, Information has been saved to the database <a href='index.php'>Click Here</a> To Return to Previous Page!.</font> <br>"; } else { echo "Error inserting customer name: " . mysqli_error($con); } mysqli_stmt_close($stmt); } else { echo "Error preparing statement: " . mysqli_error($con); } } else { echo "Please enter customers name. <br> $form"; } } else { echo $form; } } ?> </div> <?php include('styles/bottom.php'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/325890-inserting-problem/#findComment-1643932 Share on other sites More sharing options...
Markob1984 Posted November 28 Author Share Posted November 28 2 hours ago, Markob1984 said: Hey Guys I'm having problems inserting customer name in to my database it isn't showing any errors on my site just the site looks all messed but like there is something wrong. there is other fields but im not using them at the moment? <?php include('styles/top.php'); ?> <div id="left"> <?php if ($user_level !=1){ } else { $sql = "SELECT SUM(status='Pending')as pending , SUM(status='Layon') as layon , SUM(status='Late') as late , SUM(status='Paid')as paid FROM orders"; $result = mysqli_query($con, $sql); $row = mysqli_fetch_assoc($result); echo " <form action='pending.php'> <input type=\"submit\" class=\"button\" value=\"Pending Payments ( {$row['pending']} )\"> </form> <form action='total-layons.php'> <input type=\"submit\" class=\"button\" value=\"Total Layons ( {$row['layon']} )\"> </form> <form action='total-late.php'> <input type=\"submit\" class=\"button\" value=\"Total Late Payments ( {$row['late']} )\"> </form> <form action='total-paid.php'> <input type=\"submit\" class=\"button\" value=\"Total Paid Payments ( {$row['paid']} )\"> </form> <form action='add-customer.php'> <input type=\"submit\" class=\"button2\" value=\"Add Customer\"> </form> "; ?> <?php } ?> </div> <div id="right"> <div id="view_box"> <ul> <li><img src="images/1.png" /></li> <!-- <li><img src="pics/2.jpg" /></li> --> <!-- <li><img src="pics/3.jpg" /></li> --> </ul> </div> <div id="button"> <ul> <!-- <li><button class="button" scroll_value="0">*</button></li> -- > <!-- <li><button class="button" scroll_value="600">*</button></li> --> <!-- <li><button class="button" scroll_value="1200">*</button></li> --> </ul> </div> <hr /> <?php if ($user_level !=1){ echo "No Access Allowed Admin Only "; } else { ?> <?php $form = " <form action='add-customer.php' method='post'> <table cellspacing='10' cellpadding='10'> <tr> <td>Customer Name</td> <td><input type='text' name='customer_name' /></td> </tr> <tr> <td></td> <td><input type='submit' name='submit' class='button' value='Add New Order To The system'/></td> </tr> </table> </form>"; if (isset($_POST['submit'])) { $name = $_POST['customer_name']; if ($name) { incude (''); $query = "INSERT INTO customers (customer_name) VALUES ('$name')"; $result = mysqli_query($con, $query); echo "<font color='Yellow'>Thank you, Information has been saved to the database <a href='index.php'>Click Here</a> To Return to Previous Page!.</font> <br>"; } else echo "Please enter customers name. <br> $form"; } else echo $form; ?> <?php } ?> </div> <?php include('styles/bottom.php'); ?> it looks like the screen below Quote Link to comment https://forums.phpfreaks.com/topic/325890-inserting-problem/#findComment-1643947 Share on other sites More sharing options...
Barand Posted November 28 Share Posted November 28 1) Do you have php's error reporting ON and error level set to E_ALL? 2) Have you turned mysql's error reporting on with mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); immediately before your call to $con = mysqli_connect( ... ) Quote Link to comment https://forums.phpfreaks.com/topic/325890-inserting-problem/#findComment-1644002 Share on other sites More sharing options...
mac_gyver Posted November 28 Share Posted November 28 I'm gusseting the posted picture is after you have submitted the form? after you set php's error_reporting to E_ALL and display_errors to ON, you should find the reason for that problem. here are a ton of coding practices that will help organize and simplify the code - the code for any page should be laid out in this general order - initialization post method form processing get method business logic - get/produce data needed to display the page html document some specific points for the posted code - use 'require' for things your code must have. include/require are not functions. the () around the path/filename don't do anything and should be left out. don't echo large amounts of static html. drop out of php mode and put the html inline. if you use php's short open-echo tag and a closing tag around a value, you can output variables in the html document using <?=$var?> don't escape double-quotes inside a php double-quoted string. simply use single-quotes inside the string. don't unnecessarily switch out of and back into php mode. just stay in php mode. don't use post method forms for navigation. use href/links. your markup is out of date. you need to validate the resulting web pages at validator.w3.org because this entire page requires the current user to be an administrator, preform the user level test once, near the top of the code and take an appropriate action if the user isn't an administrator. to get a form to submit to the same page it is on, simply leave out the entire action='...' attribute. you need to store the customer first and last names in separate columns. as a more advanced programming subject, if you have more than 2-3 form fields, you need to dynamically validate and process the form data and dynamically produce the form fields, instead of writing out code for every possible field. some points for the post method form processing code - don't attempt to detect if the submit button is set. there are cases where it won't be. instead, detect if a post method form was submitted. keep the form data as a set in a php array variable, such as $post or $data, then operate on elements in this array variable throughout the rest of the code. trim all the input data before validating it, mainly so that you can detect if all white-space characters were entered. once you do item #2 on this list, you can trim all the data using one line of code. validate all the now trimmed input data, storing user/validation errors in an array using the field name as the array index. after the end of the validation logic, if there are no errors, use the submitted form data. use a prepared query to prevent any sql special characters in value from being able to break the sql query syntax. if it seems like using the mysqli extension is overly complicated and inconsistent, it is. this would be a good time to switch to the much simpler and better designed PDO extension. if an insert/update query can produce duplicate data errors, you need to test for and handle this in the query exception handling for the query and setup a message for the user (add it to the array holding the user/validation errors) letting them know what was wrong with the data that they submitted. after using the submitted form data, if there are no errors, perform a redirect to the exact same URL of the current page to cause a get request for that page. this will prevent to browser from trying to resubmit the form data should that page get browsed back to or reloaded. to display a one-time success message, store it or a flag value in a session variable, then test for this session variable, display the message, and clear the session variable at the appropriate location in the html document. if there are errors, the code will continue on to redisplay the html document, where you will test for an display any errors, either all at once or individually adjacent to the field they correspond with, and populate the form fields with any existing data so that the user doesn't need to keep reentering values over and over. any dynamic value you output in a html context needs to have htmlentities() applied to it to help prevent cross site scripting. Quote Link to comment https://forums.phpfreaks.com/topic/325890-inserting-problem/#findComment-1644105 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.