Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

mysql

The Situation::

I am using PHP to connect to a MySQL database and insert some demographic type form generated variables and some scoring results generated calculated variables.
The demographic "form" generated variables are being properly inserted * ... but the scoring results generated calculated variables are not*. However in the email that automatically gets sent to the user upon the form's submission, those scoring results generated calculated variables are sent and are correctly calculated, plus in the echoed/printed splash screen, both types of variables are visible.
The form by the way is a job satisfaction survey with 15 questions each question with 7 options (radio button) and each option is worth a different number of points.
I have worked on this for 5 days, read all threads related to this topic on this web site, tried many different configurations but with no success and cannot find a way to insert those last 7 variables values (skill_variety, task_identity, task_significant, autonomy, feed_back, total, MPS) into the MYSQL database.

I am using hidden input to insert the scoring results calculated variables, but have not succeeded

<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 needed I will be glad to add it. Just let me know.

I know you folks are helpful and also many times frustrated by questions like this but I hope you can offer a professional reach out and some suggestions. I already have thin hair and hope not to pull the rest of it out before its scheduled time.

My Need

Some direction /suggestion/solution for getting those recalcitrant variables (skill_variety, task_identity, task_significant, autonomy, feed_back, total, MPS) inserted into the targeted MYSQL table.

With thanks in advance for any direction and hope I have been clear and complete in this post.

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

Here is the database set up

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 are the INSERT & CONNECT PHP CODES

<?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 is 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', '[email protected]','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 is 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] => [email protected]  
    [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  
)
like image 248
Chef Mars Avatar asked Oct 31 '22 18:10

Chef Mars


1 Answers

I looked into your Code and you might just try to insert things which are not there.

First of all, this is what is sent from your user 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 the Debug-Console in Crome

enter image description here

or when you print $_POST like you did. Jason K commented the missing $_POST[Motivating_Potential_Score]. And there is no task_significant, autonomy, feed_back and total sent back. Thatwhy your $_POST-Array is not set for these keys.

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

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

You try to relay these two variables back through your html-code. Why? Try

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

to store your values within your server while your visitor is answering your Form. When he has successfully submitted your code get this value back from your serverside stored session like this:

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 seems like you forgot to process your question-XX-answers with some function. Whatever this function is, it should probably fill up your values for your database. I mean those you (whyever) try 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']

like image 123
Nibbels Avatar answered Nov 02 '22 09:11

Nibbels