Skip to main content
Inspiring
March 22, 2010
Answered

Insert query problem

  • March 22, 2010
  • 1 reply
  • 2892 views

I have a insert query that I have not changed, but for some reason it will not insert anything into the database.

The data gets inserted by a Ajax submit form and goes to insert.php

if(isset($_POST['message_wall'])){
    /* Connection to Database */
    include('config.php');
   
    /* Remove HTML tag to prevent query injection */
    $message = mysql_real_escape_string($_POST['message_wall']);
    $to = mysql_real_escape_string($_POST['profile_to']);
   
    $sql    =    'INSERT INTO wall (message) VALUES(
                "'.$message.'")';
                 mysql_query($sql);

I want to be able to add a user_id into the database too

The ajax code:

$(document).ready(function(){
    $("form#submit_wall").submit(function() {

    var message_wall = $('#message_wall').attr('value');

        $.ajax({
            type: "POST",
            url: "insert.php",
            data: "message_wall="+ message_wall,
            success: function(){
                $("ul#wall").prepend("<li style='display:none'>"+message_wall+"</li><br><hr>");
                $("ul#wall li:first").fadeIn();
            }
        });
    return false;
    });
});

This topic has been closed for replies.
Correct answer pziecina

hi,

while I was waiting for your message I went back to the raw code.

I changed my code and now it works but it falls again once I add the other info in hidden fields

        $.ajax({
            type: "POST",
            url: "insert.php",
            data: "message_wall="+ message_wall,
            date: "msg_date="+ msg_date,
            success: function(){
                $("ul#wall").prepend("<li style='display:none'>"+message_wall+"</li><br />"+msg_date+"<hr />");
                $("ul#wall li:first").fadeIn();

<form action="" id="submit_wall" name="submit_wall">

<textarea name="name" id="message_wall" cols="70" rows="2" onclick="make_blank();"></textarea>

<div align="left"><button type="submit">Post to wall</button></div>
<input name="profile_to" type="hidden" value="<?php echo $row_user_profile['user_id']; ?>" />
<input name="msg_date" type="hidden" value="<?php echo date("d/m/y");?>" />
</form>

how do I add more than one post option?

Is this right:

            data: "message_wall="+ message_wall,
             date: "msg_date="+ msg_date,

I tried

            data: "message_wall="+ message_wall, "msg_date="+ msg_date,

But nothing. I take it this is where the fields are sent


Hi

As this is an ajax form post then the data from the form should be inserted into your database using the insert.php script. Everything in the jQuery ajax form is passed to the processing script if you process it in the insert script it should work o/k.

You would then include a response text using a simple echo statement in your insert script, this should include anything you wish to be included on your page.

The php in your insert script would be similar to -

At top of script -

$date = $_POST["msg_date"];

At bottom of script -

if ($success) {
      echo "Inserted on $date";
    } else {
      echo "There was a problem processing your information.";   
    }

The jQuery code for doing this would be -

// Perform post-submission tasks
       function showResponse(responseText, statusText)  {
         $('.response').text(responseText);
         }

Plus -

success:   showResponse,

In your ajax form processing options

And simply include a <p class="response"> </p> where you wish this to be displayed in your html code.

PZ

www.pziecina.com

1 reply

Inspiring
March 23, 2010

I have tried the orginal code sample that works.

All I did was add a select statment to another page to list the messages.

What could it be?

pziecina
Legend
March 23, 2010

Hi

The ajax code:

This should give you an hint why the insert is not working!

O/K, the answer, you are using an ajax page, therefore any php code on the page is not parsed again once the page is rendered.

PZ

www.pziecina.com

pziecina
Legend
March 25, 2010

shall I abandon this?



Hi

This would be for you to decide, but at the minimum you should start a new thread as this one is no longer about the original title.

The problem with ajax is that in principle it is very simple, but once you start adding features to the site the code often stops working for unknown reasons. As an example of this on the ajax section of my site I had problems with the sIFR text rendering after an ajax call, (sIFR is disabled in IE, but for a different reason), which was solved by inserting a time delay before 're-calling' the sIFR rendering script. And this is similar to using client side form validation for an ajax form, you can validate the form, but it may still call the script, or even worse not call the script when valid.

As for the php form validation, unless you are confident in using php if/else statment's then validation will be difficult to implement unless someone is willing to virtually write the script for you.

There is a jQuery ajax form plug-in that includes validation, but unfortunately I have been unable to find it. All I can tell you is that it is less than 1yr ago when it first appeared, but as in a number of jQuery plug-ins it may have been removed from the repository due to incompatibility problems.

PZ