Number of fields - PDO

Asked

Viewed 44 times

1

I built some crud classes in PDO and almost all have 4 or 5 fields in DB, worked correctly.

Now I’m trying to create a crud of 6 fields, but does not create the record in DB at all, I rewrote the class thinking it is some typo, but even doing everything again remains the same, without registering the data in DB. isset Post is taking the form data correctly, variable naming is correct

With 4 columns in DB:

public function create($testA,$testB,$testC,$testD)
    {
        try
        {
            $stmt = $this->db->prepare("INSERT INTO ColunaUm(testA,testB,testC,testD) VALUES(:testA, :testB, :testC, :testD)");
            $stmt->bindparam(":testA",$testA);
            $stmt->bindparam(":testB",$testB);
            $stmt->bindparam(":testC",$testC);
            $stmt->bindparam(":testD",$testD);
            $stmt->execute();
            return true;
        }
        catch(PDOException $e)
        {
            echo $e->getMessage();
            return false;
        }

    }

With 6 Columns:

public function create($testA,$testB,$testC,$testD,$testE,$testF)
{
    try
    {
        $stmt = $this->db->prepare("INSERT INTO ColunaDois(testA,testB,testC,testD,testE,testF) VALUES(:testA, :testB, :testC, :testD, :testE, :testF)");
        $stmt->bindparam(":testA",$testA);
        $stmt->bindparam(":testB",$testB);
        $stmt->bindparam(":testC",$testC);
        $stmt->bindparam(":testD",$testD);
        $stmt->bindparam(":testE",$testE);
        $stmt->bindparam(":testF",$testF);
        $stmt->execute();
        return true;
    }
    catch(PDOException $e)
    {
        echo $e->getMessage();
        return false;
    }

}

In PDO there is a different treatment when saving depending on the amount of fields?

  • 1

    If no error is returned. Try using toString() to see what SQL is returning.

  • add the structure of the Colunadois table

  • @Felipeduarte the table Colunadois this ok tbm, I checked the data types, the names and are identical

  • @cHida you have an example of how to use the toString()? would be: public function __toString() { return $this->Db; } ?

  • What is the error message?

  • @rray without error message

  • Do the following and see if any error appears, change: $stmt->execute(); for if(!$stmt->execute()){ print_r($stmt->errorInfo());}. The code of the question seems correct I imagine that this is not the real code (the one with problem).

  • @rray returns error that was not registered in DB because of the isset I put, if not register appears the message was not registered. But PHP error alert is not showing up even with the code you indicated

  • Has no isset() in the question code. It becomes complicated to answer on top of a code other than real.

  • 1

    ok I took isset from custom error: this is returning the following error: SQLSTATE[23000]: Integrity constraint violation: 1048 Le champ 'testF' ne peut �tre vide (null)

  • testF is type Enum, in the form I am passing the value like this: <input type="text" name="testF" class="form-control" value="Disponivel" disabled>

  • Apparently came empty or the value does not exist no source table. Okay this error message in French?

  • It is better to edit the question and add these details. pq does not create a new table or treat this logic in PHP than to use an Enum?

  • It is at least curious, the other tests with less fields also worked with Enum in the same way, and did not have this error

  • error is also appearing: Notice: Undefined index: testF

  • Must not send its value pq the field is as disabled

  • 1

    our kkk was just that, just the disabled thing I didn’t even notice it, thinking it was something in the PHP code. Thanks solved

Show 12 more comments

1 answer

2


Seen by the comments the problem was that your Insert sent a null to a field ($testF) not null. The cause is in the html form where the input had the attribute disabled. So the solution is to remove this attribute.

Change:

<input type="text" name="testF" class="form-control" value="Disponivel" disabled>

To:

<input type="text" name="testF" class="form-control" value="Disponivel" >

Browser other questions tagged

You are not signed in. Login or sign up in order to post.