Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

There is a table:

|id name surname email    |
|1  john surjohn @mail.com|
|2  peter pet    @mail.com|                   
|.........................|

PHP:

<?php
if(isset($_POST['update']))
{
...
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());

}

$id = $_POST['id'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = $_POST['mail'];

$sql = "UPDATE emps ".
       "SET name= '$name', surname'$surname', email='$mail' ".
       "WHERE Id = $id" ;
mysql_select_db('dbase');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "blablablabla ! \n";
mysql_close($conn);
}
else

{
?>
<form method="post" action="<?php $_PHP_SELF ?>">
    <fieldset style="width: 350px" >
<table width="400" border="0" cellspacing="1" cellpadding="2">
<tr>
<td width="100">Id </td>
<td><input name="id" type="text" id="id" value=""></td>
</tr>
<tr>
<td width="100">name</td>
<td><input type="text" maxlength="15" name="name" value="" ></td>
</tr>
<tr>
<td width="100">surname</td>
<td><input type="text" maxlength="40" name="surname" value="" ></td>
</tr>
<tr>

<td width="100"> </td>
<td>
<input name="update" type="submit" id="update" value="update">
</td>
</tr>
</table>
        </fieldset>
</form>
<?php
}
?>
   } 

In this form i need update all fields otherwise, updated table can have null values. I want for example, update name field, but leave surname, email existing values. ideas?

share|improve this question
Not sure what you are asking. – MeLight Feb 27 at 12:43
2  
Best way to do it is populate the input values with the values from the database, that way if someone changes the information it will update, if they don't it will just update with whatever was in the database anyway. – Karl Feb 27 at 12:43
Can you correct sense of question? Didn't understand – 1------- Feb 27 at 12:44
What he's saying (I think) is that when he submits the form, if the person didn't complete the "surname" field it would update blank in the database, rather than keeping the content. – Karl Feb 27 at 12:45
1  
You are using an obsolete database API and should use a modern replacement. You are also vulnerable to SQL injection attacks that a modern API would make it easier to defend yourself from. – Quentin Feb 27 at 13:01
show 5 more comments

2 Answers

Could try something like this:

$id = $_POST['id'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = $_POST['mail'];

$sql = "UPDATE emps SET";
$moresql = '';

if(isset($name) && !empty($name)) {
    $moresql .= " name = '$name'";  
}

if(isset($surname) && !empty($surname)) {
    if ($moresql) $moresql .= ',';
    $moresql .= " surname = '$surname'";    
}

if(isset($mail) && !empty($mail)) {
    if ($moresql) $moresql .= ',';
    $moresql .= " mail = '$mail'";  
}

$sql .= $moresql;
$sql .= " WHERE Id = '$id'";

This is untested though.

share|improve this answer
Updated my answer, tested and working. – Karl Feb 27 at 13:03

Karl's idea is the way to go, but it can be refactored this way:

$id = $_POST['id'];

$sql = "UPDATE emps SET";
$fieldValuePairs = array();

foreach($_POST as $key => value)
    if($key != 'id')
       $fieldValuePairs[] = "'$key' = '$value'";

$sql .= " ". implode(',', $fieldValuePairs)." WHERE id = $id";

Note: this works only if you use input names (in the form) equal the column names (in the database).

Have a great day.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.