/    Sign up×
Community /Pin to ProfileBookmark

PHP AJAX MySQLi UPDATE of + 1 not working

Hi, I’m trying to update comment number but it’s not working

comments.php

[code=php]<div class=”comment-form”>
<form method=”post” autocomplete=”off”>
<div class=”hiddens”>
<input type=”hidden” id=”blog_id” value=”<?php echo $blog_id ?>” />
</div>
<fieldset>
<legend>Leave a Comment</legend>
<ul>
<li class=”msg-box” id=”comment-msg” hidden><p></p></li>
<li>
<label for=”cname”>Complete name<span class=”required”>*</span></label>
<input type=”text” id=”cname” />
</li>
<li>
<label for=”cname”>Email address<span class=”required”>*</span></label>
<input type=”text” id=”email” />
</li>
<li>
<label for=”cname”>Website</label>
<input type=”text” id=”website” />
</li>
<li>
<label for=”cname”>Comment<span class=”required”>*</span></label>
<textarea id=”comment” minlength=”10″ maxlength=”300″></textarea>
</li>
<li>
<input type=”submit” value=”Post Comment” />
</li>
</ul>
</fieldset>
</form>
</div>[/code]

comments.js

[CODE]var message;

$(document).ready(function () {
message = $(‘#comment-msg’);

$(‘.comment-form form’).submit(function (e) {
e.preventDefault();

var cname = $(‘#cname’),
email = $(‘#email’),
website = $(‘#website’),
comment = $(‘#comment’),
blog_id = $(‘#blog_id’);

// validate…
if (cname.val() === ” || email.val() === ” || comment.val() === ” || blog_id.val() === ”) {
$.addMessage(message, ‘error’, ‘Fields with (*) are required.’);
} else {
$.addMessage(message, ‘warning’, ‘Processing…’);
$.disableFields(‘.comment-form’);

$.post(‘process.php’, {

action: ‘post’,
cname: cname.val(),
email: email.val(),
website: website.val(),
comment: comment.val(),
blog_id: blog_id.val()

}, function (data) {
switch (data) {
case ‘error’:
$.enableFields(‘.comment-form’);
$.addMessage(message, ‘error’, ‘Processing failed.’);
break;
case ’email’:
$.enableFields(‘.comment-form’);
$.addMessage(message, ‘error’, ‘Invalid email address.’);
default:
$.resetFields(‘.comment-form’);
$.addMessage(message, ‘success’, ‘Your comment was successfully posted.’);
break;
}
}).error(function () {
$.addMessage(message, ‘error’, ‘Processing failed.’);
$.enableFields(‘.comment-form’);
});
}
});
});[/CODE]

process.php

[code=php]
<?php
require_once(dirname(__DIR__) . ‘/classes/database.php’);

// if is not an ajax request or a post method…
if (!$forms->isAjax() or !$forms->isPost()) {
die(‘error’);
} else {
$commentOk = 1;
$cname = trim($_POST[‘cname’]);
$email = trim($_POST[’email’]);
$phone = trim($_POST[‘website’]);
$message = trim($_POST[‘comment’]);
$blog_id = trim($_POST[‘blog_id’]);

// validate email address
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

// Validate e-mail
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
die(’email’);
$commentOk = 0;
}

if ($commentOk === 1) {
// auto update comment number
$stmt = $sqlConnection->prepare(‘UPDATE blogs SET num_comments = num_comments + 1 WHERE id = ? LIMIT 1’);
$stmt->bind_param(‘i’, $blog_id);
$stmt->execute();
$stmt->store_result();

if ($stmt->affected_rows === 1) {
die(‘success’);
} else {
die(‘error’);
}
}
}[/code]

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@holyhttpFeb 08.2016 — Did you track the execution of your program to make sure:

- The form data is sent properly

- The POSTed data is received on the server-side

- You are shown the "success" message at the end of the process
Copy linkTweet thisAlerts:
@tubc2015authorFeb 09.2016 — Yes, I'd already did that still the result doesn't change.
Copy linkTweet thisAlerts:
@holyhttpFeb 09.2016 — If your SQL query returns a success message and did not update your num_comments fields then maybe the blog_id you are providing is incorrect.

How about test your SQL by simply writing a plain query without any placeholder.

First make sure the blog_id exists in your database table.

Let assume the blog_id is 2.

Then simply run the query: UPDATE blogs SET num_comments = num_comments + 1 WHERE id =2 LIMIT 1

and see what happens. If that work, then in your PHP code, set $blog_id=2 then run your program:

if ($commentOk === 1) {

$blog_id=2; //to be removed once all runs correctly.

// auto update comment number

$stmt = $sqlConnection->prepare('UPDATE blogs SET num_comments = num_comments + 1 WHERE id = ? LIMIT 1');

$stmt->bind_param('i', $blog_id);

$stmt->execute();

$stmt->store_result();

if ($stmt->affected_rows === 1) {
die('success');
} else {
die('error');
}
}



Those incremental tweaks will help you pinpoint where in your program the bug is. I always work backward from the database query, PHP then the front-end.
Copy linkTweet thisAlerts:
@tubc2015authorFeb 10.2016 — I'd found the bug on these lines on process.php:
[code=php]
// validate email address
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

// Validate e-mail
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
die('email');
$commentOk = 0;
}[/code]


This means that even the email address was incorrect, still it responds "success" message; because of that, the comment number didn't change. Besides, when I remove these lines, the comment number has changed. On what section should I put these lines on process.php file?
Copy linkTweet thisAlerts:
@RationaltechFeb 10.2016 — <?php

require_once("dbcontroller.php");

$db_handle = new DBController();

$column=$_POST['column'];

$value=$_
POST['value'];

$id=$_POST['id'];

$sql = "UPDATE php_interview_questions SET $column = '$value' WHERE id=$id)";

$result = mysqli_query ($conn, $sql) or die(mysqli_error ($dbc));

?>

web designing company in hyderabad
Copy linkTweet thisAlerts:
@holyhttpFeb 10.2016 — How about just this:

<?php

require_once(dirname(__DIR__) . '/classes/database.php');

// if is not an ajax request or a post method...

if (!$forms->isAjax() or !$forms->isPost()) {

die('error');

}

else {

$commentOk = 1;

$cname = trim($_POST['cname']);

$email = trim($_
POST['email']);

$phone = trim($_POST['website']);

$message = trim($_
POST['comment']);

$blog_id = trim($_POST['blog_id']);

// validate email address
$email = filter_var($email, FILTER_SANITIZE_EMAIL);

// Validate e-mail
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
die('email');
}
else {
// auto update comment number
$stmt = $sqlConnection->prepare('UPDATE blogs SET num_comments = num_comments + 1 WHERE id = ? LIMIT 1');
$stmt->bind_param('i', $blog_id);
$stmt->execute();
$stmt->store_result();

if ($stmt->affected_rows === 1) {
die('success');
} else {
die('error');
}
}

}

The $commentOk variable is unnecessary since the execution of your database transaction is only contingent on the email address being valid.
×

Success!

Help @tubc2015 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...