Page 1 of 1

How to only update a record when form value is not null?

Posted: Sat Nov 29, 2008 1:21 am
by ViserExcizer
Hello,

i need a solution to this, can't seem to find it anywhere.

Code: Select all

 
 
 
 
 
<?php
 
                $name = $_POST["name"];
        $name2 = $_POST["name2"];
        $name3 = $_POST["name3"];
        
 
$con = mysql_connect("localhost","xxxxxxxxx","xxxxxxxxx");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
 
mysql_select_db("xxxxxxxxx", $con);
 
 
 
$queryb = "UPDATE usersite SET name='$name', name2='$name2', name3='$name3' WHERE user='zvork'";
mysql_query($queryb) or die(mysql_error());
mysql_close($con)
?>
 
 
 
<html>
<body>
 
<table summary="">
<tr>
<td valign="top">
<form width="" style="background-color: #fff; "  action="mysites.php" method="post">
name: <input type="text" size="30" name="name" /><br>
name2: <input type="text" size="30" name="name2" /><br>
name3: <input type="text" size="30" name="name3" /><br>
<input type="submit" value="submit" />
</form>
 
<br><br><br><br>
<table align="center"><tr><td>
<a href="/admin.php">Go back</a>
<br><br>
</td>
</tr>
</table>
</body>
</html>
 
 
 
This is a stripped down form, with only the parts where i have the problem with

Thats it...When someone only enters a value in only one input, i.e. name, and leaves the rest blank, and clicks submit, it updates the table's field name2 and name3 with blank value....it erases the old record... How to use boolean to make the table only update the field with values that are not null in the form?

Re: How to only update a record when form value is not null?

Posted: Sat Nov 29, 2008 2:24 am
by requinix
You blindly update those fields without checking if you should. So... check if you should.

This is really similar to the "multiple search criteria" thread, except you're performing an update instead of a search. Take a look at the suggestions there, decide which suits the problem and your tastes best (I suggest pcoder's for this), then try to implement it.

Re: How to only update a record when form value is not null?

Posted: Sat Nov 29, 2008 2:39 am
by ViserExcizer
tasairis wrote:You blindly update those fields without checking if you should. So... check if you should.

This is really similar to the "multiple search criteria" thread, except you're performing an update instead of a search. Take a look at the suggestions there, decide which suits the problem and your tastes best (I suggest pcoder's for this), then try to implement it.

thanks for your reply,

yes, i dont want the fields to get updated if no value is typed in the form input.

The link for the multiple search criteria thread you gave me is broken,

is there another way to do this, using a php array extract($array, EXTR_PREFIX_ALL, ,) function? I figured, it would extract the original records from the database and use it instead if the form input is null.

Re: How to only update a record when form value is not null?

Posted: Sat Nov 29, 2008 3:37 am
by requinix
...Or you could just build your query in such a way that only the fields you want to change get changed.

Don't want to update name2? Don't put it in the query.

Code: Select all

UPDATE table SET name="...", name3="..." WHERE condition

Re: How to only update a record when form value is not null?

Posted: Sat Nov 29, 2008 12:16 pm
by califdon
ViserExcizer wrote:
tasairis wrote:The link for the multiple search criteria thread you gave me is broken,
...
is there another way to do this, using a php array extract($array, EXTR_PREFIX_ALL, ,) function? I figured, it would extract the original records from the database and use it instead if the form input is null.
Link works fine for me.

Well, that's one way you could do it: load variables for all fields from database, then reassign values to any that have non-null values from form. Another way would be to simply test each value from the form and create your Update SQL string based on which values are present. I'm not sure if there's any practical difference in terms of either code or execution time.

Re: How to only update a record when form value is not null?

Posted: Sun Nov 30, 2008 12:16 pm
by PHPyrox
I think I would've used something like this:

Code: Select all

 
if(isset($_POST['name1']) && $_POST['name1'] == TRUE)
{
   //In this case I check if name1 var has some value
   //In the variable $name1 I store the complete query code
   $name1 = 'name=';
   $name1 .= $_POST['name1'];
}
else
{
   $name1 = '';
}
 
I would use the same code for name3, but a little exeption for name2:

Code: Select all

 
if(isset($_POST['name1']) && $_POST['name1'] == TRUE)
{
   //Here's the same as with name1
   $name2 = 'name=';
   $name2 .= $_POST['name2'];
 
   //This is just a comma-thing to separate name2 from name1 and/or name3, keep in mind, this is the direct query
   if($name1 == TRUE)
   {
      $name2 = ', ' . $name2;
   }
    if($name3 == TRUE)
   {
      $name2 .= ', ';
   }
}
elseif(($name1 == TRUE) && ($name3 == TRUE) && ($_POST['name2'] == FALSE))
{
   //If we don't have a 'name2' variable, we will still be able to us this as an advantage
   //to comma-separate name1 from name3
   $name2 = ', ';
}
else
{
   $name2 = '';
}
 
Good luck!

EDIT: For this to work the code-blocks for the 2nd variable has to be written after both the code-block for name1 and name3

Here's the eventual query

Code: Select all

 
$queryb = "UPDATE usersite SET $name1 . $name2 . $name3 WHERE user='zvork'";