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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T08:28:06+00:00 2026-05-31T08:28:06+00:00

I’m quite new with PHP and need help coding an add script for my

  • 0

I’m quite new with PHP and need help coding an add script for my web site. I have coded the delete and update side and they are working perfectly. Basically, on secdtions of my web site you can add values to several text boxes and what I want is that when you click on ‘Add’ this will add the details from the textboxes to the database. To do this I am using PHP, Jquery and Ajax.

This is the code I have for the update script:

public function update($tableName,$fieldArray,$fieldValues,$rowId,$updateCondition)
{
    // Get PDO handle
    $PDO = new SQL();
    $dbh = $PDO->connect(Database::$serverIP, Database::$serverPort, Database::$dbName, Database::$user, Database::$pass);

    // Build query
    $this->sql = 'UPDATE '.$tableName.' SET ';

    $fieldCount = count($fieldArray);

    for ($i = 0; $i < $fieldCount; $i++){

        // If the index is at the last field...
        $lastRow = $fieldCount - 1;

        if ($i != $lastRow) {

            // Add a comma
            $this->sql .= $fieldArray[$i].'=:'.$fieldArray[$i].', ';

        } else {

            // Dont add a comma
            $this->sql .= $fieldArray[$i].'=:'.$fieldArray[$i].' ';

        }

    }

    // If row id is null (if we don't know the row id)...
    if ($rowId == null || $rowId == "null") {

        // Then use the update condition in it's place
        $this->sql .= 'WHERE '.$updateCondition.' ';

    } else {

        // Use the ID
        $this->sql .= 'WHERE Id = '.$rowId.' ';

    }



    try {

        // Query
        $stmt = $dbh->prepare($this->sql);

        // Bind parameters
        for ($i = 0; $i < $fieldCount; $i++){

            $stmt->bindParam(':'.$fieldArray[$i].'', $fieldValues[$i]);

        }

        $stmt->execute();

        $count = $stmt->rowCount();

        echo $count.' row(s) affected by SQL: '.$stmt->queryString;


        $stmt->closeCursor();

    }

    catch (PDOException $pe) {
        echo 'Error: ' .$pe->getMessage(). 'SQL: '.$stmt->queryString;
        die();
    }   

    // Close connection
    $dbh = null;
}

This is the part I am struggling to code, if you look at the code I have used for my update script.. I basically need something similiar to use for my ‘add’ script.

Any help will be much appreciated!!

  • 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-31T08:28:07+00:00Added an answer on May 31, 2026 at 8:28 am

    Welcome to PHP! It is really a wonderful language 🙂

    Try this:

    <?php
    
    public function insert($tableName,$fieldArray,$fieldValues)
    {
        $sql = "INSERT INTO " . $tableName . " (".implode(',', $fieldArray).") VALUES (".implode(',', $fieldValues).")";
    
        // TODO: Execute $sql query
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
I have a jquery bug and I've been looking for hours now, I can't

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.