Jump to content

Adding xp throws error


thereaper87

Recommended Posts

Hello there, first post!  :D

 

I have two pages:

This one is submitxp.php

<?php include("../../tophead.php"); ?>
<form id="addxp" method="post" action="addxp.php">
<input type="submit" value="Add">
</form>

 

Then the process page addxp.php

<?php include("../../tophead.php"); ?>
<?php
include('../database.php'); 
$userid = $_SESSION['user']; 

$result = mysql_query("SELECT xp, level FROM user WHERE id=$userid");   row 7
$row = mysql_num_rows($result); 

while($row = mysql_fetch_assoc($result))       row9
{ 
    $exp = $row['xp']; 
} 
if (surfed){ 
$newexp += 100; 
} 
$inputexp = mysql_query("UPDATE user SET xp='$newexp' WHERE id='$userid'");
?>

 

What this supposed to do, is the person presses the submit button on submitxp.php it will take the logged in user, find their row in the database and add 100 to their total xp amount. It gives at least two errors that say:

mysql_num_rows(): supplied argument is not a valid MySQL result resource in row 7

and

mysql_num_rows(): supplied argument is not a valid MySQL result resource in row 9

Link to comment
Share on other sites

So it would look like this?? Sorry I am very new to PHP. 3rd week actually  :-[

 

<?php
session_start();
include('../database.php'); 
$userid = $_SESSION['user']; 

$result = mysql_query("SELECT xp, level FROM user WHERE id=$userid");
(UPDATE `user` SET `xp`=(`xp`+100) WHERE `id` = $userid")
?>

 

Is that how to do it?

Link to comment
Share on other sites

You should really separate your query string from the query execution, Makes debugging easier . . .

 

<?php
session_start();
include('../database.php'); 
$userid = $_SESSION['user']; 

$query = "UPDATE `user` SET `xp`=(`xp`+100) WHERE `id` = $userid LIMIT 1";
$result = mysql_query($query) or die('Query Failed');

Link to comment
Share on other sites

Those aren't apostrophes, they're backticks, and it won't hurt anything to have them on field and table names. They're sometimes necessary, so I'm in the habit of always using them.

 

Change the query execution to:

$result = mysql_query($query) or die('Query: ' . $query . '<br />Failed with: ' . mysql_error());

and see what the error is.

Link to comment
Share on other sites

It gives this, which I have no idea what it is saying.

Query: UPDATE user SET xp=(xp+100) WHERE 'id' = ksystem LIMIT 1

Failed with: Unknown column 'ksystem' in 'where clause'

 

 

ksystem is the profile i'm logged into. Is that the problem? Since, shouldn't it be a number?

Because my MySql table is set up like this:

 

`id` int(4) unsigned NOT NULL auto_increment,

  `username` varchar(32) NOT NULL,

  `password` varchar(32) NOT NULL,

  `xp` int(11) default '0',

  PRIMARY KEY  (`id`)

 

 

 

 

Link to comment
Share on other sites

Yup. Somehow, somewhere, the $_SESSION['user'] var is being given the wrong value, or the wrong $_SESSION var is being used. And don't use single-quotes on field names as I see you have on 'id'. Like I explained above, they should be in backticks,  which are on the key with the tilde(~) on the top left of (most) keyboards.

Link to comment
Share on other sites

With putting that line instead, it now gave this error when ran:

Query: UPDATE `user` SET `xp`=(`xp`+100) WHERE `id` = Limit 1

Failed with: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Limit 1' at line 1

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.