PHP - ajax request to UPDATE mysql database [duplicate]











up vote
-1
down vote

favorite













This question already has an answer here:




  • What is the difference between client-side and server-side programming?

    5 answers




I need help please.



i have 2 files:




  1. ajax.php

  2. shows.php


and i have table in my data base that call shows.
in shows table i have column - img_credit.
i try to UPDATE this field.



i insert value to input field and when i click on update button i want the query run and UPDATE my database.



i try:



$(document).ready(function(){
$(".update_credit").click(function(){
<?php
$val_update = $show['img_credits'];

$link->query("UPDATE `shows` SET `img_credits` = '$val_update ' WHERE `id` = show['id'] ");
?>


if i do this i get error but if i change to:



$link->query("UPDATE `shows` SET `img_credits` = 'test' WHERE `id` = 421 ");


it's work but it not help me.



i check and the value shows['img_credit'] is not empty.



thank's










share|improve this question















marked as duplicate by Akintunde-Rotimi, RiggsFolly mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 17:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • What is the error that you are getting? and where are you setting show['id'] ?
    – dsadnick
    Nov 8 at 16:59

















up vote
-1
down vote

favorite













This question already has an answer here:




  • What is the difference between client-side and server-side programming?

    5 answers




I need help please.



i have 2 files:




  1. ajax.php

  2. shows.php


and i have table in my data base that call shows.
in shows table i have column - img_credit.
i try to UPDATE this field.



i insert value to input field and when i click on update button i want the query run and UPDATE my database.



i try:



$(document).ready(function(){
$(".update_credit").click(function(){
<?php
$val_update = $show['img_credits'];

$link->query("UPDATE `shows` SET `img_credits` = '$val_update ' WHERE `id` = show['id'] ");
?>


if i do this i get error but if i change to:



$link->query("UPDATE `shows` SET `img_credits` = 'test' WHERE `id` = 421 ");


it's work but it not help me.



i check and the value shows['img_credit'] is not empty.



thank's










share|improve this question















marked as duplicate by Akintunde-Rotimi, RiggsFolly mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 17:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.















  • What is the error that you are getting? and where are you setting show['id'] ?
    – dsadnick
    Nov 8 at 16:59















up vote
-1
down vote

favorite









up vote
-1
down vote

favorite












This question already has an answer here:




  • What is the difference between client-side and server-side programming?

    5 answers




I need help please.



i have 2 files:




  1. ajax.php

  2. shows.php


and i have table in my data base that call shows.
in shows table i have column - img_credit.
i try to UPDATE this field.



i insert value to input field and when i click on update button i want the query run and UPDATE my database.



i try:



$(document).ready(function(){
$(".update_credit").click(function(){
<?php
$val_update = $show['img_credits'];

$link->query("UPDATE `shows` SET `img_credits` = '$val_update ' WHERE `id` = show['id'] ");
?>


if i do this i get error but if i change to:



$link->query("UPDATE `shows` SET `img_credits` = 'test' WHERE `id` = 421 ");


it's work but it not help me.



i check and the value shows['img_credit'] is not empty.



thank's










share|improve this question
















This question already has an answer here:




  • What is the difference between client-side and server-side programming?

    5 answers




I need help please.



i have 2 files:




  1. ajax.php

  2. shows.php


and i have table in my data base that call shows.
in shows table i have column - img_credit.
i try to UPDATE this field.



i insert value to input field and when i click on update button i want the query run and UPDATE my database.



i try:



$(document).ready(function(){
$(".update_credit").click(function(){
<?php
$val_update = $show['img_credits'];

$link->query("UPDATE `shows` SET `img_credits` = '$val_update ' WHERE `id` = show['id'] ");
?>


if i do this i get error but if i change to:



$link->query("UPDATE `shows` SET `img_credits` = 'test' WHERE `id` = 421 ");


it's work but it not help me.



i check and the value shows['img_credit'] is not empty.



thank's





This question already has an answer here:




  • What is the difference between client-side and server-side programming?

    5 answers








php mysql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 17:49









RiggsFolly

68.6k1764109




68.6k1764109










asked Nov 8 at 16:52









liran

113




113




marked as duplicate by Akintunde-Rotimi, RiggsFolly mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 17:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.






marked as duplicate by Akintunde-Rotimi, RiggsFolly mysql
Users with the  mysql badge can single-handedly close mysql questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 8 at 17:49


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • What is the error that you are getting? and where are you setting show['id'] ?
    – dsadnick
    Nov 8 at 16:59




















  • What is the error that you are getting? and where are you setting show['id'] ?
    – dsadnick
    Nov 8 at 16:59


















What is the error that you are getting? and where are you setting show['id'] ?
– dsadnick
Nov 8 at 16:59






What is the error that you are getting? and where are you setting show['id'] ?
– dsadnick
Nov 8 at 16:59














1 Answer
1






active

oldest

votes

















up vote
0
down vote













From the code you've provided, it seems that you're mixing up the client side logic (Javascript/jQuery) with the server side logic (PHP).



When you have mixed up client side and server side logic in the same file, the server side code (PHP) will execute first and only after the client side (Javascript). Also in the above code, it doesn't matter if you include PHP code in a Javascript function, the PHP code will execute regardless of where is located in a Javascript structure. That's why is a good practice to avoid mixing them up in the same file.



I would recommend using a basic API structure like this:



let body = "foo"; //param to be sent to server side
let request = $.ajax({
type: "POST",
url: "ajax.php",
data: body
});
request.done(function (response) {
console.log(response);
});
request.fail(function (jqXHR, data) {
console.log("API error", data, jqXHR);
});


And then in your ajax.php file you would have the backend logic to do database operations like this:



<?php 
$val_update = $show['img_credits'];

$link->query("UPDATE `shows`
SET `img_credits` = '$val_update'
WHERE `id` = ".$show['id']);


Don't forget to use proper means of protecting against SQL injection, like prepared statements etc.






share|improve this answer




























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    From the code you've provided, it seems that you're mixing up the client side logic (Javascript/jQuery) with the server side logic (PHP).



    When you have mixed up client side and server side logic in the same file, the server side code (PHP) will execute first and only after the client side (Javascript). Also in the above code, it doesn't matter if you include PHP code in a Javascript function, the PHP code will execute regardless of where is located in a Javascript structure. That's why is a good practice to avoid mixing them up in the same file.



    I would recommend using a basic API structure like this:



    let body = "foo"; //param to be sent to server side
    let request = $.ajax({
    type: "POST",
    url: "ajax.php",
    data: body
    });
    request.done(function (response) {
    console.log(response);
    });
    request.fail(function (jqXHR, data) {
    console.log("API error", data, jqXHR);
    });


    And then in your ajax.php file you would have the backend logic to do database operations like this:



    <?php 
    $val_update = $show['img_credits'];

    $link->query("UPDATE `shows`
    SET `img_credits` = '$val_update'
    WHERE `id` = ".$show['id']);


    Don't forget to use proper means of protecting against SQL injection, like prepared statements etc.






    share|improve this answer

























      up vote
      0
      down vote













      From the code you've provided, it seems that you're mixing up the client side logic (Javascript/jQuery) with the server side logic (PHP).



      When you have mixed up client side and server side logic in the same file, the server side code (PHP) will execute first and only after the client side (Javascript). Also in the above code, it doesn't matter if you include PHP code in a Javascript function, the PHP code will execute regardless of where is located in a Javascript structure. That's why is a good practice to avoid mixing them up in the same file.



      I would recommend using a basic API structure like this:



      let body = "foo"; //param to be sent to server side
      let request = $.ajax({
      type: "POST",
      url: "ajax.php",
      data: body
      });
      request.done(function (response) {
      console.log(response);
      });
      request.fail(function (jqXHR, data) {
      console.log("API error", data, jqXHR);
      });


      And then in your ajax.php file you would have the backend logic to do database operations like this:



      <?php 
      $val_update = $show['img_credits'];

      $link->query("UPDATE `shows`
      SET `img_credits` = '$val_update'
      WHERE `id` = ".$show['id']);


      Don't forget to use proper means of protecting against SQL injection, like prepared statements etc.






      share|improve this answer























        up vote
        0
        down vote










        up vote
        0
        down vote









        From the code you've provided, it seems that you're mixing up the client side logic (Javascript/jQuery) with the server side logic (PHP).



        When you have mixed up client side and server side logic in the same file, the server side code (PHP) will execute first and only after the client side (Javascript). Also in the above code, it doesn't matter if you include PHP code in a Javascript function, the PHP code will execute regardless of where is located in a Javascript structure. That's why is a good practice to avoid mixing them up in the same file.



        I would recommend using a basic API structure like this:



        let body = "foo"; //param to be sent to server side
        let request = $.ajax({
        type: "POST",
        url: "ajax.php",
        data: body
        });
        request.done(function (response) {
        console.log(response);
        });
        request.fail(function (jqXHR, data) {
        console.log("API error", data, jqXHR);
        });


        And then in your ajax.php file you would have the backend logic to do database operations like this:



        <?php 
        $val_update = $show['img_credits'];

        $link->query("UPDATE `shows`
        SET `img_credits` = '$val_update'
        WHERE `id` = ".$show['id']);


        Don't forget to use proper means of protecting against SQL injection, like prepared statements etc.






        share|improve this answer












        From the code you've provided, it seems that you're mixing up the client side logic (Javascript/jQuery) with the server side logic (PHP).



        When you have mixed up client side and server side logic in the same file, the server side code (PHP) will execute first and only after the client side (Javascript). Also in the above code, it doesn't matter if you include PHP code in a Javascript function, the PHP code will execute regardless of where is located in a Javascript structure. That's why is a good practice to avoid mixing them up in the same file.



        I would recommend using a basic API structure like this:



        let body = "foo"; //param to be sent to server side
        let request = $.ajax({
        type: "POST",
        url: "ajax.php",
        data: body
        });
        request.done(function (response) {
        console.log(response);
        });
        request.fail(function (jqXHR, data) {
        console.log("API error", data, jqXHR);
        });


        And then in your ajax.php file you would have the backend logic to do database operations like this:



        <?php 
        $val_update = $show['img_credits'];

        $link->query("UPDATE `shows`
        SET `img_credits` = '$val_update'
        WHERE `id` = ".$show['id']);


        Don't forget to use proper means of protecting against SQL injection, like prepared statements etc.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 17:23









        Dan D.

        1697




        1697















            Popular posts from this blog

            How to pass form data using jquery Ajax to insert data in database?

            National Museum of Racing and Hall of Fame

            Guess what letter conforming each word