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 24, 2010

Hi, still the same

    $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);

This does not allow me to insert into database. It accepts the value and when I take a look in phpMyAdmin it still shows only the 5 records I have from before.


Hi Again

Just a thought but are any of the database fields required items?

The reason I ask is that you are using ajax and will not see any error messages generated on the insert script. Try removing the ajax section for now and testing the script as a 'standard' standalone page.

PZ

www.pziecina.com