How do I update a MySQL database with PHP?

Discussion in 'PHP' started by Airwalk Enterprise, Inc, Apr 1, 2011.

  1. #1
    Here's what I have so far. The users data is prefilled into inputs, lets say the user changes the text and presses the submit button. How can I get it to save or 'update' the database with the new data and return to the current page?

    //

    <?php session_start();

    include($_SERVER["DOCUMENT_ROOT"]."/community/herotozero/database.php");

    // input data into database

    $name = $_SESSION['username'];
    $query = "SELECT * from community WHERE name='$name'";
    $result = mysql_query($query);
    $row = mysql_fetch_assoc($result);

    $name = $row['name'];
    $password = $row['password'];
    $email = $row['email'];
    $entity = $row['entity'];
    $city = $row['city'];
    $country = $row['country'];
    $website = $row['website'];
    $facebook = $row['facebook'];
    $myspace = $row['myspace'];
    $twitter = $row['twitter'];
    $deviantart = $row['deviantart'];
    $biography = $row['biography'];
    $news = $row['news'];

    echo "<form id=\"vaform\" method=\"post\" action=\"http://www.airwalk-design.com/community/view-account/\">
    <input type=\"text\" autocomplete=\"on\" class=\"required alphaspace\" minlength=\"5\" maxlength=\"49\" id=\"name\" name=\"name\" value=\"$name\" readonly=\"readonly\"/>
    Name<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"required email\" minlength=\"6\" maxlength=\"64\" id=\"email\" name=\"email\" value=\"$email\"/>
    Email<br/>
    <input type=\"password\" autocomplete=\"on\" class=\"required alpha\" minlength=\"5\" maxlength=\"32\" id=\"passwordtest\" name=\"passwordtest\"/>
    Password<br/>
    <input type=\"password\" autocomplete=\"on\" class=\"required alpha\" minlength=\"5\" maxlength=\"32\" id=\"password\" equalTo=\"#passwordtest\" name=\"password\"/>
    Verify It<br/>
    <input type=\"radio\" value=\"Male\" name=\"entity\" autocomplete=\"on\" checked=\"checked\" id=\"male\"/>
    Male<br/>
    <input type=\"radio\" value=\"Female\" name=\"entity\" autocomplete=\"on\" id=\"female\"/>
    Female<br/>
    <input type=\"radio\" value=\"Company\" name=\"entity\" autocomplete=\"on\" id=\"company\"/>
    A Company<br/>
    <input type=\"radio\" value=\"Project\" name=\"entity\" autocomplete=\"on\" id=\"project\"/>
    A Project<br/>
    <input type=\"radio\" value=\"Website\" name=\"entity\" autocomplete=\"on\" id=\"website\"/>
    A Website<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"required alphaspace\" minlength=\"2\" maxlength=\"58\" id=\"city\" name=\"city\" value=\"$city\"/>
    Village/Town/City/State<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"required alphaspace\" minlength=\"2\" maxlength=\"38\" id=\"country\" name=\"country\" value=\"$country\"/>
    Country<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"15\" maxlength=\"70\" id=\"website\" name=\"website\" value=\"$website\"/>
    Website<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"15\" maxlength=\"58\" id=\"facebook\" name=\"facebook\" value=\"$facebook\"/>
    Facebook Fan Page URL<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"14\" maxlength=\"57\" id=\"myspace\" name=\"myspace\" value=\"$myspace\"/>
    MySpace Profile URL<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"14\" maxlength=\"57\" id=\"twitter\" name=\"twitter\" value=\"$twitter\"/>
    Twitter Profile URL<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"24\" maxlength=\"67\" id=\"deviantart\" name=\"deviantart\" value=\"$deviantart\"/>
    DeviantArt Profile URL<br/>
    <textarea minlength=\"30\" maxlength=\"255\" class=\"required alphaunct\" id=\"biography\" name=\"biography\">$biography</textarea>
    Biography<br/>
    <input type=\"radio\" value=\"Email\" name=\"monthly\" autocomplete=\"on\" id=\"monthly\"/>
    Monthly Newsletter Please<br/>
    <input class=\"submit\" type=\"submit\" value=\"Save Changes\"/>
    </form>";

    ?>
     
  2. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #2
    First thing you need to do is put a name attribute on the submit input

    <input name=\"subit\" class=\"submit\" type=\"submit\" value=\"Save Changes\"/>

    Then you check to see if it's set.

    $submitted = $_REQUEST['subit'];
    if($submitted)
    {
    // processing code goes here
    }

    Inside of that IF statement you grab the values
    $name = $_REQUEST['name'];
    and so on.....

    Then you have to write the query to insert the data (still inside the IF statement). I don't see a User ID listed, if you have that use it over the username. Use a hidden field to add it to the form. Life will be much easier.

    $ins_sql = "INSERT INTO table_name (name,email,...) VALUES ('$name','$email',...) WHERE name='$name' ";
    $ins_res = mysql_query($ins_sql);

    Can check $ins_res for success.
     
    shallowink, Apr 1, 2011 IP
  3. Airwalk Enterprise, Inc

    Airwalk Enterprise, Inc Peon

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    couldn't get this to work :(

    I could have sworn the mySQL update feature should have been used here?
     
  4. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #4
    Yeah you're right. Post what you have so far. Just change the Insert INTO to UPDATE
     
    shallowink, Apr 2, 2011 IP
  5. codeartist

    codeartist Peon

    Messages:
    96
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    you need to store the user id and once you post the form then update into the database using user_id before you use the select statement
     
    codeartist, Apr 2, 2011 IP
  6. codeartist

    codeartist Peon

    Messages:
    96
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Revised script below

    <?php session_start();

    include($_SERVER["DOCUMENT_ROOT"]."/community/herotozero/database.php");

    //Added Script by codeartist

    if(isset($_POST['submit']))
    {
    //write update script

    // then redirect to the same page so that program will forget if submitted
    }

    // input data into database

    $name = $_SESSION['username'];
    $query = "SELECT * from community WHERE name='$name'";
    $result = mysql_query($query);
    $row = mysql_fetch_assoc($result);

    $name = $row['name'];
    $password = $row['password'];
    $email = $row['email'];
    $entity = $row['entity'];
    $city = $row['city'];
    $country = $row['country'];
    $website = $row['website'];
    $facebook = $row['facebook'];
    $myspace = $row['myspace'];
    $twitter = $row['twitter'];
    $deviantart = $row['deviantart'];
    $biography = $row['biography'];
    $news = $row['news'];
    ?>

    <form id="vaform" method="post" action="http://www.airwalk-design.com/community/view-account/">
    <input type="text" autocomplete="on" class="required alphaspace" minlength="5" maxlength="49" id="name" name="name" value="<?=$name?>" readonly="readonly"/>
    Name<br/>
    <input type="text" autocomplete="on" class="required email" minlength="6" maxlength="64" id="email" name="email" value="<?=$email?>"/>
    Email<br/>
    <input type="password" autocomplete="on" class="required alpha" minlength="5" maxlength="32" id="passwordtest" name="passwordtest"/>
    Password<br/>
    <input type="password" autocomplete="on" class="required alpha" minlength="5" maxlength="32" id="password" equalTo="#passwordtest" name="password"/>
    Verify It<br/>
    <input type="radio" value="Male" name="entity" autocomplete="on" checked="checked" id="male"/>
    Male<br/>
    <input type="radio" value="Female" name="entity" autocomplete="on" id="female"/>
    Female<br/>
    <input type="radio" value="Company" name="entity" autocomplete="on" id="company"/>
    A Company<br/>
    <input type="radio" value="Project" name="entity" autocomplete="on" id="project"/>
    A Project<br/>
    <input type="radio" value="Website" name="entity" autocomplete="on" id="website"/>
    A Website<br/>
    <input type="text" autocomplete="on" class="required alphaspace" minlength="2" maxlength="58" id="city" name="city" value="<?=$city?>"/>
    Village/Town/City/State<br/>
    <input type="text" autocomplete="on" class="required alphaspace" minlength="2" maxlength="38" id="country" name="country" value="<?=$country?>"/>
    Country<br/>
    <input type="text" autocomplete="on" class="url" minlength="15" maxlength="70" id="website" name="website" value="<?=$website?>"/>
    Website<br/>
    <input type="text" autocomplete="on" class="url" minlength="15" maxlength="58" id="facebook" name="facebook" value="<?=$facebook?>"/>
    Facebook Fan Page URL<br/>
    <input type="text" autocomplete="on" class="url" minlength="14" maxlength="57" id="myspace" name="myspace" value="<?=$myspace?>"/>
    MySpace Profile URL<br/>
    <input type="text" autocomplete="on" class="url" minlength="14" maxlength="57" id="twitter" name="twitter" value="<?=$twitter?>"/>
    Twitter Profile URL<br/>
    <input type="text" autocomplete="on" class="url" minlength="24" maxlength="67" id="deviantart" name="deviantart" value="<?=$deviantart?>"/>
    DeviantArt Profile URL<br/>
    <textarea minlength="30" maxlength="255" class="required alphaunct" id="biography" name="biography"><?=$biography?></textarea>
    Biography<br/>
    <input type="radio" value="Email" name="monthly" autocomplete="on" id="monthly"/>
    Monthly Newsletter Please<br/>
    <input class="submit" type="submit" name="submit" value="Save Changes"/>
    </form>
     
    codeartist, Apr 2, 2011 IP
  7. Airwalk Enterprise, Inc

    Airwalk Enterprise, Inc Peon

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    This is my revision <?php session_start();

    include($_SERVER["DOCUMENT_ROOT"]."/community/herotozero/database.php");

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

    $save_changes_work = $_REQUEST['submit'];
    if($save_changes_work) {

    $name = $_REQUEST['name'];

    $ins_sql = "INSERT UPDATE community (email, password, city, country, website, facebook, myspace, twitter, deviantart, biography, monthly) VALUES ('$email', '$password', '$city', '$country', '$website', '$facebook', '$myspace', '$twitter', '$deviantart', '$biography', '$monthly') WHERE name='$name' ";
    $ins_res = mysql_query($ins_sql);

    header('Location: http://www.airwalk-design.com/community/view-account/');

    }

    }

    // input data into database

    $name = $_SESSION['username'];
    $query = "SELECT * from community WHERE name='$name'";
    $result = mysql_query($query);
    $row = mysql_fetch_assoc($result);

    $name = $row['name'];
    $email = $row['email'];
    $password = $row['password'];
    $entity = $row['entity'];
    $city = $row['city'];
    $country = $row['country'];
    $website = $row['website'];
    $facebook = $row['facebook'];
    $myspace = $row['myspace'];
    $twitter = $row['twitter'];
    $deviantart = $row['deviantart'];
    $biography = $row['biography'];
    $news = $row['news'];

    echo "<form id=\"vaform\" method=\"post\" action=\"http://www.airwalk-design.com/community/view-account/\">
    <input type=\"text\" autocomplete=\"on\" class=\"required alphaspace\" minlength=\"5\" maxlength=\"49\" id=\"name\" name=\"name\" value=\"$name\" readonly=\"readonly\"/>
    Name<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"required email\" minlength=\"6\" maxlength=\"64\" id=\"email\" name=\"email\" value=\"$email\"/>
    Email<br/>
    <input type=\"password\" autocomplete=\"on\" class=\"required alpha\" minlength=\"5\" maxlength=\"32\" id=\"passwordtest\" name=\"passwordtest\"/>
    Password<br/>
    <input type=\"password\" autocomplete=\"on\" class=\"required alpha\" minlength=\"5\" maxlength=\"32\" id=\"password\" equalTo=\"#passwordtest\" name=\"password\"/>
    Verify It<br/>
    <input type=\"text\" value=\"$entity\" name=\"entity\" autocomplete=\"on\" id=\"entity\" readonly=\"readonly\"/>
    Entity<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"required alphaspace\" minlength=\"2\" maxlength=\"58\" id=\"city\" name=\"city\" value=\"$city\"/>
    Village/Town/City/State<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"required alphaspace\" minlength=\"2\" maxlength=\"38\" id=\"country\" name=\"country\" value=\"$country\"/>
    Country<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"15\" maxlength=\"70\" id=\"website\" name=\"website\" value=\"$website\"/>
    Website<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"15\" maxlength=\"58\" id=\"facebook\" name=\"facebook\" value=\"$facebook\"/>
    Facebook Fan Page URL<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"14\" maxlength=\"57\" id=\"myspace\" name=\"myspace\" value=\"$myspace\"/>
    MySpace Profile URL<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"14\" maxlength=\"57\" id=\"twitter\" name=\"twitter\" value=\"$twitter\"/>
    Twitter Profile URL<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"24\" maxlength=\"67\" id=\"deviantart\" name=\"deviantart\" value=\"$deviantart\"/>
    DeviantArt Profile URL<br/>
    <textarea minlength=\"30\" maxlength=\"255\" class=\"required alphaunct\" id=\"biography\" name=\"biography\">$biography</textarea>
    Biography<br/>
    <input type=\"radio\" value=\"Email\" name=\"monthly\" autocomplete=\"on\" id=\"monthly\"/>
    Monthly Newsletter Please<br/>
    <input class=\"submit\" type=\"submit\" value=\"Save Changes\" name=\"savechanges\"/>
    </form>";

    ?>

    im not quite sure what you mean with the user_id, so I assume thats why the page is just returning as a refresh :)
     
  8. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #8
    Add a die statement on the mysql_query.

    $ins_res = mysql_query($ins_sql) or die(mysql_error());

    And you do realize the way you have it now, if it was success on the UPDATE it would blank out everything but the name field?

    A quick explanation of the user_id would be that each record in the database table should have a unique, auto incremented id number per record. And it should be used as a key for any mysql queries on that table.
     
    shallowink, Apr 3, 2011 IP
  9. Airwalk Enterprise, Inc

    Airwalk Enterprise, Inc Peon

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #9
    could you elaborate on that? do you mean only the name field would actually update? because im not allowing the name field (or entity) to be changed, just for viewing purposes only. I only want the other fields to be able to be changed, but after inserting the php die it's still the same. is the user_id necessary for this to work? if not, I'll wait and sort the user_is afterwards.

    its still reverting back to the same page but no update is taking place.

    i appreciate the help btw :)
     
  10. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #10
    Yes, only the name field is set, it won't update though. You have to assign the passed values from the form to variables using the

    $variable = $_REQUEST['fieldname']; format.
    So :
    $email = $_REQUEST['email'];
    And do it for each of the values you want to update.

    The problem you have right now though is this input element :
    <input class=\"submit\" type=\"submit\" value=\"Save Changes\" name=\"savechanges\"/>

    is named 'savechanges' but you are not pulling that value in, you are using the examples we gave you where it's named 'submit' .

    Change this code section :

    to this :


    You don't need to assign $_POST['savechanges'] and check its value if you use the isset. You could use either method.
     
    shallowink, Apr 3, 2011 IP
  11. Airwalk Enterprise, Inc

    Airwalk Enterprise, Inc Peon

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Such an idiot mistake...fixed it now though. I seem to be making progress, only problem is now that I have an error after submitting "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 '(email, password, city, country, website, facebook, myspace, twitter, deviantart' at line 1"
     
  12. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #12
    Looks like you are still using the INSERT style statement (field,field,field) VALUES ('$var','$var','$var') instead of the UPDATE style statement SET field='$var',field2='$var2'. But you can just echo the $sql statement and post it if you still get the error.
     
    shallowink, Apr 3, 2011 IP
  13. Airwalk Enterprise, Inc

    Airwalk Enterprise, Inc Peon

    Messages:
    126
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #13
    Thanks guys, I've got this working like a charm. I've changed the final radio input, how could I have the radio auto-checked depending on the users input? (in the value="") using if statements?

    <?php session_start();

    include($_SERVER["DOCUMENT_ROOT"]."/community/herotozero/database.php");

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

    $name = $_REQUEST['name'];
    $email = $_REQUEST['email'];
    $city = $_REQUEST['city'];
    $country = $_REQUEST['country'];
    $website = $_REQUEST['website'];
    $facebook = $_REQUEST['facebook'];
    $myspace = $_REQUEST['myspace'];
    $twitter = $_REQUEST['twitter'];
    $deviantart = $_REQUEST['deviantart'];
    $biography = $_REQUEST['biography'];
    $monthly = $_REQUEST['monthly'];

    $ins_sql = "UPDATE community SET email='$email', city ='$city', country ='$country', website='$website', facebook='$facebook', myspace ='$myspace', twitter ='$twitter', deviantart ='$deviantart', biography ='$biography', monthly='$monthly' WHERE name='$name' ";
    $ins_res = mysql_query($ins_sql) or die(mysql_error());

    header('Location: http://www.airwalk-design.com/community/');

    }


    // input data into database

    $name = $_SESSION['username'];
    $query = "SELECT * from community WHERE name='$name'";
    $result = mysql_query($query);
    $row = mysql_fetch_assoc($result);

    $name = $row['name'];
    $email = $row['email'];
    $entity = $row['entity'];
    $city = $row['city'];
    $country = $row['country'];
    $website = $row['website'];
    $facebook = $row['facebook'];
    $myspace = $row['myspace'];
    $twitter = $row['twitter'];
    $deviantart = $row['deviantart'];
    $biography = $row['biography'];
    $monthly = $row['monthly'];

    echo "<form id=\"vaform\" method=\"post\" action=\"http://www.airwalk-design.com/community/view-account/\">
    <input type=\"text\" autocomplete=\"on\" class=\"required alphaspace\" minlength=\"5\" maxlength=\"49\" id=\"name\" name=\"name\" value=\"$name\" readonly=\"readonly\"/>
    Name<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"required email\" minlength=\"6\" maxlength=\"64\" id=\"email\" name=\"email\" value=\"$email\"/>
    Email<br/>
    <input type=\"text\" value=\"$entity\" name=\"entity\" autocomplete=\"on\" id=\"entity\" readonly=\"readonly\"/>
    Entity<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"required alphaspace\" minlength=\"2\" maxlength=\"58\" id=\"city\" name=\"city\" value=\"$city\"/>
    Village/Town/City/State<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"required alphaspace\" minlength=\"2\" maxlength=\"38\" id=\"country\" name=\"country\" value=\"$country\"/>
    Country<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"15\" maxlength=\"70\" id=\"website\" name=\"website\" value=\"$website\"/>
    Website<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"15\" maxlength=\"58\" id=\"facebook\" name=\"facebook\" value=\"$facebook\"/>
    Facebook Fan Page URL<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"14\" maxlength=\"57\" id=\"myspace\" name=\"myspace\" value=\"$myspace\"/>
    MySpace Profile URL<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"14\" maxlength=\"57\" id=\"twitter\" name=\"twitter\" value=\"$twitter\"/>
    Twitter Profile URL<br/>
    <input type=\"text\" autocomplete=\"on\" class=\"url\" minlength=\"24\" maxlength=\"67\" id=\"deviantart\" name=\"deviantart\" value=\"$deviantart\"/>
    DeviantArt Profile URL<br/>
    <textarea minlength=\"30\" maxlength=\"255\" class=\"required alphaunct\" id=\"biography\" name=\"biography\">$biography</textarea>
    Biography<br/>
    <input type=\"radio\" value=\"Email\" name=\"monthly\" autocomplete=\"on\" id=\"monthly\"/>
    Newsletter Please<br/>
    <input type=\"radio\" value=\"NoEmail\" name=\"monthly\" autocomplete=\"on\" id=\"monthly\"/>
    No Thanks<br/>
    <input class=\"submit\" type=\"submit\" value=\"Save Changes\" name=\"savechanges\"/>
    </form>";

    ?>
     
  14. shallowink

    shallowink Well-Known Member

    Messages:
    1,218
    Likes Received:
    64
    Best Answers:
    2
    Trophy Points:
    150
    #14
    if($monthly == "Email")
    $radio_one = 'checked="yes"';
    if($monthly == "NoEmail")
    $radio_two = 'checked="yes"';

    Then add this to the radio inputs.
    <input type=\"radio\" value=\"Email\" name=\"monthly\" autocomplete=\"on\" id=\"monthly\" $radio_one />

    <input type=\"radio\" value=\"NoEmail\" name=\"monthly\" autocomplete=\"on\" id=\"monthly\" $radio_two />

    Also you need to protect against SQL injection attacks by escaping the values before inserting into the database. Check out the php.net docs for mysql_real_escape_string().
     
    shallowink, Apr 3, 2011 IP
  15. srisen2

    srisen2 Peon

    Messages:
    359
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #15
    just use a php header redirect after you insert your data to take you to the file youd like to go to.
     
    srisen2, Apr 4, 2011 IP