I’m making a sort of data sharing system, where admins can create projects, add templates to them (example: Text 1 can only have up to 20 characters) and then assign them to users. The users then can update the projects to their liking (like, changing default texts for example).
In the next screenshot there is a example of how a user can view in project (in Spanish, sorry for that):
Thing is, when the user updates his, let’s say, text 1, both text 1 and 2 are changed at the same time, and this even goes to various other projects that are assigned to the same user.
The way the text-update is done is like this:
<div class="col-md-6 row"> <label for="texto_id" class="col-md-4 col-form-label">Change Text</label> <br> <select id="texto_id" class="form-control @error('texto_id') is-invalid @enderror" name="texto_id" value="{{ old('texto_id') }}" required autocomplete="texto_id" autofocus> <option value="null"> </option> @foreach($proyecto_data as $prt_data) @foreach($texto_data as $txt) @foreach($texto_proyecto_data as $txt_prt) @if($txt_prt->id_texto_data == $txt->id) @if($txt_prt->id_proyecto_data == $prt_data->id) <option value="{{ $txt->id }}">{{ $txt->nombre }} which has min {{ $txt->min }} and max {{ $txt->max }}</option> @endif @endif @endforeach @endforeach @endforeach </select> @error('name') <span class="invalid-feedback" role="alert"> <strong>{{ $message }}</strong> </span> @enderror </div>
An user chooses an text associated to them and writes a new text, here is a screenshot of that:
After that, the variables are sent to a php script that updates the table, and for that I’ve tried the next 2 lines:
$sql = "UPDATE texto_datas SET texto = " . $texto_valor . "WHERE id=" . $id_texto_valor;
and
DB::update('update texto_datas set texto = ?', $texto_array ,'where id = ?', $id_texto_valor);
The 1st one does absolutely nothing, while the second one changes all the texts associated to the user, promptly ignoring the where.
Is there something I’m doing wrong?
Advertisement
Answer
You are not using the query builder correctly. You need to do:
DB::table('texto_datas') ->where('id', $id_texto_valor) ->update(['texto' => $texto_array]);