Foros del Web » Programando para Internet » PHP »

Sistema de favoritos AJAX y PHP

Estas en el tema de Sistema de favoritos AJAX y PHP en el foro de PHP en Foros del Web. Hola buena gente! tengo un problema y me estoy volviendo loco!! Tengo echo un sistema de favoritos mediante Ajax, php y Mysql.. cuando genero una ...
  #1 (permalink)  
Antiguo 24/09/2013, 03:21
Avatar de ken-obi  
Fecha de Ingreso: julio-2004
Ubicación: Alicante
Mensajes: 314
Antigüedad: 19 años, 9 meses
Puntos: 6
Sistema de favoritos AJAX y PHP

Hola buena gente!
tengo un problema y me estoy volviendo loco!!
Tengo echo un sistema de favoritos mediante Ajax, php y Mysql..
cuando genero una cookie por cada usuario de solo números me lo acepta y lo ingresa a la BD pero el problema que tengo es cuando genero una cookie con números y letras no me lo reconoce y no se guarda en la BD.
Os adjunto código:
AJAX

Código Javascript:
Ver original
  1. function addremove(favid){
  2.  
  3.  
  4. // Configure those variables as appropriate
  5.  
  6. var divid = 'status';
  7. var url = 'favscript/addremove.php';
  8.  
  9.  
  10. // The XMLHttpRequest object
  11.  
  12. var xmlHttp;
  13. try{
  14. xmlHttp=new XMLHttpRequest(); // Firefox, Opera 8.0+, Safari
  15. }
  16. catch (e){
  17. try{
  18. xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); // Internet Explorer
  19. }
  20. catch (e){
  21. try{
  22. xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  23. }
  24. catch (e){
  25. alert("Your browser does not support AJAX.");
  26. return false;
  27. }
  28. }
  29. }
  30.  
  31.  
  32. // Generate timestamp for preventing IE caching the GET request
  33.  
  34. fetch_unix_timestamp = function()
  35. {
  36. return parseInt(new Date().getTime().toString().substring(0, 10))
  37. }
  38.  
  39. var timestamp = fetch_unix_timestamp();
  40. var nocacheurl = url+"?t="+timestamp;
  41.  
  42.  
  43. // This code sends the variables through AJAX and gets the response
  44.  
  45. xmlHttp.onreadystatechange=function(){
  46. if(xmlHttp.readyState!=4){
  47. document.getElementById(divid).innerHTML='<img src="images/spinner.gif">  Wait...';
  48. }
  49. if(xmlHttp.readyState==4){
  50. document.getElementById(divid).innerHTML=xmlHttp.responseText;
  51. }
  52. }
  53. xmlHttp.open("GET",nocacheurl+"&favid="+favid,true);
  54. xmlHttp.send(null);
  55.  
  56.  
  57. // Finally, some code for button toggle
  58.  
  59. var button = document.getElementById('button');
  60.  
  61. switch(button.name)
  62. {
  63. case 'button0':
  64.   button.src = 'images/1.jpg';
  65.   button.name = 'button1';
  66.   break;
  67. case 'button1':
  68.   button.src = 'images/0.jpg';
  69.   button.name = 'button0';
  70.   break;
  71. }
  72.  
  73. }

Código PHP:
<?php 

// Include needed files

include 'mysql.php';

// Connect to MySQL

connectMySQL();

//****** SECURITY CHECK *********

session_start();
if(isset(
$_SESSION['userid'])){
$user mysql_real_escape_string($_SESSION['userid']); 

//*******************************

// Retrieves variables through AJAX

$favid '3';
// $favid = mysql_real_escape_string($_GET['favid']);

// Firstly, check if article is favourite or not

$query mysql_query("SELECT * FROM ajaxfavourites WHERE user=$user AND favid=$favid");
$matches mysql_num_rows($query);

// If it is not favourited, add as favourite

if($matches == '0'){
mysql_query("INSERT INTO ajaxfavourites (user, favid) VALUES ('$user', '$favid')");

echo 
"<div class=\"green\">This is a favourite</div>";
}

// Instead, if it is favourited, then remove from favourites

if($matches != '0'){
mysql_query("DELETE FROM ajaxfavourites WHERE user=$user AND favid=$favid");

echo 
"<div class=\"red\">This is NOT a favourite</div>";
}

} else {

// Someone tries to directly access the file!

echo "Invalid session!";

}
?>
gracias de antemano!!!
__________________
Un mundo sin fin... !!! viva los moros y cristianos de ELDA !!!

Última edición por ken-obi; 24/09/2013 a las 05:29
  #2 (permalink)  
Antiguo 24/09/2013, 06:46
Avatar de Triby
Mod on free time
 
Fecha de Ingreso: agosto-2008
Ubicación: $MX->Gto['León'];
Mensajes: 10.106
Antigüedad: 15 años, 8 meses
Puntos: 2237
Respuesta: Sistema de favoritos AJAX y PHP

Obtienes algún mensaje de error?

En tu tabla el campo favid es varchar?
Si es así, entonces en cada consulta debes poner el valor entre comillas simples.
Si el campo es numérico, por eso no se ejecuta correctamente la consulta.
__________________
- León, Guanajuato
- GV-Foto
  #3 (permalink)  
Antiguo 24/09/2013, 07:25
Avatar de ken-obi  
Fecha de Ingreso: julio-2004
Ubicación: Alicante
Mensajes: 314
Antigüedad: 19 años, 9 meses
Puntos: 6
Respuesta: Sistema de favoritos AJAX y PHP

Gracias por la respuesta pero...!!
No me da ningún error mensaje de error.
Y si la tabla de la BD es VARCHAR.
la variable userid es así: scandi_rgdjhyb2ed1yv85v9fry0tuu1l2kqu
y cambiando las comillas me sigue haciendo lo mismo...
__________________
Un mundo sin fin... !!! viva los moros y cristianos de ELDA !!!
  #4 (permalink)  
Antiguo 24/09/2013, 09:29
Avatar de GatorV
$this->role('moderador');
 
Fecha de Ingreso: mayo-2006
Ubicación: /home/ams/
Mensajes: 38.567
Antigüedad: 18 años
Puntos: 2135
Respuesta: Sistema de favoritos AJAX y PHP

En efecto el problema es que le faltan las comillas a tu variable $user dentro de la consulta SQL, ¿como la pusiste?
  #5 (permalink)  
Antiguo 25/09/2013, 02:03
Avatar de ken-obi  
Fecha de Ingreso: julio-2004
Ubicación: Alicante
Mensajes: 314
Antigüedad: 19 años, 9 meses
Puntos: 6
Respuesta: Sistema de favoritos AJAX y PHP

Gracias chicos !!! como siempre!!!!
__________________
Un mundo sin fin... !!! viva los moros y cristianos de ELDA !!!

Etiquetas: ajax, favoritos, mysql, select, sistema, sql, variable
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 12:14.