Jump to content

Problem in insert into MySql table


maideen
Go to solution Solved by maideen,

Recommended Posts

Hi

 

I am new in php. I could not insert the data into mysql table. and there is no error prompt  Below is my code. Pls help me.

<?php
include_once '../inc/header.php';
if (isset($_POST['submit']) && $_POST['submit']  != "" )
    {
        $name = $_POST["name"];
        $address = $_POST["address"];
        $tel = $_POST["tel"];
        $fax = $_POST["fax"];
        $email = $_POST["email"];
        $website = $_POST["website"];
        $type = $_POST["type"];    
        try 
            { 
                $sql="INSERT INTO bk_customer (id,name,address,tel,fax,email,type,website)
                      VALUES ('$name','$address','$tel','$fax','$email','$website','$type')";
                $result = mysqli_query($con,$sql) ; 
                exit();
            } 
        catch (Exception $ex) 
            {
               echo $e->getMessage() . "\n";
               file_put_contents('PDOErrors.txt', $e->getMessage(), FILE_APPEND);
               exit();            
            }
     }
mysqli_close($con);
?>
Link to comment
Share on other sites

How do you know you don't have an error?  Do you have php errors enabled?  You aren't checking that the result of your query is valid either.  You exit without showing any of your results, not even an acknowledgement that it MAY have worked.

 

With that said - your query is bad.

Link to comment
Share on other sites

Happy that you have solved this problem.  Something you should consider in future efforts.  The following line:

asdf

 

$result = mysqli_query($con,$sql);

 

holds the results of your query.  IF it ran successfully.  When it doesn't it contains 'false' - which you should always test for in order to be sure that you actually have results to play with.  Simply add:

$result = mysqli_query($con,$sql);
if (!$result)
{
     echo "Query did not run - error is ---";
    ( here you should show an error message from mysqli and decide what you want to do from this point on.)
}
Link to comment
Share on other sites

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.