Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I obtained an error:
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'First_Name' cannot be null (SQL: insert into `customers` (`First_Name`, `Last_Name`, `Email`, `Credit_Card`, `Password`) values (?, ?, ?, ?, ?))


Here is my controller:
class InputController extends Controller
{
    public static function save(Request $request)
    {
        $First_Name = $request->First_Name;
        $Last_Name = $request->Last_Name;
        $Email = $request->Email;
        $Credit_Card = $request->Credit_Card;
        $Password = $request->Password;

        $customer = new CustomersTable;
        $customer->First_Name = $First_Name;
        $customer->Last_Name = $Last_Name;
        $customer->Email = $Email;
        $customer->Credit_Card = $Credit_Card;
        $customer->Password = $Password;

        $customer->save();
    }
}


What I have tried:

I've been stuck for 2 hours try to use post things to the database, even use sample code on Stackoverflow.
Posted
Updated 15-Aug-21 0:55am
Comments
Richard MacCutchan 15-Aug-21 6:37am    
Where is the code that tries to send the information to the database?
HackerRhino 15-Aug-21 23:12pm    
I use by my controller. But now it worked.

1 solution

You get that error because the value you are actually sending to SQL is null - it has no value, and your column is (rightly) set to not accept null first names.

So you need to find out exactly what you are sending, and why it isn't what you think it is. Most likely, the problem is either here:
PHP
$First_Name = $request->First_Name;
Or here:
PHP
$customer->save();
But the first is most likely.

So use the debugger (A Detailed Guide to PHP Debugging – Stackify[^]) to find out exactly what is is your variables at run time. At a guess, the request has no First_name value, but we can't run your code under the same circumstances as you so we can't tell.

When you have found out what exactly is null you can start look for why that doesn't have the value you expected.

Sorry, but we can't do any of that for you - time for you to learn a new (and very, very useful) skill - debugging!
 
Share this answer
 
Comments
HackerRhino 15-Aug-21 23:12pm    
Thanks for helping me. It eventually worked!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900