I have a form and I want to update the database. If I want to update only line 1 in input field or line 2 input field then how is this possible using update query? Note: I want the previous data leave as it was store in database.
This is from code.
<?php include 'header.php'; ?>
<?php
if(isset($_POST) && count($_POST)>0) {
$description1 = $_POST['line1_desc'];
$description2 = $_POST['line2_desc'];
$sSQL =mysql_query("UPDATE tbl_images SET $description1,$description2 Where id=".intval($_GET['id']));
if($sSQL){
echo 'done';
}
else {
mysql_error();
}
//header("location: show_property_pic.php");
}
?>
<div>
<form name="frmProudct" id="frmProduct" action="" method="post" enctype="multipart/form-data">
<table align='center'>
<tr>
<td>Line1:</td>
<td> <textarea cols="100" name="line1_desc"></textarea></td>
</tr>
<td>Line2:</td>
<td> <textarea cols="100" name="line2_desc"></textarea></td>
</tr>
<tr>
<td></td>
<td><input type="submit" id="btnSub" value="Save" /> </td>
</tr>
</table>
</form>
</div>
<?php include 'footer.php' ?>
Thanks in advance.
If you want to make the second textarea optional, you could just create a initial string (which is the first textarea), then just append the second one if its not empty. Consider this example: Sample Fiddle
if(isset($_POST['line1_desc']) && (trim($_POST['line1_desc']) != '')) {
$input[] = "`line1_desc` = '" . $_POST['line1_desc'] ."'";
}
if(isset($_POST['line2_desc']) && (trim($_POST['line2_desc']) != '')) {
$input[] = "`line2_desc` = '" . $_POST['line2_desc'] ."'";
}
$values = implode(', ', $input);
$statement = "UPDATE `tbl_images` SET $values WHERE `id` = your_id";
echo $statement;
// if both textareas have input:
// UPDATE `tbl_images` SET `line1_desc` = 'comment 1', `line2_desc` = 'comment 2' WHERE `id` = your_id
// if only line1_desc1 textarea is only provided:
// UPDATE `tbl_images` SET `line1_desc` = '1' WHERE `id` = your_id
// if only line1_desc2 textarea is only provided:
// UPDATE `tbl_images` SET `line2_desc` = '2' WHERE `id` = your_id
Note: If possible, migrate/use the newer version of mysql extension which is
MYSQLI
or usePDO
Basicly, your SQL Statement is Wrong.
It should be:
'UPDATE <tablename> SET <fieldname> = "<Content>", <fieldname> = "<Content>" WHERE <Condition>'
In your case
'UPDATE tbl_images SET description1 = "' . $description1 . '", description2 = "' . $description2 . '" Where id='.intval($_GET['id'])
But please inform about SQL Injection
and Prepared Statements
. And Stop using mysql_
functions right now! Better to use mysqli_
for fast migration even PDO
, wich is 1.000 times better.