D
D
Dmitry Filandor2017-06-12 13:51:51
PHP
Dmitry Filandor, 2017-06-12 13:51:51

Why is PHP not writing to the database?

Hello! PHP is zero. I asked the familiar simplest example to throw in, pass the test ....
I sent the soap, I did the reading from the table, but the insertion into the table already hurt my forehead .... well, no way, tell me what's wrong:

<?php

$servername = "localhost"; 
            $username = "23423434";
            $password = "4234234234";
            $dbname = "study_db";

            // Create connection
            $conn = new mysqli($servername, $username, $password, $dbname);
            // Check connection
            if ($conn->connect_error) {
              die("Ошибка соединения с БД: " . $conn->connect_error);
              }  
              
      //данные
        /*$user_name = mysql_real_escape_string($user_name);
        $user_email = mysql_real_escape_string($user_email);
        $subject = mysql_real_escape_string($subject);
        $message = mysql_real_escape_string($message);*/

        
      
          /*$sql = "INSERT INTO Main (from_name, from_email, subject, message)
      VALUES ('$user_name', '$user_email', '$subject', '$message')";*/

      $sql = "INSERT INTO Main (from_name, from_email, subject, message)
        VALUES ('John', 'Doe', 'johnexample.com', 'sdsd')";
      
      
      if ($conn->query($sql) === TRUE) {
        echo "Новая запись создана успешно";
      } else {
        echo "Error: " . $sql . "<br>" . $conn->error;

      }

      $conn->close();
      
      
?>

I just inserted the lines for the test...
UP
took this code into a separate php file.... , it works there, but if you insert it into my php file in which mail is sent, and to a different place - joxi.ru/52aQR7cGEZYn20 and inside the sending procedure itself after successful sending and the next block - joxi.ru/MAjMp4IvjZBW2e it
will not insert data , and I call it from the send email page:
//Ajax post data to server
            $.post('test.php', post_data, function(response){  
        if(response.type == 'error'){ //load json data from server and output message     
          output = '<div class="error">Ошибка</div>';
        }else{
            output = '<div class="success">Успешно</div>';
          //reset values in all input fields
          $("#contact_form  input[required=true], #contact_form textarea[required=true]").val(''); 
          $("#contact_form #contact_body").slideUp(); //hide form after success
        }
        $("#contact_form #contact_results").hide().html(output).slideDown();
            }, 'json');

it works, though crocozables are written to the database instead of Cyrillic, somewhere you probably need to specify the encoding
UP
and figured out the encoding, after connecting to the database:
mysql_query("SET NAMES 'utf8';");
  mysql_query("SET CHARACTER SET 'utf8';");
  mysql_query("SET SESSION collation_connection = 'utf8_general_ci';");

but the joke was that it was just that php mayadmin showed me crookedly .... and in the browser everything is beautiful

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Boris Korobkov, 2017-06-12
@BorisKorobkov

you need variables from whom, message, subject, insert into the plate ...

If you cannot take variables from one line (where mail is sent) and insert them into the next line (where insertion into the database) - then you need to start learning PHP in general and variables in particular. php.net/manual/ru/langref.php
Especially since:
pass exam...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question