update specific fields in mysql
Moderator: General Moderators
-
php4user2007
- Forum Newbie
- Posts: 12
- Joined: Mon Apr 02, 2007 4:12 am
update specific fields in mysql
Hi,
I would like to know if there is an easy way to let php update a mysql database only if values have been assigned to the specific variables that go into each field.
For instance if I would like to update a table in mysql with 10 fields I would create a form on a website with 10 input fields and write a php script that will insert all of the 10 variables into the 10 fields. However, I want only the fields that contain values entered by the user to be updated; i.e., the fields that the user did not enter any new varialbes for should not be altered.
Is there a way to do that or will I have to write 10 if statements and make the update of each field conditional whether the user input a value in the corresponding field in the form.
thanks,
Philip
I would like to know if there is an easy way to let php update a mysql database only if values have been assigned to the specific variables that go into each field.
For instance if I would like to update a table in mysql with 10 fields I would create a form on a website with 10 input fields and write a php script that will insert all of the 10 variables into the 10 fields. However, I want only the fields that contain values entered by the user to be updated; i.e., the fields that the user did not enter any new varialbes for should not be altered.
Is there a way to do that or will I have to write 10 if statements and make the update of each field conditional whether the user input a value in the corresponding field in the form.
thanks,
Philip
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
- mikeeeeeeey
- Forum Contributor
- Posts: 130
- Joined: Mon Jul 03, 2006 4:17 am
- Location: Huddersfield, UK
let the form hold the existing data,
that way the user can then use the form to change whatever they want, the form will then overwrite the existing data with the form contents.
hence, information that has changed will be changed and the rest will stay the same.
alternatively, look at hidden form inputs and a foreach() if you'd prefer to do this without outputting lots of data.
hope it helps!
that way the user can then use the form to change whatever they want, the form will then overwrite the existing data with the form contents.
hence, information that has changed will be changed and the rest will stay the same.
alternatively, look at hidden form inputs and a foreach() if you'd prefer to do this without outputting lots of data.
hope it helps!
If you only want to input the data into the database that the user has inputed and leave everything else alone, you can build the query string dynamically. Below is an example of a script I did that builds a query string dynamically. It takes values put into fields and searches the database for values pertaining to the variables received from the form. Rather selecting you would be inserting:
I'm fairly new to php so there might be some errors but the script does work as i wanted it to.
Hope that helps.
Wayne
Code: Select all
if (isset($_POST['submit'])) {
require_once ('../mysql_connect.php');
//Build query
$query = "SELECT * FROM users WHERE ";
if (!empty($_POST['first_name'])) {
$first_name = $_POST['first_name'];
$num = ($num + 1);
$query .= "first_name='$first_name' ";
} else {
$query = $query;
$first_name = FALSE;
}
if (!empty($_POST['last_name'])) {
$last_name = $_POST['last_name'];
$num = ($num + 1);
if ($num >= 2) {
$query .= "AND last_name='$last_name' ";
} else {
$query .= "last_name='$last_name' ";
}
} else {
$last_name = FALSE;
}
if (!empty($_POST['email'])) {
$email = $_POST['email'];
$num = ($num + 1);
if ($num >= 2) {
$query .= "AND email='$email'";
} else {
$query .= "email='$email'";
}
} else {
$email = FALSE;
}
if (($first_name == FALSE) && ($last_name == FALSE) && ($email == FALSE)) {
echo '<p><font color="red" size="+1">You did not enter any search criteria.</font></p>';
} else {
$result = mysql_query($query);
if (mysql_num_rows($result) >= 1) {
echo '<table border="1" cellpadding="5" align="center">';
echo "<tr><th>Name</th><th>email</th></tr>";
while ($row = mysql_fetch_array($result)) {
echo '<tr><td align="center">';
echo $row['first_name'] . " " . $row['last_name'];
echo '</td><td align="center">';
echo '<a href="mailto:' . $row['email'] . '">' . $row['email'] . '</a>';
echo "</td></tr>";
}
echo '</table>';
mysql_close();
include ('./includes/footer.php');
exit();
} else {
echo '<p><font color="red" size="+1">Your search did not match any of our records.</font></p>';
}
}
}Hope that helps.
Wayne
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
This is my usual logic...
- Fetch the data to edit and read it into an array (or a series of individual vars if that is your preference)
- Use that data to fill the form the user is editing
- On submit (checking for POST vars sent), assign the value of each preset variable to the posted data
- Validate/sanitize/filter your vars
- If there are no errors, use the clean posted data (which occupies the preset vars at the point) in your update query
- Otherwise use that same data to refill the form without any changes to the core data
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
- RobertGonzalez
- Site Administrator
- Posts: 14293
- Joined: Tue Sep 09, 2003 6:04 pm
- Location: Fremont, CA, USA
What I meant was:
Code: Select all
<?php
if (isset($_POST['some_form_field_name']))
{
// do some stuff
}
?>