Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5982093
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:02:15+00:00 2026-05-22T22:02:15+00:00

I am reading a tutorial on how to insert and update data into a

  • 0

I am reading a tutorial on how to insert and update data into a MySQL table using PHP, the code is listed below. My problem is when i click update but I have not modified any data, rowCount() returns 0 and breaks the code.

My question is, If I am simply updating the database with the same values that are in the database, why does rowCount() return zero? My thoughts were that even though it was the same data it would be inserted anyway and return a count of the updated rows? I am guessing that it check the data before it try’s the update? Can anyone shed some light on this for me and suggest a workaround? I have been starring at the code for hours and have been unable to come up with anything, thanks.

<?php
require_once('../includes/connection.inc.php');
// initialize flags
$OK = false;
$done = false;
// create database connection
$conn = dbConnect('write', 'pdo');
if (isset($_GET['article_id']) && !$_POST) {
    // prepare sql query
    $sql = 'SELECT article_id, title, article FROM blog WHERE article_id = ?';
    $stmt = $conn->prepare($sql);
    // bind the results
    $stmt->bindColumn(1, $article_id);
    $stmt->bindColumn(2, $title);
    $stmt->bindColumn(3, $article);
    // execute query by passing array of variables
    $OK = $stmt->execute(array($_GET['article_id']));
    $stmt->fetch();
}
// if form has been submitted, update record
if (isset($_POST['update'])) {
    //prepare update query
    $sql = 'UPDATE blog SET title = ?, article = ? WHERE article_id = ?';
    $stmt = $conn->prepare($sql);
    // execute query by passing array of variables
    $stmt->execute(array($_POST['title'], $_POST['article'], $_POST['article_id']));
    $done = $stmt->rowCount();
}
// redirect page on sucess or if $_GET['article_id'] not defined
if ($done || !isset($_GET['article_id'])) {
    header('Location: http://localhost/PHP_Solutions/admin/blog_list_pdo.php');
    exit();
}
// store error message if query fails
if (isset($stmt) && !$OK && !$done) {
    $error = $stmt->errorInfo();
    if (isset($error[2])) {
        $error = $error[2];
    }
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Update Blog Entry</title>
<link href="../styles/admin.css" rel="stylesheet" type="text/css">
</head>

<body>
<h1>Update Blog Entry</h1>
<p><a href="blog_list_pdo.php">List all entries </a></p>
<?php if (isset($error[2])) {
    echo "<p class='warning'>Error: $error[2]</p>";
    echo '<pre>';
    print_r($_POST);
    print_r($error);
    echo '</pre>';
}
if ($article_id == 0) { ?>
    <p class="warning">Invalid request: record does not exist.</p>
<?php } else { ?>
<form id="form1" method="post" action="">
    <input name="article_id" type="hidden" value="<?php echo $article_id; ?>">
  <p>
    <label for="title">Title:</label>
    <input name="title" type="text" class="widebox" id="title" value="<?php echo htmlentities($title, ENT_COMPAT, 'utf-8'); ?>">
  </p>
  <p>
    <label for="article">Article:</label>
    <textarea name="article" cols="60" rows="8" class="widebox" id="article"><?php echo htmlentities($article, ENT_COMPAT, 'utf-8'); ?></textarea>
  </p>
  <p>
    <input type="submit" name="update" value="Update Entry" id="update">
  </p>
</form>
<?php } ?>
</body>
</html>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T22:02:16+00:00Added an answer on May 22, 2026 at 10:02 pm

    My question is, If I am simply updating the database with the same values that are in the database, why does rowCount() return zero?

    rowCount is counting the affected rows by a query. As you haven’t changed anything, there are zero affected rows.

    PDOStatement->rowCount — Returns the number of rows affected by the last SQL statement

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Been reading the tutorial How to handle a Many-to-Many relationship with PHP and MySQL
When I was reading this tutorial I noticed the following performance tip about using
I am reading one tutorial :- http://www.tutorialized.com/view/tutorial/Integrating-Flash-and-mySQL/2334 But i can't understand what is Gateway.php
I was reading this tutorial for a simple PHP login system . In the
According to the tutorial I am reading, the program below displays Hello Ima Reader.
Bad idea mixing HTML and PHP? I was reading this tutorial and it seems
I just started reading Michael Hartl's Ruby on Rails 3 Tutorial. He recommends using
I'm reading a tutorial about creating a shoutbox with jquery, php and ajax. In
I am reading a tutorial that uses the following example (that I'll generalize somewhat):
I've been reading many a tutorial/article on unmanaged DLLs in C++. For the life

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.