Only part of the PHP form variables are inserted into the MySQL table

Situation::

I use PHP to connect to the MySQL database and insert some kind of demographic form generated by the variables, and some scoring results generate the calculated variables.
The generated variables of the demographic "form" are located correctly * ... but the results of the calculation of the results generated by the calculated variables are not * . However, in an email that is automatically sent to the user when the form is submitted, these scoring results generated by the calculated variables are sent and calculated correctly, and both types of variables are displayed in an echo or printed splash screen.
The form, by the way, is a survey of satisfaction with 15 questions on each question with 7 options (switch), and each option costs a different number of points.
I worked on this for 5 days, read all the topics related to this topic on this website, tried many different configurations, but without success and could not find a way to insert these last 7 variable values ​​(skill_variety, task_identity, task_significant, autonomy , feed_back, total, MPS) to the MYSQL database.

I use hidden input to insert the results of counting the results calculated but failed

<FORM METHOD=POST action="<?php echo $_SERVER['PHP_SELF']; ?>" NAME="action" id="action" onSubmit="return validate(this);"> <input type="hidden" name="action" value="true"> <input type="hidden" name="skill_variety" id="skill_variety" value="<?php echo $_POST['skill_variety'] ?>" /> <input type="hidden" name="task_variety" id="task_variety" value="<?php echo $_POST['task_variety'] ?>" /> 

If more code is required, I will be happy to add it. Just let me know.

I know that you help, and are also upset by such questions many times, but I hope you can offer professional advertising and some suggestions. I already have thin hair and hope not to pull out the rest before the scheduled time.

My need

Some direction / suggestion / solution for obtaining these disobedient variables (skill_variety, task_identity, task_significant, autonomy, feed_back, total, MPS) is inserted into the MYSQL target table.

Thanks in advance for any direction and I hope that I was clear and ready in this post.

BTW is testing the URL here if it helps to see the situation "en vivo" http://www.marscafe.com/php/hr2/master_9.php

Database configured here

 CREATE TABLE IF NOT EXISTS mydb3 ( id int(11) NOT NULL AUTO_INCREMENT, s varchar(60), name varchar(50), email varchar(50), ordered varchar(4), optype varchar(50), rate varchar(50), time timestamp not null default now(), skill_variety varchar(8), task_identity varchar(8), task_significant varchar(8), autonomy varchar(8), feed_back varchar(8), total varchar(8), MPS varchar(12), PRIMARY KEY (id) ) ENGINE=MyISAM 

Here is the INSERT and CONNECT PHP CODES code

 <?php //MySQL Database Connect include '../../../../phpinc.php'; // Only process the form if $_POST isn't empty if ( ! empty( $_POST ) ) { // Connect to MySQL variables are defined in the include file $mysqli = new mysqli($hostname, $username, $password, $dbName ); // Check the connection if ( $mysqli->connect_error ) { die( 'Connect Error: ' . $mysqli->connect_errno . ': ' . $mysqli->connect_error ); } // Insert the data $sql = "INSERT INTO `mydb3` ( name, email, ordered, optype, rate, skill_variety, task_identity, task_significant, autonomy, feed_back, total, MPS ) VALUES ( '{$mysqli->real_escape_string($_POST['name'])}', '{$mysqli->real_escape_string($_POST['email'])}', '{$mysqli->real_escape_string($_POST['ordered'])}', '{$mysqli->real_escape_string($_POST['optype'])}', '{$mysqli->real_escape_string($_POST['rate'])}', '{$mysqli->real_escape_string($_POST['skill_variety'])}', '{$mysqli->real_escape_string($_POST['task_identity'])}', '{$mysqli->real_escape_string($_POST['task_significant'])}', '{$mysqli->real_escape_string($_POST['autonomy'])}', '{$mysqli->real_escape_string($_POST['feed_back'])}', '{$mysqli->real_escape_string($_POST['total'])}', '{$mysqli->real_escape_string($_POST['Motivating_Potential_Score'])}' )"; $insert = $mysqli->query($sql); echo $sql; // Print response from MySQL if ( $insert ) { print("<br>"); echo "Success! Row ID: {$mysqli->insert_id}"; } else { die("Error: {$mysqli->errno} : {$mysqli->error}"); } ## #$mysqli->close(); has been moved from here to after the automated email code ?> 

Here's what happens as printed on the splash screen using echo $ sql;

 INSERT INTO mydb3 (name, email, ordered, optype, rate, skill_variety, task_identity, task_significant, autonomy, feed_back, total, MPS) VALUES ( 'Jack White', ' mars@marscafe.com ','NO', 'Other','Other', '','', '','', '','','' ) Success! Row ID: 46 skill_variety -- 15 task_identity -- 18 task_significant -- 6 autonomy -- 9 feed_back -- 4 total -- 52 MPS -- 468 

Here's what happens as printed on the splash screen using

 echo "<pre>"; print_r($_POST); echo "</pre>"; Array ( [action] => true [skill_variety] => [task_variety] => [name] => Jack White [email] => mars@marscafe.com [rate] => Other [ordered] => NO [optype] => Other [question-1-answers] => E [question-2-answers] => F [question-3-answers] => B [question-4-answers] => C [question-5-answers] => A [question-6-answers] => G [question-7-answers] => G [question-8-answers] => G [question-9-answers] => G [question-10-answers] => D [question-11-answers] => D [question-12-answers] => D [question-13-answers] => D [question-14-answers] => D [question-15-answers] => D ) 
+5
source share
1 answer

I looked at your code, and you can just insert things that are not there.

First of all, this is what your user sent to the server:

 action:true skill_variety: task_variety: name:TestNibbels email: TestNibbels@dow *****ht.de rate:Chef ordered:YES optype:Corporation question-1-answers:C question-2-answers:C question-3-answers:D question-4-answers:E question-5-answers:C question-6-answers:C question-7-answers:C question-8-answers:C question-9-answers:C question-10-answers:C question-12-answers:C question-13-answers:C question-14-answers:C question-15-answers:C 

You see this list when you press F12 for Debug-Console in Crome

enter image description here

or when you print $ _POST, just like you. Jason K commented on the missing $ _POST [ Motivating_Potential_Score ]. And no task_significant , autonomy , feed_back and total sent back. That your $ _POST array is not set for these keys.

The following two lines are the reason that $ _POST [ skill_variety ] and $ _POST [ task_variety ] are empty but set. You see this in lines 8 and 9 of your last code.

 <input type="hidden" name="skill_variety" id="skill_variety" value="" /> <input type="hidden" name="task_variety" id="task_variety" value="" /> 

You are trying to pass these two variables back through your html code. What for? Try

 session_start(); $_SESSION['skill_variety'] = 'something'; 

to store your values ​​on your server while your visitor responds to your form. When it successfully sent your code, return this value from a session stored on the server as follows:

 session_start(); $thisissomething = $_SESSION['skill_variety']; 

Then you can omit these:

 <input type="hidden" name="skill_variety" id="skill_variety" value="<?php echo $_POST['skill_variety'] ?>" /> <input type="hidden" name="task_variety" id="task_variety" value="<?php echo $_POST['task_variety'] ?>" /> 

Secondly: It looks like you forgot to process your question-XX-answers with some function. Regardless of this function, it should probably populate your values ​​for your database. I mean those whom you (why not trying) to find in your POST request: $_POST['skill_variety'] $_POST['task_identity'] $_POST['task_significant'] $_POST['autonomy'] $_POST['feed_back'] $_POST['total'] $_POST['Motivating_Potential_Score']

+2
source

Source: https://habr.com/ru/post/1232735/


All Articles