1

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
  • You would benefit from watching this video .. and actually others from "Clean Code Talks" series. Commented Mar 13, 2012 at 23:22

2 Answers 2

1

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
}
Sign up to request clarification or add additional context in comments.

2 Comments

The only problem I'm having now is that it adds the details to the database when I two integer values, but when I put one using characters I get the following error message:
NetworkError: 500 Internal Server Error. The field type is set to varchar(20) as well so I cant understand what the problem is
0

You should basically write out your functions and then at the end print out the resulting SQL statements it creates. Once you're able to see them from that level, you can try them in a query browser to see if you're constructing them correctly.

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.