Home>

I am a php beginner.
In php, I have a program to post/search/create/edit articles.

[Process part I want to ask]
It is an event on the edit screen to receive the id from the display screen of the article with a specific id and edit the article.
The user is asked to enter new information and finally press the update button to complete the DB update and it is already a specification.

[Image flow]
① Get new value entered by user
Each value of input and textarea contains the value obtained from DB as the initial value, so in the change event,
radio is a click event, and the value that the user has edited and entered on the browser is checked, and the value is stored by assigning it to a variable with var

$(function () {
    // Substitute the value of input and textarea updated in the browser to the variable
  $('# id_url'). change (function () {
    var $inputVal_url = $(this) .val ();
  });
  $('# id_wire_url'). change (function () {
    var $inputVal_wire_url = $(this) .val ();
  });
  $('# id_project'). change (function () {
    var $nputVal_project = $(this) .val ();
  });
  $('# id_coment'). change (function () {
    var $textareaVal_coment = $(this) .val ();
  });

    // assign the value of the radio button selected on the browser to the variable
$(". page_name_btn"). click (function () {
    var $radioVal_page_name = $("input [name = 'style']: checked"). val ();
});
$(". job_btn"). click (function () {
    var $radioVal_job = $("input [name = 'style']: checked"). val ();
});
$(". style_btn"). click (function () {
    var $radioVal_style = $("input [name = 'style']: checked"). val ();
});

② Pass variables to php
Pass a variable to update.php when clicking #ajax with ajax

$('# ajax'). on ('click', function () {
          // assign id to variable
          var $id_id = $('# id_id'). val ();$.ajax ({
          url: 'update.php', // destination
          type: 'POST', // Sending method
          datatype: 'json', // Received data type
          data: {
          id: $id_id,
          url: $inputVal_url,
          wire_url: $inputVal_wire_url,
          project: $inputVal_project,
          coment: $textareaVal_coment,
           page_name: $radioVal_page_name,
           job: $radioVal_job,
           style: $radioVal_style
          },


          // When Ajax communication is successful
          success: function () {
            alert ('Updated. \ nThank you.');
          console.log ('communication successful');
          },


          // When Ajax communication fails
          error: function () {
            alert ('Communication failed. \ nPlease check the communication environment and press the update button again.');
          console.log ('communication failure');
        }
         });
         // Prevent screen reload by submit.
         return false;
    });// # ajax click end

③ Update DB
In update.php, run sql to update the value

ini_set ("display_errors", 1);
error_reporting (E_ALL);
// Substitute the received value
$id = $_REQUEST ['id'];
$project = $_REQUEST ['project'];
$url = $_REQUEST ['url'];$wire_url = $_REQUEST ['wire_url'];
$page_name = $_REQUEST ['page_name'];
$job = $_REQUEST ['job'];
$style = $_REQUEST ['style'];
$coment = $_REQUEST ['coment'];

$pdo = new PDO (// connection information);
$sql = 'UPDATE articles SET project =: project and url =: url and wire_url =: wire_url and page_name =: page_name and job =: job and style =: style and coment =: coment
        WHERE id =: id
        ';
        $statement = $pdo->prepare ($sql);
        $statement->bindValue (': id', $id, PDO :: PARAM_INT);
        $statement->bindValue (': project', $project, PDO :: PARAM_STR);
        $statement->bindValue (': url', $url, PDO :: PARAM_STR);
        $statement->bindValue (': wire_url', $wire_url, PDO :: PARAM_STR);
        $statement->bindValue (': page_name', $page_name, PDO :: PARAM_STR);
        $statement->bindValue (': job', $job, PDO :: PARAM_STR);
        $statement->bindValue (': style', $style, PDO :: PARAM_STR);
        $statement->bindValue (': coment', $coment, PDO :: PARAM_STR);
        $statement->execute ();
    $statement = null;
    $pdo = null;

(Supplement)

<form>
html description of click event part of ajax
<!-description of input code of input, textarea, radio->
<input type = "submit" value = "update">
</form>
Error message

It is unknown whether the ajax communication has been successful without updating the DB. . .

I'm sorry. There is no particular change just by updating the browser.

I'm gugging.
I passed a jquery variable to php, examined ajax, and copied and customized the above source code.

Supplemental information (FW/tool version etc.)

php: 5.7
mysql: 5.7.28

Please provide more information here.
  • Answer # 1

      

    DB is not updated,

    There is a mistake in SQL first. As pointed out already.

    However, SQL is an external mechanism from PHP.
    It can not be executed only from PHP.
    It can be executed from other languages, or it can be executed directly by logging in from a terminal or tool.

    Of course, if you execute SQL directly and do not work as expected, it will not work even if you execute it from outside such as PHP.
    Before using variable information that accepts input such as variables, set it to a fixed value and execute it directly to make sure that you get the expected results.
    By doing so, even if something goes wrong, one possibility can be crushed: "The SQL syntax itself is fine".

    If you suddenly execute a variable (or prepared statement) from PHP,
    You have to face various possibilities, whether PHP code is wrong, SQL is bad, value is not passed from PHP properly, communication status is bad.
    If you eliminate even one possibility, it will be easier to check because there are fewer parts to consider.

      

    It is unknown whether ajax communication is successful. . .

    The success or failure of the communication itself and the output on the PHP side can be acquired on the JavaScript side.

    As you can see from the jQuery reference, you can see that various arguments are returned.

      

    success
      Type: Function (Anything data, String textStatus, jqXHR jqXHR)
      error
      Type: Function (jqXHR jqXHR, String textStatus, String errorThrown)

    Even if you come to success, you should always check textStatus.
    By the way, it is not required because it is an argument on the receiving side.
    However, it's better to keep a look at everything as much as possible.
    There is no need to use alert () directly on the screen, just console.log ().
    * If you are going to release it to users,

    In Ajax, there is a mechanism that picks up the output on the PHP side with the "data" of the success, so it is better to leave something out.
    Separately, 1 or 0 may be used. By not returning anything, the situation on the PHP side is unclear.

    Furthermore, if you want to process the DB on the PHP side, you should introduce a mechanism to catch errors.

    try {
    // From PDO connection to transaction, DB processing, commit/rollback
    } catch (PDOException $e) {
      echo $e->getMessage ();
      // prefer all $e log output
    }


    PHP Manual: Errors and Error Handling

    As I wrote last, let's introduce a mechanism that can debug Ajax.
    You can also check the communication status with the browser developer tool.
    If you want to see how far the PHP code has passed, you can write the situation to a text file, and a debugger can be useful.
    Even though I execute Ajax, I only send a request with POST/GET after all, so I feel that it is quite quick to send a POST/GET request with normal form transmission instead of JavaScript. You can see it in your browser.

    Snacks:
    success/error is recognized as a rather old way of writing.
    Once you have settled down, try a new way of writing.
    When my uncle was young, I specified success or error in the $.ajax () option (remember)