2017-09-14 47 views
1

我有这种形式更新数据库中的记录。我应该输入十进制数字,它完成它的工作并更新数据库。问题是它只能读取整个数字。在数据库上设置十进制但仍然只读取整数

实施例:

2.35(更新值变为2)

我也没有设置sickleave和vacationleave的类型在我的数据库为十进制。可能是什么问题?我做错了吗?

这里是我的代码:

<?php 
 
/* 
 
Allows the user to both create new records and edit existing records 
 
*/ 
 

 
// connect to the database 
 
include("connect-db.php"); 
 

 
// creates the new/edit record form 
 
// since this form is used multiple times in this file, I have made it a function that is easily reusable 
 
function renderForm($fullname = '', $username ='', $error = '', $id = '', $email = '', $address = '', $contact = '', $gender = '', $password = '', $user_levels = '', $date = '', $picture = '', $sickleave = '', $vacationleave = '') 
 
{ ?> 
 
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 
 
<html> 
 
<head> 
 

 
\t <link href="https://fonts.googleapis.com/css?family=Questrial" rel="stylesheet"> 
 

 
<style> 
 

 
\t body { 
 
\t \t background-image: url("img/wood2.jpg"); 
 
\t } 
 
\t 
 
\t html { 
 
\t \t font-family: Questrial; 
 
\t } 
 
\t 
 
\t .main > div { 
 
\t \t position: center; 
 
\t } 
 

 
\t .register { 
 
\t width: 500px; 
 
\t margin: 10px auto; 
 
\t padding: 10px; 
 
\t border: 7px solid #285f9f; 
 
\t border-radius: 10px; 
 
\t font-family: Questrial; 
 
\t color: #444; 
 
\t background-color: #F0F0F0; 
 
\t box-shadow: 0 0 20px 0 #000000; 
 
\t } 
 
\t .register h3 { 
 
\t margin: 0 15px 20px; 
 
\t border-bottom: 2px solid #285f9f; 
 
\t padding: 5px 10px 5px 0; 
 
\t font-size: 1.1em; 
 
\t } 
 
\t .register div { 
 
\t margin: 0 0 15px 0; 
 
\t border: none; 
 
\t } 
 
\t .register label { 
 
\t display: inline-block; 
 
\t width: 25%; 
 
\t text-align: right; 
 
\t margin: 10px; 
 
\t } 
 
\t .register input[type=text], .register input[type=password] { 
 
\t width: 65%; 
 
\t font-family: "Lucida Grande", "Lucida Sans Unicode", Tahoma, Sans-Serif; 
 
\t padding: 5px; 
 
\t font-size: 0.9em; 
 
\t border-radius: 5px; 
 
\t background: rgba(0, 0, 0, 0.07); 
 
\t } 
 
\t .register input[type=text]:focus, .register input[type=password]:focus { 
 
\t background: #FFFFFF; 
 
\t } 
 
\t .register .button { 
 
\t font-size: 1em; 
 
\t font-family: Questrial; 
 
\t border-radius: 8px; 
 
\t padding: 10px; 
 
\t border: 1px solid #285f9f; 
 
\t background: #285f9f; 
 
\t 
 
\t } 
 
\t .register .button:hover { 
 
\t background: #51DB1C; 
 
\t background: -webkit-linear-gradient(#51DB1C, #6BA061); 
 
\t background: -moz-linear-gradient(#51DB1C, #6BA061); 
 
\t background: -o-linear-gradient(#51DB1C, #6BA061); 
 
\t background: linear-gradient(#51db1c, #6ba061); 
 
\t } 
 
\t .register .sep { 
 
\t border: 1px solid #72B372; 
 
\t position: relative; 
 
\t margin: 35px 20px; 
 
\t } 
 
\t .register .or { 
 
\t position: absolute; 
 
\t width: 50px; 
 
\t left: 50%; 
 
\t background: #F0F0F0; 
 
\t text-align: center; 
 
\t margin: -10px 0 0 -25px; 
 
\t line-height: 20px; 
 
\t } 
 
\t .register .connect { 
 
\t width: 400px; 
 
\t margin: 0 auto; 
 
\t text-align: center; 
 
\t } 
 
\t 
 
</style> 
 

 
<title> <?php if ($id != '') { echo "Edit Record"; } else { echo "New Record"; } ?> </title> 
 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 
 

 
</head> 
 
<body> 
 

 
\t <?php if ($error != '') { 
 
\t echo "<div style='padding:4px; border:1px solid red; color:red'>" . $error 
 
\t . "</div>"; 
 
\t } ?> 
 

 
<div class="main"> 
 
\t <div class="one"> 
 
    <div class="register"> 
 
     <center><h3>Edit Account #</h3></center> 
 
     <form action="" method="post"> 
 
\t \t <div> 
 
      <label>ID</label> 
 
\t \t <?php if ($id != '') { ?> 
 
\t \t <?php echo $id;?> 
 
      <input type="hidden" name="id" value="<?php echo $id;?>"> 
 
\t \t <?php } ?> 
 
     </div> 
 
     <div> 
 
      <label>Fullname</label> 
 
      <input type="text" name="fullname" value="<?php echo $fullname;?>"> 
 
     </div> 
 
     <div> 
 
      <label>Email</label> 
 
      <input type="text" name="email" value="<?php echo $email; ?>"> 
 
     </div> 
 
     <div> 
 
      <label>Username</label> 
 
      <input type="text" name="username" value="<?php echo $username; ?>"> 
 
     </div> 
 
     <div> 
 
      <label>Password</label> 
 
      <input type="password" name="password" value="<?php echo $password; ?>"> 
 
     </div> 
 
     <div> 
 
      <label>Address</label> 
 
      <input type="text" name="address" value="<?php echo $address; ?>"> 
 
     </div> 
 
\t \t <div> 
 
      <label>Contact</label> 
 
      <input type="text" name="contact" value="<?php echo $contact; ?>"> 
 
     </div> 
 
\t \t <div> 
 
      <label>Gender</label> 
 
      <input type="text" name="gender" value="<?php echo $gender; ?>"> 
 
     </div> 
 
\t \t <div> 
 
      <label>User Levels</label> 
 
      <input type="text" name="user_levels" value="<?php echo $user_levels; ?>"> 
 
     </div> 
 
\t \t <div> 
 
      <label>Date</label> 
 
      <input type="text" name="date" value="<?php echo $date; ?>"> 
 
     </div> 
 
\t \t <div> 
 
      <label>Picture</label> 
 
      <?php echo $picture; ?> 
 
     </div> 
 
\t \t <div> 
 
      <label>Sick Leave</label> 
 
      <input type="text" name="sickleave" value="<?php echo $sickleave; ?>"> 
 
     </div> 
 
\t \t <div> 
 
      <label>Vacation Leave</label> 
 
      <input type="text" name="vacationleave" value="<?php echo $vacationleave; ?>"> 
 
     </div> 
 
     <div> 
 
      <label></label> 
 
\t \t <input type="submit" name="submit" value="Save Changes" class="button" /> 
 
\t \t <a href="view.php"><input type="button" value="View Records" class="button" /> 
 
     </div> 
 
     </form> 
 
    </div> 
 
    </div> 
 
\t 
 
</body> 
 
</html> 
 

 
<?php } 
 

 
/* EDIT RECORD */ 
 
// if the 'id' variable is set in the URL, we know that we need to edit a record 
 
if (isset($_GET['id'])) 
 
{ 
 
// if the form's submit button is clicked, we need to process the form 
 
if (isset($_POST['submit'])) 
 
{ 
 
// make sure the 'id' in the URL is valid 
 
if (is_numeric($_POST['id'])) 
 
{ 
 
// get variables from the URL/form 
 
\t $id = $_POST['id']; 
 
\t $fullname = htmlentities($_POST['fullname'], ENT_QUOTES); 
 
\t $username = htmlentities($_POST['username'], ENT_QUOTES); 
 
\t $email = htmlentities($_POST['email'], ENT_QUOTES); 
 
\t $address = htmlentities($_POST['address'], ENT_QUOTES); 
 
\t $contact = htmlentities($_POST['contact'], ENT_QUOTES); 
 
\t $gender = htmlentities($_POST['gender'], ENT_QUOTES); 
 
\t $password = htmlentities($_POST['password'], ENT_QUOTES); 
 
\t $user_levels = htmlentities($_POST['user_levels'], ENT_QUOTES); 
 
\t $date = htmlentities($_POST['date'], ENT_QUOTES); 
 
\t $picture = htmlentities($_POST['picture'], ENT_QUOTES); 
 
\t $sickleave = htmlentities($_POST['sickleave'], ENT_QUOTES); 
 
\t $vacationleave = htmlentities($_POST['vacationleave'], ENT_QUOTES); 
 

 
// check that firstname and lastname are both not empty 
 
if ($fullname == '' || $username == '' || $email == '' || $address == '') 
 
{ 
 
// if they are empty, show an error message and display the form 
 
\t $error = 'ERROR: Please fill in all required fields!'; 
 
\t renderForm($fullname, $username, $error, $id, $email, $address, $contact, $gender, $password, $user_levels, $date, $picture, $sickleave, $vacationleave); 
 
} 
 
else 
 
{ 
 
// if everything is fine, update the record in the database 
 
if ($stmt = $mysqli->prepare("UPDATE signup_and_login_users_table SET fullname = ?, username = ?, email = ?, 
 
address = ?, 
 
contact = ?, 
 
gender = ?, 
 
password = ?, 
 
user_levels = ?, 
 
date = ?, 
 
picture = ?, 
 
sickleave = ?, 
 
vacationleave = ? 
 
WHERE id = ?")) 
 
{ 
 
    $stmt->bind_param("ssssssssssiii", $fullname, $username, $email, $address, $contact, $gender, $password, $user_levels, $date, $picture, $sickleave, $vacationleave, $id); 
 
    $stmt->execute(); 
 
    $stmt->close(); 
 
} 
 
// show an error message if the query has an error 
 
else 
 
{ 
 
echo "ERROR: could not prepare SQL statement."; 
 
} 
 

 
// redirect the user once the form is updated 
 
header("Location: view.php"); 
 
} 
 
} 
 
// if the 'id' variable is not valid, show an error message 
 
else 
 
{ 
 
echo "Error!"; 
 
} 
 
} 
 
// if the form hasn't been submitted yet, get the info from the database and show the form 
 
else 
 
{ 
 
// make sure the 'id' value is valid 
 
if (is_numeric($_GET['id']) && $_GET['id'] > 0) 
 
{ 
 
// get 'id' from URL 
 
\t $id = $_GET['id']; 
 

 
// get the recod from the database 
 
if($stmt = $mysqli->prepare("SELECT * FROM signup_and_login_users_table WHERE id=?")) 
 
{ 
 
\t $stmt->bind_param("i", $id); 
 
\t $stmt->execute(); 
 

 
\t $stmt->bind_result($id, $fullname, $username, $email, $address, $contact, $gender, $password, $user_levels, $date, $picture, $sickleave, $vacationleave); 
 
\t $stmt->fetch(); 
 

 
// show the form 
 
renderForm($fullname, $username, NULL, $id, $email, $address, $contact, $gender, $password, $user_levels, $date, $picture, $sickleave, $vacationleave); 
 

 
$stmt->close(); 
 
} 
 
// show an error if the query has an error 
 
else 
 
{ 
 
\t echo "Error: could not prepare SQL statement"; 
 
} 
 
} 
 
// if the 'id' value is not valid, redirect the user back to the view.php page 
 
else 
 
{ 
 
\t header("Location: view.php"); 
 
} 
 
} 
 
} 
 

 

 

 
/* 
 

 
NEW RECORD 
 

 
*/ 
 
// if the 'id' variable is not set in the URL, we must be creating a new record 
 
else 
 
{ 
 
// if the form's submit button is clicked, we need to process the form 
 
if (isset($_POST['submit'])) 
 
{ 
 
// get the form data 
 
\t $fullname = htmlentities($_POST['fullname'], ENT_QUOTES); 
 
\t $username = htmlentities($_POST['username'], ENT_QUOTES); 
 
\t $email = htmlentities($_POST['email'], ENT_QUOTES); 
 
\t $address = htmlentities($_POST['address'], ENT_QUOTES); 
 
\t $contact = htmlentities($_POST['contact'], ENT_QUOTES); 
 
\t $gender = htmlentities($_POST['gender'], ENT_QUOTES); 
 
\t $password = htmlentities($_POST['password'], ENT_QUOTES); 
 
\t $user_levels = htmlentities($_POST['user_levels'], ENT_QUOTES); 
 
\t $date = htmlentities($_POST['date'], ENT_QUOTES); 
 
\t $picture = htmlentities($_POST['picture'], ENT_QUOTES); 
 
\t $sickleave = htmlentities($_POST['sickleave'], ENT_QUOTES); 
 
\t $vacationleave = htmlentities($_POST['vacationleave'], ENT_QUOTES); 
 

 
// check that firstname and lastname are both not empty 
 
if ($fullname == '' || $username == '' || $email == '' || $address == '') 
 
{ 
 
// if they are empty, show an error message and display the form 
 
$error = 'ERROR: Please fill in all required fields!'; 
 
renderForm($fullname, $username, $error, $email, $address, $contact, $gender, $password, $user_levels, $date, $picture, $sickleave, $vacationleave); 
 
} 
 
else 
 
{ 
 
// insert the new record into the database 
 
if ($stmt = $mysqli->prepare("INSERT signup_and_login_users_table (fullname, username, email, address, contact, gender, password, user_levels, date, picture, sickleave, vacationleave) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) 
 
{ 
 
$stmt->bind_param("ssssssssssiii", $fullname, $username, $email, $address, $contact, $gender, $password, $user_levels, $date, $picture, $sickleave, $vacationleave, $id); 
 
$stmt->execute(); 
 
$stmt->close(); 
 
} 
 
// show an error if the query has an error 
 
else 
 
{ 
 
echo "ERROR: Could not prepare SQL statement."; 
 
} 
 

 
// redirec the user 
 
header("Location: view.php"); 
 
} 
 

 
} 
 
// if the form hasn't been submitted yet, show the form 
 
else 
 
{ 
 
renderForm(); 
 
} 
 
} 
 

 
// close the mysqli connection 
 
$mysqli->close(); 
 
?>

Table Schema

+0

可以尝试只显示相关的代码吗? – CarlosCarucce

+0

表模式将在这里非常有用... – ThoriumBR

+0

@ThoriumBR添加表架构 –

回答

1

您可以利用iintegers结合的数字,所以地板他们。尝试结合与d双打因此它保持了小数:

$stmt->bind_param("ssssssssssddi", $fullname, $username, $email, $address, $contact, $gender, $password, $user_levels, $date, $picture, $sickleave, $vacationleave, $id); 

此外,你必须DECIMAL(11, 0)的数据类型。第二个数字是0意味着你将有0小数。尝试将sickleavevacationleave设置为DECIMAL(11, 2)以存储2位小数。

+0

没有工作:(我应该改变我的数据库类型要加倍吗? –

+1

将表格模式添加到原始文章中,以便我们可以看到它 – ishegg

+0

添加了表格模式 –