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.
php: 5.7
mysql: 5.7.28
-
Answer # 1
Related articles
- [php] i want to save the information from the form in the database using bindparam
- php - i want to call my name using my membership information, but
- i want to update the db using variables in pdo in php, but it doesn't work
- edit user information of each logged-in user without using db in php
- exif information of images taken with iphone camera cannot be obtained correctly from php
- php - i want to save a file on another server using the move_uploaded_file function
- how to send an email using yahoo mail in php
- php - i want to retrieve database information, but i can't
- i want to solve the reason why the entered information is not displayed in the email sent from the inquiry form created with php
- php - how do i update the database values when i press the button?
- php - about "update" when adding products in the shop to the cart while creating an ec site
- php - payment using square i want to register a memo or product name in payment
- ruby - i want to update user information
- sql - data update using update and join
- php - about passing value to form using tag
- php - about the version when developing using the framework
- php - wordpress: tab switching using custom field items
- php - [laravel] i want to display the information of the related table in my page
- about encryption at the time of output in php using form input tyle = "password"
- mysql - i want to update using multiple tables with update
- php : How to write the query builder correctly
- php - i want to insert data from multiple tbls into one tbl
- php : Add post + refresh page without reloading
- php : Automatic messages on the site
- php : LARAVEL database query
- How to get column names in php from sql query
- php : Update SQL tables from CSV file
- javascript : (AJAX + PHP) Why is sending a POST request not processed by clicking on the Delete button?
- php : Redirecting from the page if the user data in the database does not match
- php : SQLSTATE [42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax;
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.
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.
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.
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)