11 Replies - 862 Views - Last Post: 22 July 2008 - 04:44 PM Rate Topic: -----

#1 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Manipulating/editing values in database?

Post icon  Posted 21 July 2008 - 05:56 PM

I have a test variable called stylepoints. On account login, it shows style points: 0

Style Points: <? echo $_SESSION['stylepoints']; ?>


this is working correctly

How do you increment/decrement/edit values in a database? I'm still learning, but I want to create a test button that will increase +1 and save it, then log out and in to test it!


Thanks very much!

Is This A Good Question/Topic? 0
  • +

Replies To: Manipulating/editing values in database?

#2 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Manipulating/editing values in database?

Posted 21 July 2008 - 06:34 PM

[rules][/rules]

Here is a decent article on working with database data with PHP :)
Was This Post Helpful? 0
  • +
  • -

#3 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Manipulating/editing values in database?

Posted 21 July 2008 - 06:51 PM

Your first comment wasnt helpful at all, don't post stuff like that just to make me look bad.

My code is

button:
<input class="nicebutton3" type="submit" name="Increment value="">


increment code
if (isset($_POST['Increment'])) {

UPDATE stylepoints SET stylepoints=stylepoints=stylepoints+1;

}


I know the stylepoints variable is working, because it says "0" when I log in. I also gave enough information for someone to help me. All you need to know is the variable name.

Thanks for the help and I'll check out your link
Was This Post Helpful? 0
  • +
  • -

#4 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Manipulating/editing values in database?

Posted 21 July 2008 - 06:53 PM

Wasn't trying to make you look bad, was simply stating the rules of the forum :)
Was This Post Helpful? 0
  • +
  • -

#5 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Manipulating/editing values in database?

Posted 21 July 2008 - 07:03 PM

Hey it's no problem.

Now that I cleaned up my post, could you help me?


It's a bit ironic how the guide you linked me to is having php and mysql errors, and isn't displaying page 2!

:blink: :blink:
Was This Post Helpful? 0
  • +
  • -

#6 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Manipulating/editing values in database?

Posted 21 July 2008 - 07:34 PM

An update to tell you / and show you - i'm trying...

I have 2 rows in my database, User and Administrator. I have a variable stylepoints:

I'm trying to get stylepoints to increment on a button press.


User and Administrator.

UPDATE bloom_itsla SET stylepoints = stylepoints+1 WHERE user = 'User';



Do i have to echo it immediately to get it to update on screen without refreshing?

<? echo $_SESSION['stylepoints']; ?>

Was This Post Helpful? 0
  • +
  • -

#7 AdaHacker  Icon User is offline

  • Resident Curmudgeon

Reputation: 452
  • View blog
  • Posts: 811
  • Joined: 17-June 08

Re: Manipulating/editing values in database?

Posted 21 July 2008 - 07:34 PM

I'm a little confused as to what you're trying to do. The "code" you posted is just an SQL query inside a PHP if block. There's no hint of any code to connect to an actual database. Are we to assume you just omitted that?

As for your actual query (since this is the database forum), it isn't going to work the way you want. For starters, it's updating every row in the stylepoints table, which probably isn't what you want, unless the table only has one row. Second, you have stylepoints=stylepoints=stylepoints+1, which actually doesn't increment stylepoints. Did you mean just stylepoints=stylepoints+1? What you posted actually sets stylepoints ot 0 (at least in MySQL) because it does an equality comparison between stylepoints to stylepoints+1, which is always false, and casts the value to an integer zero.
Was This Post Helpful? 0
  • +
  • -

#8 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Manipulating/editing values in database?

Posted 21 July 2008 - 07:52 PM

I'm omitting code, and that was a typo. Thanks for spotting that, Its correct in my code tho.


Basically, I go to my website. I type in username and password.
Page refreshes. It says Hello admin! Rank: Administrator , Style Points = 0
Style Points = 0 shows up, so that must be working.


