-1

my query don't save data to the database.This is my code it save all the values corect in the query but did not save in database. same problem i faced in every web page.

    <?php
    require_once('config.php');
    if(isset($_POST['submit'])){

    $contactName    = $_POST['Contact_Name'];
    $companyName  = $_POST['Company_Name'];
    $phone              = $_POST['phone'];
    $cell                   = $_POST['cell'];
    $fax                    = $_POST['fax'];
    $website            = $_POST['website'];
    $email              = $_POST['email'];
    $emailForInvoice= $_POST['email-for-invoice'];
    $address            = $_POST['address'];
    $city               = $_POST['city'];
    $state              = $_POST['state'];
    $zipCode            = $_POST['zip-code'];
    $country      = $_POST['myCountry'];

    $sql = "INSERT INTO user_info (name, companyName, phone, cell, fax, website, email, invoiceMail, address, city, state, zipCode, country)
    VALUES ('$contactName', '$companyName', '$phone', '$cell', '$fax', '$website', '$email', '$emailForInvoice', '$address', $city, '$state','$zipCode', '$country');";
    echo "$sql";
    $db->query($sql);
    $result = mysqli_query($db,$sql);
    if($result){
    echo "Sucessfully Insert";
    }else {
    echo "Error in Query";
    }``
    }
    ?>
Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Have you tried to check your echo query into the PHPMyAdmin SQL prompt? so you got the error exactly where you going wrong. – Sachin Oct 08 '19 at 07:07
  • Check out the content of $db and $sql and extend your question including those so we can also check it out – Matt Oct 08 '19 at 07:11
  • I'd suggest attempting to run your query from the result of `echo "$sql"` straight into your database to see if it's an error with your query. I'd also suggest sanitising the values you're inserting into your database with `mysqli_real_escape_String` as you're prone to a SQL Injection with your current code. – Paradigm Oct 08 '19 at 07:37
  • Possible duplicate of [How can I prevent SQL injection in PHP?](https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) – Dharman Oct 08 '19 at 09:04
  • @Dharman please stop flagging posts as duplicates of that one, when that is not the actual question that needs solving. All your flags are unnecessarily filling up the close votes queue. Instead, just provide a link to that answer so that OP can research it in their own time. – Nick Oct 13 '19 at 03:03

1 Answers1

0

You have set all the fields into database for this table as either varchar or any type that store the string. If so you have to make one change into you query.

 $sql = "INSERT INTO user_info (name, companyName, phone, cell, fax, website, email, invoiceMail, address, city, state, zipCode, country)
    VALUES ('$contactName', '$companyName', '$phone', '$cell', '$fax', '$website', '$email', '$emailForInvoice', '$address', '$city', '$state','$zipCode', '$country');";

I've quoted city.

You don't required this

$db->query($sql);

Your $db should be the connection variable something like

$db = mysqli_connect("localhost","my_user","my_password","my_db");

Check your config.php file if it should have above line.

Rest seems ok. Just need to change in query, remove $db->query($db) line and double check your $db should be as I mentioned above.

Following steps should work for you.

Vantiya
  • 612
  • 1
  • 4
  • 11