In my controller.php
How to save multiple query rows of data with additional input fields data in a new table?
error message: below. Thanks.
QueryException in Connection.php line 647: SQLSTATE[42000]: [Microsoft][ODBC Driver 11 for SQL Server][SQL Server]Incorrect syntax near ')'. (SQL: insert into [timetracks] () values ())
This is my goal: insert multiple rows of data with additional values to the new table in laravel 5.4
But, in order to achieve my goal, there are several issues below. I can query a multiple rows of data from the old table. But, I don't know how to use another controller to get the correct IDs (plural, coz multiple rows of data) which is called 'CEN' as primary key ---- correct records to insert to the new table?
Please help if you or other experts can. Thanks.
in updatelogcontroller.php --- I changed my code a lot to get improved. But, not working yet.
Make sure you add
protected $fillable = ['Date', 'Badge', 'CEN', 'PFN', 'LNAM', /* etc. */ ];
In your model
@silverxjohn I don't know if you can see my code that I posted yesterday. I don't know why it's not showing the post with my codes. But, I can see my post if I click edit icon.
Anyway, Thanks. I did that in the model. AS you can see the error message above that the system could insert date and badge but not the rest from another table. I think I need the controller needs to find a way to fetch those data correctly first.
Secondly, I am thinking I need to find a way to count those multiple rows of data and then assign ID to each row, so that I think the system knows it's multiple rows of data by checking the IDs and the system knows to insert and save to another table.
Currently, my code in the controller as follows:
@achieve100 Honestly, I don't know what it is you're trying to accomplish.
Would you mind sending a link to your code repository and explain exactly it you're trying to do?
I'm sure many people here in this forum would like to help you. But do us a favor by explaining things so that it is easier for us to understand.
Communication is the key! lol
I think the error message is pretty clear on what the problem is:
Cannot insert the value NULL into column 'CEN' ... column does not allow nulls.
You need to make your 'CEN' column be able to accept null values by creating a migration and set that field to nullable(), or check if it's null before inserting and convert it to an empty string, or a 0, or whatever value would be appropriate for the column type that CEN is, or if you are expecting a value, investigate why it's coming back as null instead of what you are expecting.
to show code, enter 3 backticks (`) on a line. Paste your code on the next line. Then on the line after your code, enter 3 more backticks.
@silverxjohn Sorry. Let me reorganize my questions with my coding below.
I haven't gone though all that code, but I did see that you are not assigning any values to your options, so that's why they return null, at least here:
@foreach ($FACs as $FAC)
<option>{{ $FAC->HFAC_NAME }}</option>
@endforeach
Edit: also, you don't have any form field with the name "CEN". It's commented out. So when you try to grab it with $CEN = Request::get('CEN'); it's null.
It's been answered already, as I showed in another post, grab the data and insert it. Like you would do any insert in any table.
To better explain:
- Loop through and get the checked rows
- get the data
- insert the data
It's just that easy.
@Cronix Thanks for your suggestion. Let me try.
@jlrdw Thanks. Unfortunately, it's not that easy. If you take a look my reply with concern issues, then you understand more.
I am not only fetching multiple rows of data from one table, I also need to add some additional fields from drop down menus, then, insert all rows to another table call timetracks table.
@achieve100 make your life easier, use a popup table instead of a dropdown, much easier. That was recently demoed in another recent post. Just my advice. However you have to know how to return data from a child to parent window with javascript / ajax. If you haven't learned that yet, probably won't work for you. But would be a good time to learn it.
But step one is getting the field types correct as @Cronix indicated.
@jlrdw Thanks. I think using javascript/ajax will make my life tougher as I don't want to have too much change front end. I want to concentrate to spend time to fix back end at this moment. But, thanks for your suggestion.
@Cronix I found out the following can get the data.
$CEN = DB::table('popage')->value('CEN');
However, it's the first row of the data in the table. And I need to get the query data I made from another controller. I just don't know how to get the correct data (Multiple CEN in this case)
By the way, I used (search_log) method from another controller even though I got it. I don't know how to use it in this (update_log) controller
app('App\Http\Controllers\HomeController')->search_log();
@jlrdw @Cronix Thanks for both. I have resolved the value for the drop down menu. Also, I could insert one row in the database although it's not the query data that I wanted to insert in. At least, I can prove the insert function is working to the new table by combining data from old table and selected data from drop menu and insert together to the new table.
Now, I need to fix or if some of experts can help me, that would be great.
- fetch the correct data and insert to the new table.
- insert and save one time with multiple rows not a single row by clicking the save button. Currently, I am using tag to try to save multiple rows in the table.
I believe I need to work on the controller again and probably I need to work on the home.blade.php as well. But, I don't know how at this moment. Doing researching now or please help if you know. Thanks so much.
you will learn more if you take the time to review the browser development tools.
you can then see what your elements are called and what is posted to the server when you press submit
i don't think your problems are laravel related yet
Do you mean the Google Inspect on the web? I did the inspection from Google. Below is the error message although I didn't get any in my web site. It's from the Google inspection console result. I don't know what does it mean?
ncaught ReferenceError: response is not defined
at HTMLDocument.<anonymous> (home:207)
at i (jquery-1.12.0.min.js:2)
at Object.fireWith [as resolveWith] (jquery-1.12.0.min.js:2)
at Function.ready (jquery-1.12.0.min.js:2)
at HTMLDocument.K (jquery-1.12.0.min.js:2)
(anonymous) @ home:207
i @ jquery-1.12.0.min.js:2
fireWith @ jquery-1.12.0.min.js:2
ready @ jquery-1.12.0.min.js:2
K @ jquery-1.12.0.min.js:2
I
No, thats not what I mean
In Chrome web browser
right click on page and select inspect
In the top panel that opens, you will see Memory, Elements, Console, Sources, Network (you may need to scroll the menu)
Choose Network
Press submit on your form.
The very first row that appears is the request TO the server.
Click on this. On the right are the the Headers. Scroll to the bottom and you will see the form data that is submitted to Laravel and importantly, the exact names of all the elements being passed.
@Snapey I got it. Thanks. Although you can point out the issue which is sharing the name, however, if I am using different name, I am sure it won't even insert to the correct field name since it's different. In this case, I have 2 questions as follows:
-
How can I configure correct datetime data type in order to convert successfully from php string to mssql datetime data type?
-
How can I insert 2 drop down menus with different sub-names to one column field?
Thanks.
What type of column are you writing to? (Time, timestamp, dateTime or varchar)
datetime
ok, so assume you have two input fields for hours and minutes. What are you going to set the date to? Assume it is today, you could use Carbon like
$dt = Carbon::today(); // (today at 00:00)
$dt->addHours($request->hours);
$dt->addMinutes($request->minutes);
then write $dt into your database column.
Thanks. But, I got Class 'App\Http\Controllers\Carbon' not found. I think I need to add this class in some where?
you need to 'import' it
At the top of your controller, add use Carbon\Carbon;
Fix the form
<td>
<select type="time" id="OutTimeHour" name="OutTimeHour" value="">
<option selected="selected" value="--">--</option>
<?php for($i = 0; $i <= 23; $i++): ?>
<option value="{{$i}}">{{$i}}</option>
<?php endfor; ?>
</select>
<?php echo ':'; ?>
<select type="time" id="OutTimeMinute" name="OutTimeMinute" value="">
<option selected="selected" value="--">--</option>
<?php for($j = 0; $j <= 59; $j++): ?>
<option value="{{$j}}">{{$j}}</option>
<?php endfor; ?>
</select>
</td>
<td>
<select type="time" id="InTimeHour" name="InTimeHour" value="">
<option selected="selected" value="--">--</option>
<?php for($i = 0; $i <= 23; $i++): ?>
<option value="{{$i}}">{{$i}}</option>
<?php endfor; ?>
</select>
<?php echo ':'; ?>
<select type="time" id="InTimeMinute" name="InTimeMinute" value="">
<option selected="selected" value="--">--</option>
<?php for($j = 0; $j <= 59; $j++): ?>
<option value="{{$j}}">{{$j}}</option>
<?php endfor; ?>
</select>
</td>
now, you have 4 parameters returned from the form.
- OutTimeHour, OutTimeMinute
- InTimeHour, InTimeMinute
Then change the controller;
$dt = Carbon::today();
$dt->addHours($Request->OutTImeHour);
$dt->addMinutes($Request->OutTimeMinute);
$Timetrack->OutTime = $dt
$dt = Carbon::today();
$dt->addHours($Request->InTImeHour);
$dt->addMinutes($Request->InTimeMinute);
$Timetrack->InTime = $dt
You will also need to fix the validation since you have 4 time values, but since you should also validate all the other fields, you might as well comment all that out for now and then concentrate on validation when this is working.
As an idea
'OutTimeHour' => 'required|integer|max:23',
'OutTimeMinute' => 'required|integer|max:59',
Thanks. Snapey. It's almost successful. It could successfully be inserted in the table finally. But, it's only shown in mssql as follows: Although it passed the validation and no error message, OutTimeHour and InTimeHour can't insert in?
Outime: 2017-05-27 00:22:00.000 when I selected 18:22
Intime: 2017-05-27 00:59:00.000 when I selected 19:59
sorry if you copied my code exactly. there is a typo
OutTImeHour should be OutTimeHour
same for InTImeHour
@Snapey I tried to make it work. Unfortunately, I got error message after modifying. Below is the error message and the code. Please advise.
FatalErrorException in UpdateLogController.php line 60: syntax error, unexpected '=>' (T_DOUBLE_ARROW)
59 $dt = Carbon::today(),
60 $dt->addHours('OutTimeHour' => $input['OutTimeHour']),
$dt->addMinutes('OutTimeMinute' => $input['OutTimeMinute']),
'OutTime' => $dt->$input['OutTime'],
$dt = Carbon::today();
$dt->addHours('InTimeHour' => $input['InTimeHour']),
$dt->addMinutes('InTimeMinute' => $input['InTimeMinute']),
'OutTime' => $dt->$input['inTime'],
Not sure why you are trying to use double arrow there.
So, lets recap the process.
We create a carbon object that represents a date with 00:00 as the time. Then we add a number of hours or add a number of minutes.
The value being added is just a simple integer.
if your form has multiple rows then, working with a row at a time, you need to get the hours or the minutes from that row.
Accessing the hours integer is no different to any of your other fields
So, if you foreach($inputs as $input) and for instance 'RACE' => $input['RACE'], then;
$dt = Carbon::today(),
$dt->addHours($input['OutTimeHour']), //add the hours in this row
$dt->addMinutes($input['OutTimeMinute']), // add the minutes in this row
'OutTime' => $dt; // persist the date time object to the database
I've never used insert in that way so I don't know what it is expecting.
Personally, i would just create each record per loop
//$Timetracks[] = new Timetracks(array(
Timetracks::create(array(
and then drop the insert statement
Thanks Snapey. I will give it a try later and let u know. In this case, I still need to use query in app.blade.php or not? Thanks.
Personally, i would just create each record per loop
Which is what I demoed and suggested quite a while ago.
Please or to participate in this conversation.