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?

4
  • 1
    check that query run successfully and show us if you getting any error Commented Apr 1, 2013 at 4:41
  • Try "UPDATE projects SET favorite = '".$fav_update."' WHERE id = '".$fav_id."'"; Commented Apr 1, 2013 at 4:41
  • also you are using obsolesce mysql_* api its deprecated and will cause E_DEPRECATED error in php >=5.5 check stackoverflow.com/questions/12859942/… Commented Apr 1, 2013 at 4: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 Commented Apr 1, 2013 at 4:55

4 Answers 4

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); 
?>
Sign up to request clarification or add additional context in comments.

Comments

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); 

3 Comments

on the alert(output) its echoing the query and the query is right and works on PHPMyAdmin
@raul5660 r us sure you have made the connection properly with database?
yes because i've been using the same connection file for multiple scripts before this, thats why i'm stumped
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

3 Comments

i'm getting the right query but database still isnt being updated?
please cehck your main.php once, or post that file over here
i've checked it but the funny thing is that it works cause i actually use that connection file frequently thru out the site
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.