2
I’m a beginner in php. Does anyone know why it’s not working? It does the correct update of integer values, but if it is a string of letters it does not work
html It will type here a url to do the update
<input type="text" id="url_video">
salva php.
$con = mysqli_connect("localhost","root","","database_name");
$resultado = mysqli_query($con,"Select * from video");
echo "<table class='table'>".
"<thead>".
"<tr>".
"<td>ID</td>".
"<td>url_video</td>".
"</tr>".
"</thead><tbody>";
while($valor = mysqli_fetch_array($resultado)){
echo "<tr>".
"<td>".$valor['ID']."</td>".
"<td>".$valor['url_video']."</td>".
"<td><input type='button' value='Update' class='btn-update' data-id='".$valor['ID']."'>"."</td>".
"</tr>";
}
echo "</tbody></table>";
ajax I send the typed string (url) and pass the id of the selected column.
$(document).ready(function(){
$('.btn-update').click(function(){
var id = $(this).data('id');
var string = $('#url_video').val();
$.ajax({
url: "tabelaUpdate.php",
data: { 'idDeUpdate' : id,
'url_video' : string
},
type: "POST",
cache: false,
success: function(response){
$('#result').html(response);
}
})
});
});
php tableUpdate.php
$con = mysqli_connect("localhost","root", "", "database_name");
$id = $_POST['idDeUpdate'];
$string_url = $_POST['url_video'];
mysqli_query($con,"Update video SET url_video=$string_url WHERE ID=$id");
echo "ID -> ".$id." URL -> ".$string_url;
Just out of curiosity, why are you using suppression (@) on
mysql_connect
?– Bacco
if I don’t, this message-> Deprecated: mysql_connect(): The mysql Extension is deprecated and will be Removed in the Future: use mysqli or PDO Instead
– Jose Maximilian
And isn’t it better to fix it than to hide the alert? Tip: Take advantage of the fact that you’re building the application and replace it with mysqli, instead of having to redo everything when you remove this function from PHP. (including why today you are running a risk of Injection in this AJAX, by the way it does update).
– Bacco
I made some changes to the code using mysqli.
– Jose Maximilian
It is good that you have put the salva.php as well. It can help someone to find the problem. This seems problematic to me:
var string = $('#url_video').val();
, will always get the first URL, no?– Bacco
First of all, connect in the bank (serves a client type Heidisql) and check if the column
url_video
is asvarchar
. If not, switch tovarchar
that you must solve. If you post the schematic of the table that we replicate more accurately. D– Fernando Cordeiro
could resolve! mysqli_query($con,"Update video SET url_video='$string_url' WHERE ID=$id"); missing single quotes
– Jose Maximilian
@Josemaximilian vc tested with more than one value to make sure the 2nd video tb works?
– Bacco
it worked, I just used a text box to test it. I mean, it type the url and then will press one of the update buttons. further I put an input text inside the loop to do the update.
– Jose Maximilian
That’s right, take the test with more than one, 'cause I think it’s gonna make a difference between
$('#url_video').val();
and the$(this).data('urlvideo');
that I put in the answer. If something is missing, let us know, because I did the answer without having to test here for your specific case. I may have made some syntax error.– Bacco
Could you give me some tips? I had printed a text type input inside the while loop:
"<td><input type='text' value='".$valor['url_video']."' style='width: 400px;' id='url-video'></td>".
, within that input is printed tourl do vídeo
, by clicking on the btn update, I have it update the selected input fieldvar string = $('#url-video').val();
the problem is that it only updates the first columnurl_video
, while the others is only made a copy of this first to other fields.– Jose Maximilian