• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Insert query problem

Participant ,
Mar 22, 2010 Mar 22, 2010

Copy link to clipboard

Copied

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

TOPICS
Server side applications

Views

1.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 24, 2010 Mar 24, 2010

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

...

Votes

Translate

Translate
Participant ,
Mar 23, 2010 Mar 23, 2010

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 23, 2010 Mar 23, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 23, 2010 Mar 23, 2010

Copy link to clipboard

Copied

no, sorry I did not make myself clear.

The Ajax code is in home.php

And the insert code is in insert.php

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 23, 2010 Mar 23, 2010

Copy link to clipboard

Copied

Hi

In that case check the code in red as you have a . before and after the variable, which should be removed, and single quotes inserted between the double quotes.

$sql    =    'INSERT INTO wall (message) VALUES(
                 "'.$message.'")';
                 mysql_query($sql);

PZ

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 24, 2010 Mar 24, 2010

Copy link to clipboard

Copied

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 24, 2010 Mar 24, 2010

Copy link to clipboard

Copied

Hi

Maybe it's my eye sight, but I am still seeing the extra quote(s) around the variable -

"'$message''");

PZ

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 24, 2010 Mar 24, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 24, 2010 Mar 24, 2010

Copy link to clipboard

Copied

ok,

If i do that, once I submit the page can I refresh it? or load it so that it appears to have not reloaded the whole page?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 24, 2010 Mar 24, 2010

Copy link to clipboard

Copied

Hi

The procedure when developing with ajax is to first ensure that everything works as a 'standard' dynamic site, which is what I am suggesting. This way you can ensure everything is working correctly and see any errors generated with scripts such as the insert script you are using.

Only when this is completed should you 'convert' your site to use ajax, this is also why many developers have a 'standard' set-up for testing and an ajax enabled section. I f you look on many ajax web sites you will see a link to a none ajax version, this is often done as an 'option' for those who cannot use ajax, (screen readers, etc) but also a method of testing features work correctly. This is what I am suggesting you do at the moment, as it only involves saving your current ajax page under a different name and removing the ajax section from the code, this way you will see any errors generated.

PZ

www.pziecina.com

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 24, 2010 Mar 24, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 24, 2010 Mar 24, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 24, 2010 Mar 24, 2010

Copy link to clipboard

Copied

Hi,

You know your ajax!

Its working fine now.

There is only one problem, but I'll take a look at it myself.

Once I submit the message the message pops up but the date does not unless I refresh.

I have removed it from here because internet explorer errored.

success: function(){
                $("ul#wall").prepend("<li style='display:none'>"+message_wall+"</li><hr />");
                $("ul#wall li:first").fadeIn();

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 24, 2010 Mar 24, 2010

Copy link to clipboard

Copied

I think I was abit hasty, the date is not being inserted into the database.

I will try and play around with it, if not I will have to take this functionality off.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 24, 2010 Mar 24, 2010

Copy link to clipboard

Copied

Hi

The best way to insert a date from the php date function, into a mysql database for such items, is to set the database field as text and insert as such.

PZ

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 24, 2010 Mar 24, 2010

Copy link to clipboard

Copied

Hi

It may be worth looking at the source code for how I have used the ajax form plug-in on my site, at - http://www.pziecina.com/design/index.php.

The form code is not the actual ajax page code but the code just before the closing </head> tag.

As for the echo statement in the insert, try changing this to use the concatenation type of code -

echo "Information inserted on" . $date;

The original should work, but one never knows with ajax.

PZ

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 25, 2010 Mar 25, 2010

Copy link to clipboard

Copied

Im not having any luck.

I am trying to send the values but they do not want to send.

It works via php

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 25, 2010 Mar 25, 2010

Copy link to clipboard

Copied

Hi

What does your form code look like?

PZ

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 25, 2010 Mar 25, 2010

Copy link to clipboard

Copied

The form looks like this:

<form action="" id="submit_wall" name="submit_wall">
<label for="message_wall"><img src="images/icons/edit-comment-green.gif" width="14" height="14" /> Share your message on the Wall</label>
<br />
<span id="sprytextarea1">
<label>
<textarea name="name" id="message_wall" cols="70" rows="2" onclick="make_blank();"></textarea>
<span id="countsprytextarea1"> </span></label><br />
<span class="textareaRequiredMsg">A value is required.</span><span class="textareaMaxCharsMsg">Exceeded maximum number of characters.</span></span>
<br />
<input name="pro_to" type="hidden" value="<?php echo $row_user_profile['user_id']; ?>" id="pro_to"  />
<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="profile_from" type="hidden" value="" />
<input name="msg_date" type="hidden" value="<?php echo date("d/m/y");?>" />
</form>

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 25, 2010 Mar 25, 2010

Copy link to clipboard

Copied

Hi

Looking at your form, it appears that you are using standard javascript and the spry framework validation in the form, which will give you problems, even using the jQuery validation extension causes problems with the jQuery ajax form.

All I can suggest is to use php form validation as the pre-processing section of your insert.php page and set the jQuery form option to "resetForm: false".

This is a known problem with using the jQuery form plug-in.

PZ

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 25, 2010 Mar 25, 2010

Copy link to clipboard

Copied

im sorry,

I do not know how to do any of that.

:'(

I must be stupid

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 25, 2010 Mar 25, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 25, 2010 Mar 25, 2010

Copy link to clipboard

Copied

shall I abandon this?


Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Mar 25, 2010 Mar 25, 2010

Copy link to clipboard

Copied

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
Mar 25, 2010 Mar 25, 2010

Copy link to clipboard

Copied

I will try one more time, if I can findout how to send more than one value so that it can be inserted into the database.

if not then I will cancel this.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines