Jump to content

Help, Form/MySQL Problem!


techiefreak05

Recommended Posts

Hi I have an "account editor" on my site and it works.. here is the code for my "update email" form:
[code]<?php
if($_POST['updateI']){
$username = $_SESSION['username'];
$new_email = mysql_real_escape_string(trim($_POST['emailNew']));

$sql = "UPDATE `users` SET `email` = '$new_email' WHERE `username` = '$username'";
$query = mysql_query($sql) or die(mysql_error());
}
?>
<?php
$sql = "SELECT * FROM users WHERE username = '$_SESSION[username]' LIMIT 1";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "<b>Current E-mail: '" .$row['email']. "'</b>";
}
?>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td ><font color=black>New E-mail:</font></td><td><input type="text" name="emailNew" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="updateI" value="-Update-"></td></tr>
</table>
    </form>[/code]
now that works fine. and here is my update Name code:
[code]<?php
if($_POST['updateFNAME']){
$username = $_SESSION['username'];
$new_name = mysql_real_escape_string(trim($_POST['fnameNew']));

$sql = "UPDATE `users` SET `fname` = '$new_name' WHERE `username` = '$username'";
$query = mysql_query($sql) or die(mysql_error());
}
?>
<?php
$sql = "SELECT * FROM users WHERE username = '$_SESSION[username]' LIMIT 1";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "<b>Current First Name: '" .$row['fname']. "'</b>";
}
?>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td ><font color=black>New First Name:</font></td><td><input type="text" name="fnameNew" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="updateFNAME" value="-Update-"></td></tr>
</table>
    </form>[/code]
**NOW, heres my problem, I have a button that is supposed to change everything at once, cuz right now if you want to update any of those above, you need to press each single button for each item.. heres my code thats NOT WORK, that is suposed to combine bothe codes from above:
[code]<?php
if($_POST['updateALL']){
$username = $_SESSION['username'];
$new_name = mysql_real_escape_string(trim($_POST['fnameNew']));
$new_email = mysql_real_escape_string(trim($_POST['emailNew']));

$sql = "UPDATE `users` SET `fname` = '$new_name', `email` = '$new_email' WHERE `username` = '$username'";
$query = mysql_query($sql) or die(mysql_error());
}
?>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td colspan="2" align="right"><input type="submit" name="updateALL" value="-Save All-"></td></tr>
</table>
    </form>
</center>[/code]
How can I change that to update all the fields at once??
Link to comment
Share on other sites

This line:

<input type="text" name="fnameNew" maxlength="30">

And this line:

<input type="text" name="emailNew" maxlength="30">

Both have to be within the <form> tags for the update all form or the the values $_POST['fnameNew'] and $_POST['emailNew'] will both be empty.
Link to comment
Share on other sites

[quote author=techiefreak05 link=topic=104456.msg416640#msg416640 date=1155708798]
wont ading those lines add text boxes?? i dont want that, i just want tthe update all button to update all the inromation at once, i only want one text box for each piece of information
[/quote]
Yeah. You should just create one form with three different submit buttons with three different names. If you have three different forms, then you'll have to have six different text boxes (in your case).
Link to comment
Share on other sites

Hey I got it! . heres what i ended up with:
[code]<center>
<?php
if($_POST['updateI']){
$username = $_SESSION['username'];
$new_email = mysql_real_escape_string(trim($_POST['emailNew']));

$sql = "UPDATE `users` SET `email` = '$new_email' WHERE `username` = '$username'";
$query = mysql_query($sql) or die(mysql_error());
}
?>
<?php
$sql = "SELECT * FROM users WHERE username = '$_SESSION[username]' LIMIT 1";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "<b>Current E-mail: '" .$row['email']. "'</b>";
}
?>
<form action="" method="post">
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td ><font color=black>New E-mail:</font></td><td><input type="text" name="emailNew" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="updateI" value="-Update-"></td></tr>
</table>
<! - END EMAIL - !>
<hr width=35%>
<! - UPDATE FNAME - !>
<?php
if($_POST['updateFNAME']){
$username = $_SESSION['username'];
$new_name = mysql_real_escape_string(trim($_POST['fnameNew']));

$sql = "UPDATE `users` SET `fname` = '$new_name' WHERE `username` = '$username'";
$query = mysql_query($sql) or die(mysql_error());
}
?>
<?php
$sql = "SELECT * FROM users WHERE username = '$_SESSION[username]' LIMIT 1";
$query = mysql_query($sql);
while($row = mysql_fetch_array($query)) {
echo "<b>Current First Name: '" .$row['fname']. "'</b>";
}
?>
      <table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td ><font color=black>New First Name:</font></td><td><input type="text" name="fnameNew" maxlength="30"></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="updateFNAME" value="-Update-"></td></tr>
</table>
<table align="center" border="0" cellspacing="0" cellpadding="3">
<tr><td colspan="2" align="right"><input type="submit" name="updateALL" value="-Save All-"></td></tr>
</table>
    </form>
</center>
<!-  END FNAME - !>

<! - UPDATE ALL - !>
<?php
if($_POST['updateALL']){
$username = $_SESSION['username'];
$new_name = mysql_real_escape_string(trim($_POST['fnameNew']));
$new_email = mysql_real_escape_string(trim($_POST['emailNew']));

$sql = "UPDATE `users` SET `fname` = '$new_name', `email` = '$new_email' WHERE `username` = '$username'";
$query = mysql_query($sql) or die(mysql_error());
}
?>
<!-  END ALL - !>[/code]
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.