0

I can send a query to mysql database with following code:

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

My questions is, Is there any way to send a query with just a click on some text.

Let's example: there are a text name Reply. I want if i click this Reply text then mysql database field value (field name: Reply, type: int) will be increase by 1.

Sorry I DON'T KNOW ABOUT JAVASCRIPT/AJAX:(

FINAL UPDATER CODE TO @DEVELOPER:

<html>
<head>
<title>Untitled Document</title>
</head>
<script language="javascript">
$("#mylink").click(function() {
$.ajax({
url: "test.php"
}).done(function() { 
$(this).addClass("done");
});
});
</script>
<body>
echo "<a href='#' id='mylink'>Reply</a>";
</body>
</html>
Php page:
<?php
include("database/db.php");
$sql = mysql_query("INSERT INTO wall VALUES('','','','','','','','1');");
?>
5
  • you can use this using jquery and php//// Commented May 21, 2012 at 5:47
  • Yes, there is a way, you can use html form or ajax Commented May 21, 2012 at 5:47
  • You would use $__Session , $__get[....] variables on a page request but you should escape your SQL queries to prevent xss. Commented May 21, 2012 at 5:49
  • Be careful of SQL injection attacks! Commented May 21, 2012 at 5:52
  • Step one: use mysqli instead... Commented May 21, 2012 at 6:29

3 Answers 3

1

You should have this link or button to be clicked wired to an ajax call using jQuery

http://api.jquery.com/jQuery.ajax/

It should call a php page, which contains the query you're looking to run. You can pass in arguments with the ajax call as well, so that your $message and $replyno are set properly before executing.

<script>
$("#mylink").click(function() {
  $data = $("#myform").serialize();
  $.ajax({
    url: "postquery.php",
    data: $data
  }).done(function() { 
     $(this).addClass("done");
  });
});
</script>

then your php page would look something like this:

<?php
...
$message = mysql_real_escape_string($_REQUEST['message']);
$replyno = mysql_real_escape_string($_REQUEST['replyno']);
$sql = mysql_query("INSERT INTO wall VALUES('', '$message', '$replyno')");
....
?>

Excaping your incoming strings using "mysql_real_escape_string" is always important to prevent SQL Injection attacks on your database.

Your HTML should look something like this:

<html>
...
<input type="textarea"></input>
<a href="#" id="mylink">Reply</a>
...
</html>

This will cause the previously stated jquery statement to trigger when "Reply" is clicked.

Here is with your updated code. I corrected the link ID and also removed the form serialization data since your test code does not appear to need it. I also added the reference to the jQuery library:

<html>
<head>
<title>Untitled Document</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<script language="javascript">
$("#mylink").click(function() {
  $.ajax({
    url: "test.php"
  }).done(function() { 
     $(this).addClass("done");
  });
});
</script>
</head>
<body>
<a href='#' id='mylink'>Reply</a>
</body>
</html>

The problems you're likely seeing are because of your query, not the front end code. Try adding some debug code like this:

<?php
include("database/db.php");
$sql = mysql_query("INSERT INTO wall VALUES('','','','','','','','1');");
if(!$sql)
{
  echo mysql_error();
}
?>

Or try checking your servers error logs.

Sign up to request clarification or add additional context in comments.

11 Comments

Thanks @Developer. But I just want to add reply value not message. If anyonce click the "Reply text" then only Mysql database reply field value increase..
If you're looking for an ID to increase by one, you should probably be using a AUTO_INCREMENT field in mysql. dev.mysql.com/doc/refman/5.0/en/example-auto-increment.html. If not, you'll have to be more specific as to what you're trying to do. Thanks
I know how to use auto-increment. But how do i send a query with just a click on Reply text.
I have updated my answer with the html which would be needed as well in order to trigger the ajax call.
is it require to use <script> your code </script> tag for $("#mylink").click(function() ?
|
0
$sql = mysql_query("INSERT INTO wall VALUES('', '$message', '$replyno')");

You have to use jquery and ajax like this:-

<script type="text/javascript">
    $j(document).ready(function() {
        $j('#reply').click(function(){

    jQuery.ajax({
                url: "test.php", //Your url detail
                type: 'POST' ,
                data: ,
                success: function(response){
   }
  });
 });
});
</script>

Comments

0

In the file "updat_post.php" write:

If(isset($_GET['visit_post']))
     $pdo->query('update posts set counter = counter+1');

In your js/jquery file on document ready write:

$('#mybutton').click(function() {
    $.post('update_post.php', {visit_post: true});
});

Comments

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.