如何获取代码以写入数据库

时间:2019-05-10 14:46:33

标签: php sql-server

我要做的就是写到数据库,我相信没有建立连接。但是,我之前已经向数据库编写了代码,这就是我过去与数据库的连接方式。现在我不确定在我的Java脚本代码中是否无法正确获取值。
自从处理INTS以来,我决定执行一个存储过程,并在其中设置值。 我仍然是处理JSON的初学者,我认为这可能也是问题的一部分。 有任何想法吗?

var ProcessManager;
var Validator;

function submit(){
    var formHash = new Hash();
    formHash.collectDate = $('itemDate').value;
    formHash.txtMain = $('txtMain').value;
//radiobuttons
$$('input[name=iMenuChoiceID').each(function(el){
        if(el.getProperty('checked'))
            formHash.menuChoiceID = el.value;
    });
    //txtboxes
    formHash.txtMain = $('txtMain').value;
    formHash.txtSide = $('txtSide').value;
    formHas.txtAlternative = $('txtAlternative').value;

    menuProcessManager.setOptions({
        data: {
            'encFormData' : JSON.encode(formhash)
        }
    }).send();
}

function clearMenu(){

    alert("The form has been resetted.");
    $('menuMsg').set('html', '');
    $('txtMain').value = '';
    $('txtSide').value= '';
    $('txtAlternative').value = '';
    $('input[name=iMenuChoiceID').each(function(el){
        if(el.value ==1){
            el.setProperty('checked', true);
        });
    }
    menuValidator.reset();
});

/**Add window.addevent ***/

window.addEvent('domready', function(){

    /*site validators*/
    Validator = new Form.Validator.Inline('Form', {
        onFormValidate : function(passed, form, event) {
            if (passed) {
                event.preventDefault();
                submit();
            }
        }
    });

    /*JSON Request Managers*/
    ProcessManager = new Request.JSON({
        method:'post',
        async: false,
        url: 'includes/file/Process.php',
        data: {
            'encFormData' : ''
        },
        onRequest : function(){
            $('Submit').disabled = true;
            $('Submit').set('value', 'Processing...');
            $('successMsg').set('html', '');
            $('successMsg').setStyle("display", "none");
        },
        onSuccess : function(responseText){
            var returnVals = new Hash(response);
            $('Submit').set('value', 'Submit');
            $('Submit').disabled = false;
            var results = responseText.split("/");
            if (results[0] == 'Success') {
                $('successMsg').set('html', 'Success! );
                menuClear();
            }else {
                alert(responseText);
            }
            return true;
}

    });
});

Process.php

<?php
session_start ();


// decodedFormData
$decFD = json_decode ( $_POST ['encFormData'] );

try{
    $dbh = new PDO ( 'db', '', '' );

    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $dbh->beginTransaction ();
//stored procedure just declares the columns with their paramters since I have a couple of INTs
    $stmt = $dbh( "exec uspWebAdder ?, ?, ?, ?, ? ");
    $stmt->bindParam ( 1, $decFD->ChoiceID, PDO::PARAM_INT );
    $stmt->bindParam ( 2, $_SESSION ['userID'], PDO::PARAM_INT );
    $stmt->bindParam ( 3, $decFD->txtMain, PDO::PARAM_STR );
    $stmt->bindParam ( 4, $decFD->txtSide, PDO::PARAM_STR );
    $stmt->bindParam ( 5, $decFD->txtAlternative, PDO::PARAM_STR );

    $dbh = null;

} catch ( PDOException $e ) {
    $errMsg = "There was a database error:<br>" . $e->getFile () . ' (' . $e->getLine () . ')<br>' . $e->getMessage ();
    $errMsg .= "<br><br>SQL : " . $sql;
    echo $errMsg;
    die ();
}
echo "Success"
?>

0 个答案:

没有答案