Jump to content

Problem in insert into MySql table


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
https://forums.phpfreaks.com/topic/287577-problem-in-insert-into-mysql-table/
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.

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.