0

I'm having trouble getting this AJAX code to update my database. The code is a image that onClick will run the command to update the database

HTML:

<a>
    <img class       = "heart" 
         src         = "images/heart.png" 
         onClick     = "favUpdate(0,1)" 
         onMouseover = "this.src='images/heart_mo.png'"
         onMouseout  = "this.src='images/heart.png'"/>
</a>

Javascript code:

function favUpdate(fav_up, id_up) {
        $.ajax({
            type: 'post',
            url: 'includes/fav_update.php',
            data: {favorite: fav_up, id: id_up},
            success: function(output) {
              alert('success, server says '
                            + output
                            + 'Variables passed are '+fav_up+' '+id_up);
                }, 
                    error: function() {
              alert('something went wrong, Favorite update failed');
            }
            });
}

PHP code:

<?php
    require_once('../Connections/main.php');
    $fav_update = mysql_real_escape_string($_POST['favorite']);
    $fav_id     = mysql_real_escape_string($_POST['id']);
    $query      = "UPDATE projects SET favorite = $fav_update WHERE id = $fav_id";
    mysql_query($query, $main); 
?>

main.php

<?php
$hostname_main = "localhost";
$database_main = "test";
$username_main = "root";
$password_main = "";
$main = mysql_pconnect($hostname_main, $username_main, $password_main) or trigger_error(mysql_error(),E_USER_ERROR); 
?>

Does anyone know why it's not updating the database and why the "option" isn't getting data for the variable?

raul5660
  • 11
  • 4
  • 1
    check that query run successfully and show us if you getting any error – NullPoiиteя Apr 01 '13 at 04:41
  • Try `"UPDATE projects SET favorite = '".$fav_update."' WHERE id = '".$fav_id."'";` – Dipesh Parmar Apr 01 '13 at 04:41
  • also you are using obsolesce `mysql_*` api its deprecated and will cause E_DEPRECATED error in php >=5.5 check http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189 – NullPoiиteя Apr 01 '13 at 04:43
  • the server i'm using has php version 5.3.19, so i dont believe thats the issue, i tried to run the query in PHPMyAdmin and worked perfectly fine – raul5660 Apr 01 '13 at 04:55

4 Answers4

1

Try this

<?php
    require_once('../Connections/main.php');
    $fav_update = mysql_real_escape_string($_POST['favorite']);
    $fav_id = mysql_real_escape_string($_POST['id']);
    $query = "UPDATE projects SET favorite = '".$fav_update."' WHERE id = '".$fav_id."'";
    mysql_query($query, $main); 
?>
Kautil
  • 1,321
  • 9
  • 13
0

you have to put single quote around $fav_update if its datatype is string VARCHAR,TEXT

$query = "UPDATE projects SET favorite = '$fav_update' WHERE id = $fav_id";
                                         ^           ^

Remove $main from here and try

mysql_query($query); 
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

Please try to debug your PHP (server side code first) :-

<?php
    require_once('../Connections/main.php');
    $fav_update = mysql_real_escape_string($_REQUEST['favorite']);
    $fav_id = mysql_real_escape_string($_REQUEST['id']);
    $query = "UPDATE projects SET favorite = $fav_update WHERE id = $fav_id";
    echo $query;
     mysql_query($query, $main); 
?>

Use $_REQUEST instead of $_POST, and call this api directly from the browser, by creating its url like http://localhost/filename.php?favorite=somevalue1&id=somevalue2

And check whether you got the insert in DB or not, and check the query by printing it too.

And after checking the API, please change the $_REQUEST, back to $_POST

Nishant
  • 3,614
  • 1
  • 20
  • 26
0

Changed php script to:

<?php
    require_once('../Connections/main.php');
    $fav_update = mysql_real_escape_string($_POST['favorite']);
    $fav_id = mysql_real_escape_string($_POST['id']);
    $updateSQL = sprintf("UPDATE projects 
                          SET favorite=%s  
                          WHERE id=%s",
                          $fav_update,
                          $fav_id);
    mysql_select_db($database_main, $main);
    $Result1 = mysql_query($updateSQL, $main) or die(mysql_error());


?>

hope that helps if anyone runs into the same issue

raul5660
  • 11
  • 4