0

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?

3
  • I think it's time for you to get glasses. It should be $stmt = $dbh->prepare('INSERT INTO Streams (theStream, theLabel, theLocation, theStatus) VALUES (:theStream, :theLabel, :theLocation, :theStatus)');. What you forgot is the command prepare. Commented Aug 16, 2015 at 13:23
  • By the way. Im not using PDO to connect to database. Im using MySQLi Procedural as described here w3schools.com/php/php_mysql_insert.asp Commented Aug 16, 2015 at 13:28
  • Then you're doing it wrong in your script. You're using MySQLi OOP, not procedural. Also use the php.net manual, not w3schools.com. Commented Aug 16, 2015 at 13:29

2 Answers 2

1

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)');
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks. That seems to tackle the error above but new one pops up. <b>Fatal error</b>: Call to undefined function validate() in <b>add_stream.php</b> on line <b>19</b><br />
It seem that your validate function is not defined in your add_stream.php file, be sure to add/include it on your file ;), don't forget to accept and upvote my answer bro' :)
Thanks for your answer. I gave you an upvote for leading me to the right solution.
1

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>

3 Comments

I get expecting ')' error. the syntax highlighting seems not correct. $_POST is black on last 3 instances but red on the first one. Is this correct? tried to correct it by adding some quotes but i got the same error
everything seems to work now on the code level but my data is not being inserted into the database.
Thanks. Did not worked for me specifically but I got useful hints from your code that led me to find this api.jquery.com/jQuery.ajax then i dropped the array data and use the same as per the example in that url. Working now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.