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

Inspiring
March 25, 2010

Hi

Don't worry about feeling 'stupid' as you put it, we all felt this way until we learned how to do something, and I often still do when something does not work as intended.

Unfortunately though, unless you know how to write/use php for validation, using the jQuery form plug-in with validation is at the moment not possible, and the tutorials I have looked at are not very helpful as they do assume a reasonable knowledge of both php and jQuery.

However all is not lost, as there are a number of ready made php validation routines that you may be able to adapt that are available on the web, although I do not know of one I could personally recommend, but code examples are many, it may also be worth checking out books/articles on php programming as form validation is often included.

I did glance through one by David Powers a few weeks ago, (yes, curiosity can be dangerous, but!) and I seem to remember his book contained a chapter or two on form/user input validation, see - http://foundationphp.com/.


PZ


shall I abandon this?