Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,547 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,180 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Manipulating/editing values in database?

 
Reply to this topicStart new topic

Manipulating/editing values in database?, please assist

Sonastylol
post 21 Jul, 2008 - 05:56 PM
Post #1


D.I.C Head

**
Joined: 15 Dec, 2007
Posts: 69


My Contributions


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!
User is offlineProfile CardPM

Go to the top of the page


PsychoCoder
post 21 Jul, 2008 - 06:34 PM
Post #2


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,248



Thanked 52 times

Dream Kudos: 7775

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI, Boo.Net

My Contributions


Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Please post like this:

Thank you for helping us helping you.

Here is a decent article on working with database data with PHP smile.gif
User is online!Profile CardPM

Go to the top of the page

Sonastylol
post 21 Jul, 2008 - 06:51 PM
Post #3


D.I.C Head

**
Joined: 15 Dec, 2007
Posts: 69


My Contributions


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

My code is

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


increment code
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
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 21 Jul, 2008 - 06:53 PM
Post #4


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,248



Thanked 52 times

Dream Kudos: 7775

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI, Boo.Net

My Contributions


Wasn't trying to make you look bad, was simply stating the rules of the forum smile.gif
User is online!Profile CardPM

Go to the top of the page

Sonastylol
post 21 Jul, 2008 - 07:03 PM
Post #5


D.I.C Head

**
Joined: 15 Dec, 2007
Posts: 69


My Contributions


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.gif blink.gif
User is offlineProfile CardPM

Go to the top of the page

Sonastylol
post 21 Jul, 2008 - 07:34 PM
Post #6


D.I.C Head

**
Joined: 15 Dec, 2007
Posts: 69


My Contributions


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.

CODE
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?

CODE
<? echo $_SESSION['stylepoints']; ?>
User is offlineProfile CardPM

Go to the top of the page

AdaHacker
post 21 Jul, 2008 - 07:34 PM
Post #7


D.I.C Head

**
Joined: 17 Jun, 2008
Posts: 76



Thanked 10 times
My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Sonastylol
post 21 Jul, 2008 - 07:52 PM
Post #8


D.I.C Head

**
Joined: 15 Dec, 2007
Posts: 69


My Contributions


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.

CODE


? 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 Jul, 2008 - 07:52 PM
User is offlineProfile CardPM

Go to the top of the page

Sonastylol
post 21 Jul, 2008 - 08:59 PM
Post #9


D.I.C Head

**
Joined: 15 Dec, 2007
Posts: 69


My Contributions


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

CODE
<?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');
?>
User is offlineProfile CardPM

Go to the top of the page

Sonastylol
post 21 Jul, 2008 - 10:00 PM
Post #10


D.I.C Head

**
Joined: 15 Dec, 2007
Posts: 69


My Contributions


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.
User is offlineProfile CardPM

Go to the top of the page

Trogdor
post 22 Jul, 2008 - 02:24 PM
Post #11


D.I.C Regular

Group Icon
Joined: 6 Oct, 2006
Posts: 451



Thanked 2 times

Dream Kudos: 125
My Contributions


You are putting the username directly from the POST into the sql statement. That is VERY BAD.
Google on sql-injection to learn why.
User is offlineProfile CardPM

Go to the top of the page

mocker
post 22 Jul, 2008 - 04:44 PM
Post #12


D.I.C Head

**
Joined: 14 Oct, 2007
Posts: 177



Thanked 6 times
My Contributions


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/manual/en/function.mysq...cape-string.php

$username = mysql_real_escape_string($_POST['username']);
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/7/08 10:13PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month