我正在从事图书馆管理系统工作。并在更新字段中。如果用户没有传递数据,我希望保持我的数据不变,而不是将其更改为空白
<?php
if( isset($_POST['update']) )
{
$conn = mysql_connect('localhost', 'root', '');
if( !$conn )
{
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('library', $conn);
$Book_ID = $_POST['Book_ID'];
$Book_Name = $_POST['Book_Name'];
$Author = $_POST['Author'];
$Quantity = $_POST['Quantity'];
$sql = "update books set Book_Name='$Book_Name', Author='$Author',
Quantity='$Quantity' where Book_ID='$Book_ID'";
mysql_select_db('test_db');
$retval = mysql_query($sql, $conn);
if( !$retval )
{
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
header("refresh:2; url=updatebook.php");
mysql_close($conn);
}
else
{
?>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<table width = "400" border =" 0" cellspacing = "1" cellpadding = "2">
<tr>
<td width = "100">Book ID</td>
<td><input name = "Book_ID" type = "number" id = "Book_ID"></td>
</tr>
<tr>
<td width = "100">Book Name</td>
<td><input name = "Book_Name" type = "text" id = "Book_Name"></td>
</tr>
<tr>
<td width = "100">Author</td>
<td><input name = "Author" type = "text" id = "Author"></td>
</tr>
<tr>
<td width = "100">Quantity</td>
<td><input name = "Quantity" type = "text" id = "Quantity"></td>
</tr>
<tr>
<td width = "100"> </td>
<td> </td>
</tr>
<tr>
<td width = "100"> </td>
<td>
<tr>
<td width = "100"> </td>
<td> </td>
</tr>
<tr>
<td width = "100"> </td>
<td>
<input name = "update" type = "submit" id = "update" value = "Update">
</td>
</tr>
</table>
</form>
<?php
}
?>
如果用户不想更改某个字段的值并将其保留为空白。我不想用空白更新该字段。我希望保留该字段的数据。
如果我输入图书编号并保留图书名称,则从此代码中删除,然后此代码传递值
答案 0 :(得分:2)
您可以借助mysql中的简单if...else
条件来完成此操作。
$sql = "UPDATE books SET
Book_Name=IF(LENGTH('$Book_Name')=0, Book_Name, '$Book_Name'),
Author=IF(LENGTH('$Author')=0, Author, '$Author'),
Quantity=IF(LENGTH('$Quantity')=0, Quantity, '$Quantity')
WHERE Book_ID='$Book_ID'";
不要对数据库使用直接查询。它就像是对Sql Injection的公开邀请。
答案 1 :(得分:0)
如果变量为空,则需要首先获取原始值,然后使用IF ELSE来使用旧值而不是新值。请参阅我修改过的以下代码:
`
$conn = mysql_connect('localhost', 'root', '');
if(!$conn ) {
die('Could not connect: ' . mysql_error());
}
$db_selected = mysql_select_db('library', $conn);
$old_data = mysql_fetch_array(mysql_query("select * from books where Book_ID='".$_POST['Book_ID']."'"));
$Book_ID = $_POST['Book_ID'];
$Book_Name = ($_POST['Book_Name'] == '' ? $old_data['Book_Name'] : $_POST['Book_Name']);
$Author = ($_POST['Author'] == '' ? $old_data['Author'] : $_POST['Author']);
$Quantity = ($_POST['Quantity'] == '' ? $old_data['Quantity'] : $_POST['Quantity']);
$sql = "update books set Book_Name='$Book_Name', Author='$Author',
Quantity='$Quantity' where Book_ID='$Book_ID'";
mysql_select_db('test_db');
$retval = mysql_query( $sql, $conn );
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";
header("refresh:2; url=updatebook.php");
mysql_close($conn);
}else {
?>
<form method = "post" action = "<?php $_PHP_SELF ?>">
<table width = "400" border =" 0" cellspacing = "1"
cellpadding = "2">
<tr>
<td width = "100">Book ID</td>
<td><input name = "Book_ID" type = "number"
id = "Book_ID"></td>
</tr>
<tr>
<td width = "100">Book Name</td>
<td><input name = "Book_Name" type = "text"
id = "Book_Name"></td>
</tr>
<tr>
<td width = "100">Author</td>
<td><input name = "Author" type = "text"
id = "Author"></td>
</tr>
<tr>
<td width = "100">Quantity</td>
<td><input name = "Quantity" type = "text"
id = "Quantity"></td>
</tr>
<tr>
<td width = "100"> </td>
<td> </td>
</tr>
<tr>
<td width = "100"> </td>
<td>
<tr>
<td width = "100"> </td>
<td> </td>
</tr>
<tr>
<td width = "100"> </td>
<td>
<input name = "update" type = "submit"
id = "update" value = "Update">
</td>
</tr>
</table>
</form>
<?php
}
&GT?; `