Jump to content

Recommended Posts

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'); ?>

 

Link to comment
https://forums.phpfreaks.com/topic/325890-inserting-problem/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/325890-inserting-problem/#findComment-1643929
Share on other sites

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'); ?>

 

Link to comment
https://forums.phpfreaks.com/topic/325890-inserting-problem/#findComment-1643932
Share on other sites

image.png.0ebd0265f5570efa1986a16d1cd7c9a7.png

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

Link to comment
https://forums.phpfreaks.com/topic/325890-inserting-problem/#findComment-1643947
Share on other sites

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

 

Link to comment
https://forums.phpfreaks.com/topic/325890-inserting-problem/#findComment-1644002
Share on other sites

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 -

  1. initialization
  2. post method form processing
  3. get method business logic - get/produce data needed to display the page
  4. html document

some specific points for the posted code -

  1. use 'require' for things your code must have.
  2. include/require are not functions. the () around the path/filename don't do anything and should be left out.
  3. don't echo large amounts of static html. drop out of php mode and put the html inline.
  4. 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?>
  5. don't escape double-quotes inside a php double-quoted string. simply use single-quotes inside the string.
  6. don't unnecessarily switch out of and back into php mode. just stay in php mode.
  7. don't use post method forms for navigation. use href/links.
  8. your markup is out of date. you need to validate the resulting web pages at validator.w3.org
  9. 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.
  10. to get a form to submit to the same page it is on, simply leave out the entire action='...' attribute.
  11. you need to store the customer first and last names in separate columns.
  12. 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 -

  1. 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.
  2. 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.
  3. 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.
  4. validate all the now trimmed input data, storing user/validation errors in an array using the field name as the array index.
  5. after the end of the validation logic, if there are no errors, use the submitted form data.
  6. 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.
  7. 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.
  8. 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.
  9. 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.
  10. 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.
  11. any dynamic value you output in a html context needs to have htmlentities() applied to it to help prevent cross site scripting.
Link to comment
https://forums.phpfreaks.com/topic/325890-inserting-problem/#findComment-1644105
Share on other sites

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.