? if ( $_SESSION['level'] == "4" ) { ?>
		<td bgcolor="#FFFFFF" valign="top">
		  <div style="margin: 5px 0px 0px 5px">
			<p>		  Welcome<font color="#FF0000"><? echo $_SESSION['username']; ?></font>!<br>
		  User Rank: <font color="#FF0000">Administrator</font>!<br />
		  Style Points: <? echo $_SESSION['stylepoints']; ?><br />
		  <input class="nicebutton2" type="submit" name="Logout" value="">



ALL of this is working - including the Logout.

Under the nicebutton2 line, I created the exact same line, but named it nicebutton3 with name="Increment"


then I used my if statement I showed you to make it increment when I click the button. The button shows up just fine, but I cant even load my page because I have a code error in the if statement.

I only want to increment admin's stylepoints variable by 1, no one elses.

I hope I gave you all you need to know. variable stylepoints for user "admin" who is under the rank Administrator ( which shouldnt matter, I think)

This post has been edited by Sonastylol: 21 July 2008 - 07:52 PM

Was This Post Helpful? 0
  • +
  • -

#9 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Manipulating/editing values in database?

Posted 21 July 2008 - 08:59 PM

This is for the guys at gamefaqs.com , and you guys too, thanks

<?php

session_start();
include("config.php");
include ('header.php');
include ('nav.php');
include ('banner.php');
include ('side.php');
include ('index-content.php');
include ('include-challenge.php');
include ('loginheader.php');

$msg = "";

if (isset($_POST['Submit']))
{
	
	$username = $_POST['username'];
	$password = md5($_POST[password]);
	
	$result = mysql_query("Select * From login_table where user_name='$username'",$con);
	
	if(mysql_num_rows($result)>0)
	{
		$row = mysql_fetch_array($result, MYSQL_BOTH);
		if($password == $row["user_pass"])
		{
			
			$_SESSION['loginok'] = "ok";
			$_SESSION['username'] = $row["user_name"];
			$_SESSION['password'] = "password";
			$_SESSION['level'] = $row["user_level"];
			$_SESSION['userid'] = $row["userid"];
			$_SESSION['avatar'] = $row["avatar"];
			$_SESSION['stylepoints'] = $row["stylepoints"];

		}
		else
		{
			$msg = "Password incorrect";
		}
	}
	else
	{
		$msg = "Username incorrect";
	}

}

if (isset($_POST['Logout'])) {

$_SESSION['level'] = "";
echo "<meta http-equiv=\"refresh\" >";

}

if (isset($_POST['Increment'])) {
UPDATE bloom_itsla SET stylepoints = stylepoints + 1 WHERE username = 'Administrator'
}


?>

<? if ( $_SESSION['level'] == "" ) { ?>
		<td bgcolor="#FFFFFF" valign="top"><form name="form1" method="post" action="">
		  <div style="margin: 5px 0px 0px 5px">
		  <table><tr>
			<td width="62">
			<font style="font-family: Tahoma; font-size: 11px; font-weight: bold; color:#5f8295;">
			
			<strong>Email</strong>: </font></td>
			<td width="93">
			<input type="text" style="border: 1px solid #0099CC; font-size:9px" value="" name="username" id="username" size="13">			</td>
			</tr>
			<tr>
			  <td>
			<font style="font-family: Tahoma; font-size: 11px; font-weight: bold; color:#5f8295;">
			
			<strong>Password</strong>:</font></td>
			  <td>
			<input type="password" style="border: 1px solid #0099CC; font-size:9px" value="" id="password" name="password" size="13">			</td>
		</tr></table><table><tr>
		  <td width="109"><font class="navstyle2">></font> <a href="forgotpw.php" class="forgotpw">Forgot Password</a></td>
		  <td align="right" width="46">
 <input class="nicebutton" type="submit" name="Submit" value="	 ">
		</td></tr>
		</table>
		<table><tr><td align="center" class="error"><? echo $msg; ?></td></tr></table>
		</div>
			</form></td>
			<? } ?>
<? if ( $_SESSION['level'] == "4" ) { ?>
		<td bgcolor="#FFFFFF" valign="top">
		  <div style="margin: 5px 0px 0px 5px">
			<p>		  Welcome<font color="#FF0000"><? echo $_SESSION['username']; ?></font>!<br>
		  User Rank: <font color="#FF0000">Administrator</font>!<br />
		  Style Points: <? echo $_SESSION['stylepoints']; ?><br />
		  <input class="nicebutton2" type="submit" name="Logout" value="">
			<input class="nicebutton3" type="submit" name="Increment" value="">	  
			</p>
</div></td>
<? } ?>	
<? if ( $_SESSION['level'] == "1" ) { ?>
		<td bgcolor="#FFFFFF" valign="top">
		  <div style="margin: 5px 0px 0px 5px">
		  Welcome <font color="#FF0000"><? echo $_SESSION['username']; ?></font>!<br>
		  User Rank: <font color="#FF0000">User</font>
		  Links to pages for users go here.
		</div></td>
<? } ?>			

<?
include ('bottom-nav.php');
include ('footer.php');
?>

Was This Post Helpful? 0
  • +
  • -

#10 Sonastylol  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 151
  • Joined: 15-December 07

Re: Manipulating/editing values in database?

Posted 21 July 2008 - 10:00 PM

Its working now!


Forgot 2 things, the <form> </form> code to set this up and the table was changed from bloom_itsla to login_table, and my friend didn't inform me.
Was This Post Helpful? 0
  • +
  • -

#11 Trogdor  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 627
  • Joined: 06-October 06

Re: Manipulating/editing values in database?

Posted 22 July 2008 - 02:24 PM

You are putting the username directly from the POST into the sql statement. That is VERY BAD.
Google on sql-injection to learn why.
Was This Post Helpful? 0
  • +
  • -

#12 mocker  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 466
  • Joined: 14-October 07

Re: Manipulating/editing values in database?

Posted 22 July 2008 - 04:44 PM

Use the php function mysql_real_escape_string to make sure there isn't anything nasty sent to you in the username field
http://us2.php.net/m...cape-string.php

$username = mysql_real_escape_string($_POST['username']);
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1