简体   繁体   中英

How to properly Insert javascript variable to mysql database using ajax

Im trying to implement the solution posted in this question Best approach to add records into DB using php/ajax/mysql?

My code so far is like this

JS

function FromFlash(m1, m2, m3, m4){ 
    var postData = {
        theStream: m1,
        theLabel: m2,
        theLocation: m4,
        theStatus: m3
    };

    $.post('add_stream.php', postData)
    .done(function(response) {
        alert("Data Loaded: " + response);
    });
}

PHP

//Connect to DB
...

// INSERT DATA

$data = validate($_POST);

$stmt = $dbh->('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus) 
    VALUES (:theStream, :theLabel, :theLocation, :theStatus)');

$stmt->execute($data); 


if ($conn->query($sql) === TRUE) {
    echo "New stream added successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();

I am getting this error. (line 21 refers to $stmt = $dbh-> )

Data Loaded: <br />
<b>Parse error</b>:  syntax error, unexpected '(', expecting T_STRING or T_VARIABLE or '{' or '$' in <b>add_stream.php</b> on line <b>21</b><br />

I can't figure out what is wrong with my code. I checked pairing of open/close parenthesis and it is properly paired

What am I missing?

Your forgot to prepare your query :)

replace :

$stmt = $dbh->('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus) 
VALUES (:theStream, :theLabel, :theLocation, :theStatus)');

With this :

$stmt = $dbh->prepare('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus) 
VALUES (:theStream, :theLabel, :theLocation, :theStatus)');

You missed prepare() and $data should be an array of place holders.

$data = array(
':theStream'=>$_POST['theStream'],
':theLabel'=>$_POST['theLabel'],
':theLocation'=>$_POST['theLocation'],
':theStatus'=>$_POST['theStatus']
);

$stmt = $dbh->prepare('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus) 
VALUES (:theStream, :theLabel, :theLocation, :theStatus)');

$stmt->execute($data); 

For ajax:

var postData = {
theStream: m1,
theLabel: m2,
theLocation: m4,
theStatus: m3
};

$(".form").submit(function(){

$.ajax({
type:'post',
url:'target.php',
data: postData,
success:function(data){
//code to run after success
}

})

})

Total code:

    <?php

    include 'PDODB.php';    

    if(isset($_POST['submit'])){

    $data = array(
    ':theStream'=>$_POST['theStream'],
    ':theLabel'=>$_POST['theLabel'],
    ':theLocation'=>$_POST['theLocation'],
    ':theStatus'=>$_POST['theStatus']
    );

    $stmt = $dbh->prepare('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus) 
    VALUES (:theStream, :theLabel, :theLocation, :theStatus)');



    $stmt->execute($data);

    }


?>

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<button class="form">Form</button>

<script>

    var postData = {
    theStream: 'qq',
    theLabel: 'ww',
    theLocation: 'ee',
    theStatus: 'rr',
    submit: 'submit'
    };

    $(".form").click(function(){

    $.ajax({
    type:'post',
    url:this.url,
    data: postData,
    success:function(data){
    //code to run after success
    }

    })

    })

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM