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 8873051
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T18:22:09+00:00 2026-06-14T18:22:09+00:00

Im currently following a tutorial on creating a forum in PHP / MySQL …

  • 0

Im currently following a tutorial on creating a forum in PHP / MySQL … and im trying to implement it into my CodeIgniter project.

I have hit a snag that i have never dealt with before, transactions … I have read the documentation on codeigniters transactions, but im not really understanding it considering the code i need to convert.

I was wondering if someone could take the code below and turn it into codeigniters transaction code for me, i have tried doing it myself but its using multiple tables and i just get completely confused.

Any help would be great, code is below:

$query = "BEGIN WORK;";
$result = mysql_query($query)

if(!$result) {
    echo 'An error has occured';
} else {
    $sql = "INSERT INTO topics(t_subeject,date,cat) VALUES ($_POST['subject'],NOW(),$_POST['cat'])";
    $result = mysql_query($sql);

    if(!$result) {
        echo 'An error has occured';
        $sql = "ROLLBACK;";
        $result = mysql_query($query)
    } else {
        $topid = mysql_insery_id();
        $sql = "INSERT INTO posts(content, date) VALUES ($_POST['content'],NOW())";
        $result = mysql_query($sql);

        if(!$result) {
            echo 'An error has occured';
            $sql = "ROLLBACK;";
            $result = mysql_query($sql);
        } else {
            $sql = "COMMIT;";
            $result = mysql_query($sql);
            echo 'Insert successful!';
        }
    }
}
  • 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-06-14T18:22:11+00:00Added an answer on June 14, 2026 at 6:22 pm

    If an INSERT failed, it will do a ROLLBACK automatically. That’s why you’re using transactions in the first place. No need to explicitly check for it. So in the end, this should be all there is to it:

    $this->db->trans_start();
    $this->db->query("INSERT INTO topics(t_subeject,date,cat) VALUES ($_POST['subject'],NOW(),$_POST['cat'])");
    $topid=$this->db->insert_id();
    $this->db->query("INSERT INTO posts(content, date) VALUES ($_POST['content'],NOW())");
    $this->db->trans_complete();
    

    …from within your model class. So if the first INSERT already fails, it won’t execute the second.

    http://ellislab.com/codeigniter/user_guide/database/transactions.html

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

Sidebar

Related Questions

I'm currently following the third tutorial listed here: here where I'm trying to compile
I have been following a tutorial on creating an iPhone app with multiple xibs.
I have following problem. I use this tutorial to creating a multiple file uploader:
I'm currently following a PHP tutorial, and I've got the following code:: $sql =
I am currently following this tutorial about getting started on PhoneGap. I have installed
I'm currently following a tutorial in a book, and it instructs to create a
I'm following the python tutorial at their site and I'm currently at the break
Currently I have following XML: <Rowsets> <Rowset> <Row> <DrilldownDepth>0</DrilldownDepth> <ScheduleWeek>---</ScheduleWeek> <ABC>---</ABC> <PQR>1500</PQR> <XYZ>15000</XYZ> <Quant>285</Quant>
I currently have the following controller method in a Rails app: def index @entries
I am currently learning the MVVM pattern, and the tutorial I am following uses

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.