【问题标题】:Ajax database insertAjax 数据库插入
【发布时间】:2016-03-17 23:40:28
【问题描述】:

您好已经启动了一个需要通过 ajax 更新 user_points 行的新程序 我的代码是

<?php
session_start();
include_once 'dbconnect.php';

if(!isset($_SESSION['user']))
{
	header("Location: index.php");
}

$res=mysql_query("SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysql_fetch_array($res);
// Retrieve data from database 
$user_id = mysql_real_escape_string($_SESSION['user']);
$sql = mysql_query("UPDATE `users` SET user_points = user_points +'$user_points' WHERE user_id = " . $_SESSION['user']);
// close MySQL connection 
mysql_close();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta http-equiv="refresh" content="0;URL= http://mywebsite/login-registration/home.php">
<link rel="stylesheet" href="style.css" type="text/css" />

</head>
<body bgcolor="#ffffff">


</body>
</html>

如何设置正确的查询以将数据发送到数据库我想通过 ajax 更新 user_points 从其他应用程序发送的信息这个应用程序通过 ajax 将 user_points 发送到 php 和 php 去更新用户积分

【问题讨论】:

  • 在错误日志上它来了
  • 我一直在查找错误日志文件,发现错误
  • [17-Mar-2016 14:25:22 Europe/Lisbon] PHP 警告:mysql_fetch_array() 期望参数 1 是资源,在 /home/publiadd/public_html/mywebsite/login- 中给出布尔值第 20 行的registration/home.php

标签: ajax


【解决方案1】:

<?php
session_start();
include_once 'dbconnect.php';

if(!isset($_POST['user']))
{
    header("Location: index.php");
    die();
}

$user_id = mysql_real_escape_string($_POST['user']);
$res=mysql_query("SELECT * FROM users WHERE user_id=$user_id");
$userRow=mysql_fetch_array($res);
// Retrieve data from database 

$sql = mysql_query("UPDATE `users` SET user_points = user_points +1 WHERE user_id = $user_id");
// close MySQL connection 
mysql_close();
?>

$user_ponts 未在您的代码中定义。此外,您应该在 SQL 语句中使用转义的 $user_id。此外,您应该在标头重定向之后使用 die 以防止执行其余代码。 此外 $_SESSION 可能不是您想要用于 ajax 调用的。您可以改用 $_POST。

【讨论】:

  • 感谢您重播了我需要通过 ajax 从应用程序发送到数据库 user_points 的内容
  • 数据库是用户,我有 3 行 1=user_id 2=user_name 3= user_points
  • 好吧,我已经更改了代码,现在它给了我那个错误 [17-Mar-2016 14:56:09 Europe/Lisbon] PHP 警告:mysql_fetch_array() 期望参数 1 是资源,布尔值在第 20 行的 /home/publiadd/public_html/mywebsite/login-registration/home.php 中给出
  • 您可以发送一个带有参数 user_id 和用户点数的 ajax 请求,然后像 id 一样从 $_Post 获取点数。您必须使用的 ajax 调用取决于您在应用中使用的语言
  • 我的应用程序使用 html5,我通过“bitcoinrotator.publiadds.org.pt/login-registration/…”调用
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-07
  • 1970-01-01
  • 2014-01-13
  • 2015-11-06
  • 2023-03-08
  • 2013-08-26
  • 1970-01-01
相关资源
最近更新 更多