ecabrera Posted February 5, 2012 Share Posted February 5, 2012 ok i have a questions i making a form so when users sign up to my website they we have an account page and in the account page the will have some more fields they need to filll in which i would use INSERT to put the things inside my db but what if they want to update there account should i use UPDATE Link to comment https://forums.phpfreaks.com/topic/256430-php-forms/ Share on other sites More sharing options...
Andy11548 Posted February 5, 2012 Share Posted February 5, 2012 Yeah, you will want to use the UPDATE for when they update thier account. It'll be like $update = mysql_query("UPDATE `users` SET `email`='$newEmail', `fullname`='$fullname' WHERE `username`='$_SESSION['username']'"); Link to comment https://forums.phpfreaks.com/topic/256430-php-forms/#findComment-1314627 Share on other sites More sharing options...
ecabrera Posted February 5, 2012 Author Share Posted February 5, 2012 ok but there or going to be some new fills in there account wouldnt i have to use insert ?? Link to comment https://forums.phpfreaks.com/topic/256430-php-forms/#findComment-1314633 Share on other sites More sharing options...
Andy11548 Posted February 5, 2012 Share Posted February 5, 2012 I don't understand your database... If its like: user_id | username | password | email | fullname | number when you do the insert you would leave the fields they fill in later black like: mysql_query("INSERT INTO `users` VALUES ('', '$username', '$password', '$email', '', '')"); as you can see I left the fields "Fullname" and "Number" blank. When you use the update, you just define what field you want updating with what value. Link to comment https://forums.phpfreaks.com/topic/256430-php-forms/#findComment-1314637 Share on other sites More sharing options...
ecabrera Posted February 5, 2012 Author Share Posted February 5, 2012 ok Link to comment https://forums.phpfreaks.com/topic/256430-php-forms/#findComment-1314639 Share on other sites More sharing options...
ecabrera Posted February 5, 2012 Author Share Posted February 5, 2012 ok for example here users have to put in more details so should i use INSERT...But if THEY WANT TO UPDATE?? <?php //if logged in if($email){ ?> <?php require ("scripts/connect.php"); $query = mysql_query("SELECT * FROM users WHERE email='$email'"); $numrows = mysql_num_rows($query); if ($numrows == 1){ $row = mysql_fetch_assoc($query); $id = $row['id']; $first = $row['firstname']; $last = $row['lastname']; $email = $row['email']; } else echo "An error occured while connecting to the database."; ?> <div id="userid"> <center><h2><?php echo $first; ?> Account</h2></center> <div id="useraccount"> <?php require ("scripts/connect.php"); if($_POST['savebutton']){ $msg = ""; $first = $_POST['name']; $last = $_POST['last']; $email = $_POST['email']; $phone = $_POST['phone']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state ']; $zip = $_POST['zip']; if($first && $last && $email){ $update = mysql_query("UPDATE users SET firstname='$first',lastname='$last',email='$email', phone='$phone',address='$address',city='$city',state='$state',zip='$zip' WHERE id='$id'"); $msg = "Changes Save"; }else $msg = "YOU MUST FILL IN ALL THREE"; } mysql_close(); ?> <form> <table> <tr> <td></td> <td><?php echo $msg; ?></td> </tr> <tr> <td>First Name</td> <td><input type="text" name="name" value="<?php echo $first; ?>" /></td> </tr> <tr> <td>Last Name</td> <td><input type="text" name="last" value="<?php echo $last; ?>" /></td> </tr> <tr> <td>Email:</td> <td><input type="text" name="email" value="<?php echo $email; ?>" /></td> </tr> <tr> <td>Phone:</td> <td><input type="text" name="phone" /></td> </tr> <tr> <td>Address:</td> <td><input type="text" name="address" /></td> </tr> <tr> <td>City:</td> <td><input type="text" name="city" /></td> </tr> <tr> <td>State:</td> <td> <select name="state" value="<?php echo $state; ?>"> <option>Select a state</option> <option value="AL">Alabama</option> <option value="AK">Alaska</option> <option value="AZ">Arizona</option> <option value="AR">Arkansas</option> <option value="CA">California</option> <option value="CO">Colorado</option> <option value="CT">Connecticut</option> <option value="DE">Delaware</option> <option value="DC">District Of Columbia</option> <option value="FL">Florida</option> <option value="GA">Georgia</option> <option value="HI">Hawaii</option> <option value="ID">Idaho</option> <option value="IL">Illinois</option> <option value="IN">Indiana</option> <option value="IA">Iowa</option> <option value="KS">Kansas</option> <option value="KY">Kentucky</option> <option value="LA">Louisiana</option> <option value="ME">Maine</option> <option value="MD">Maryland</option> <option value="MA">Massachusetts</option> <option value="MI">Michigan</option> <option value="MN">Minnesota</option> <option value="MS">Mississippi</option> <option value="MO">Missouri</option> <option value="MT">Montana</option> <option value="NE">Nebraska</option> <option value="NV">Nevada</option> <option value="NH">New Hampshire</option> <option value="NJ">New Jersey</option> <option value="NM">New Mexico</option> <option value="NY">New York</option> <option value="NC">North Carolina</option> <option value="ND">North Dakota</option> <option value="OH">Ohio</option> <option value="OK">Oklahoma</option> <option value="OR">Oregon</option> <option value="PA">Pennsylvania</option> <option value="RI">Rhode Island</option> <option value="SC">South Carolina</option> <option value="SD">South Dakota</option> <option value="TN">Tennessee</option> <option value="TX">Texas</option> <option value="UT">Utah</option> <option value="VT">Vermont</option> <option value="VA">Virginia</option> <option value="WA">Washington</option> <option value="WV">West Virginia</option> <option value="WI">Wisconsin</option> <option value="WY">Wyoming</option> </select> </td> </tr> <tr> <td>Zip Code</td> <td><input type="text" name="zip" maxlength="10" size="8" /></td> </tr> <tr> <td></td> <td><input type="submit" name="savebutton" value="SAVE CHANGES" /></td> </tr> </table> </form> </div> </div> Link to comment https://forums.phpfreaks.com/topic/256430-php-forms/#findComment-1314646 Share on other sites More sharing options...
Pikachu2000 Posted February 5, 2012 Share Posted February 5, 2012 If the record already exists, and you want to change something about it, you use an UPDATE query. If the record does not already exist, and you need to create it, you use an INSERT query. that's about all there is to it. Link to comment https://forums.phpfreaks.com/topic/256430-php-forms/#findComment-1314648 Share on other sites More sharing options...
ecabrera Posted February 5, 2012 Author Share Posted February 5, 2012 ok Link to comment https://forums.phpfreaks.com/topic/256430-php-forms/#findComment-1314649 Share on other sites More sharing options...
ecabrera Posted February 5, 2012 Author Share Posted February 5, 2012 why doesnt this work <?php require ("scripts/connect.php"); if($_POST['savebutton']){ $msg = ""; $first = $_POST['name']; $last = $_POST['last']; $email = $_POST['email']; $phone = $_POST['phone']; $address = $_POST['address']; $city = $_POST['city']; $state = $_POST['state ']; $zip = $_POST['zip']; if($first && $last && $email){ $insert = mysql_query("INSERT INTO users (firstname, lastname, email, phone, address, city, state, zip) VALUES('$first','$last','$email','$phone','$address','$city','$state','$zip')"); $msg = "Changes Save"; }else $msg = "YOU MUST FILL IN ALL THREE"; } mysql_close(); ?> Link to comment https://forums.phpfreaks.com/topic/256430-php-forms/#findComment-1314652 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.