/    Sign up×
Community /Pin to ProfileBookmark

Updating rows in a mysql table

Hey I im trying to udpate the rows PassWord, email, Age in my membersys table of my mysql database. With the code im using its only saving the age and nothing else.

Also the the new info is coming from a form using the POST method

[code=php]<?php
session_start();

$con = mysql_connect(“localhost”,”MyUser”,”MySecretPass”);
if (!$con)
{
die(‘Could not connect: ‘ . mysql_error());
}

mysql_select_db(“membersys”, $con);

$ui = $_POST[‘username’];
$pi = $_POST[‘password’];
$ei = $_POST[’email’];
$ag = $_POST[‘age’];
$user = $_SESSION[‘UserName’];

mysql_query(“UPDATE Member SET PassWord=$ui WHERE UserName=’$user'”);
mysql_query(“UPDATE Member SET email=$ei WHERE UserName=’$user'”);
mysql_query(“UPDATE Member SET Age=$ag WHERE UserName=’$user'”);

Header(“Location: acc_content.php?account=updated”);

mysql_close($con);
?>[/code]

Any Ideas?

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@NogDogJan 19.2013 — Check the return value from your calls to mysql_query() to see if it is false, and if so output/log some debug info (such as via mysql_error()). I suspect the problem is that you are not quoting the non-numeric values for email and password.

Note that there is no need for 3 separate queries: all 3 fields can be set in a single update query.

Lastly, your script is susceptible to SQL injection since you are not escaping the strings being inserted as values or used in the where clause, nor ensuring that numeric values are actually numeric (see mysql_real_escape_string()).

Even more lastly, the MySQL extension has been deprecated in favor of either the MySQL[b]i[/b] or PDO extension (both of which allow you to use bound parameters in your queries, getting rid of the need to worry about escaping text parameters.
Copy linkTweet thisAlerts:
@evenstar7139Jan 20.2013 — What happens when you replace this:

[code=php]mysql_query("UPDATE Member SET PassWord=$ui WHERE UserName='$user'");
mysql_query("UPDATE Member SET email=$ei WHERE UserName='$user'");
mysql_query("UPDATE Member SET Age=$ag WHERE UserName='$user'"); [/code]


With this?:
[code=php]mysql_query("UPDATE Member SET PassWord = '".$ui."', email = '".$ei."', Age = '".$ag."' WHERE UserName = '".$user."'");[/code]

  • - -


  • Also, you might want to consider going all lower case with your table and column names. Then you don't have to remember where the uppercase letters were.

    I also recommend more descriptive variable names (e.g. $email, not $ei)
    ×

    Success!

    Help @anthonyw17 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.29,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...