0

I have a page on my site where I want to update only certain fields. There is a php form tht captures the data. Currently, when I update one field - it blanks out all the others. I want to be able to only update the one field and if there is no change to the others - to simply leave the data as is. I believe the answer is to use isnull.

I think I am close....thoughts?

<?php
 $con=mysqli_connect("xxxxxx","xxxxx","xxxxxxx","xxxxxxxxx");
 // Check connection
 if (mysqli_connect_errno())
   {
   echo "Failed to connect to MySQL: " . mysqli_connect_error();
   }

$sql="UPDATE trades$year$dash$leagueID 
SET
TeamA = isnull(@TeamA, '$_POST[TeamA]'),
A1 = isnull(@A1, '$_POST[A1]'),
A2 = isnull(@A2, '$_POST[A2]'),
A3 = isnull(@A3, '$_POST[A3]'),
A4 = isnull(@A4, '$_POST[A4]'),
A5 = isnull(@A5, '$_POST[A5]'),
A6 = isnull(@A6, '$_POST[A6]'),
A7 = isnull(@A7, '$_POST[A7]'),
A8 = isnull(@A8, '$_POST[A8]'),
A9 = isnull(@A9, '$_POST[A9]')
WHERE
tradeID = @'$_POST[tradeID]'
";

if (!mysqli_query($con,$sql))
   {
   die('Error: ' . mysqli_error());
   }

 // Make a MySQL Connection
mysql_connect("xxxxxxxx", "xxxxx", "xxxxxxx") or die(mysql_error());
mysql_select_db("xxxxxxx") or die(mysql_error());   
$GETQ="SELECT * FROM trades$year$dash$leagueID where tradeID = '$_POST[tradeID]'";

$result = mysql_query($GETQ)
or die(mysql_error()); 
echo "<center><table width='400' border='1' bordercolor='ffffff' cellspacing='0' cellpadding='1' bgcolor='ffffff'>";

// keeps getting the next row until there are no more to get

while($row = mysql_fetch_array( $result )) {
    // Print out the contents of each row into a table
    echo "<tr height='20'><td colspan='2' bgcolor='000000' valign='top' align='left'><font face='arial' size='1' color='FFFFFF'><b>"; 
    echo $row['tradeID'];
    echo "<font face='arial' size='3' color='cccccc'> |  <font face='arial' size='2' color='FF0000'><b>"; 
    echo ' ' .date('M. d, Y', strtotime($row['Date']) );
    echo "</b></td></tr>";


    echo "<tr><td width='200' bgcolor='000000' align='center'><font face='arial' size='2' color='000000'>";
    echo "<img src='/images/100".$row['TeamA'].".jpg'>";
    echo "</td><td width='200' bgcolor='000000' align='center'><font face='arial' size='2' color='000000'>";
    echo "<img src='/images/100".$row['TeamB'].".jpg'>";
    echo "</td></tr>";

    echo "<tr><td width='200'align='left'><font face='arial' size='2' color='000000'>";
    echo $row['A1'];
    echo "<td width='200' align='left'><font face='arial' size='2' color='000000'>";
    echo $row['B1'];

    echo "</td></tr><tr><td width='200' align='left'><font face='arial' size='2' color='000000'>";
    echo $row['A2'];
    echo "<td width='200' align='left'><font face='arial' size='2' color='000000'>";
    echo $row['B2'];

    echo "</td></tr><tr><td width='200' align='left'><font face='arial' size='2' color='000000'>";
    echo $row['A3'];
    echo "<td width='200' align='left'><font face='arial' size='2' color='000000'>";
    echo $row['B3'];


    echo "</td></tr><tr><td width='200' align='left'><font face='arial' size='2' color='000000'>";
    echo $row['A4'];
    echo "<td width='200' align='left'><font face='arial' size='2' color='000000'>";
    echo $row['B4'];



    echo "</td></tr>";
}
echo"</table>"; 
mysqli_close($con);
 ?>  

Any help would be appreciated!

2
Contributors
2
Replies
13
Views
4 Years
Discussion Span
Last Post by ceeandcee
1

The problem is probably that your values may contain an empty string, instead of a null value.

1

Thanks Pritaeas - your suggestion launched yet another google search - that lead to the answer! I appreciate your help!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.