gpt4 book ai didi

php - Insert 只存储第一个数字,其余的被忽略

转载 作者:行者123 更新时间:2023-11-29 06:50:22 28 4
gpt4 key购买 nike

我对我的数据库有疑问。我开发了一个系统,当用户插入值时,它会自动计算百分比。我现在的问题是,我的数据库只存储了第一个数字。例如,如果我输入 4000,它只会存储 4,一开始我以为后面的 3 个零可能有问题,但事实并非如此,如果我输入 3245,它仍然只存储 3。

下面是我的数据库; enter image description here

这里是与该数据库相关的代码。

添加记录:

<form action="" method="post">

<div>

<?php if ($id != '') { ?>

<input type="hidden" name="id" value="<?php echo $id; ?>" />

<p>ID: <?php echo $id; ?></p>

<?php } ?>





<p><strong>Tarikh&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: *</strong>

<input type="text" style="text-transform:uppercase" name="date" value=" <?php echo $date; ?>"/><br/></p>

<p><strong>Di bawah 60 Minit : *</strong> <input type="text" name="casesolved_u"

value="<?php echo $casesolved_u; ?>"/><br/></p>

<p><strong>Jumlah kes &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;: *</strong> <input type="text" name="casesolved_a"

value="<?php echo $casesolved_a; ?>"/></p>



<!--Remove Percentage entry -->


<p>* required</p>

<input type="submit" name="submit" value="Submit" />

</div>

</form></center>



</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

$id = $_POST['id'];

$date = strtoupper($_POST['date']);

$casesolved_u = htmlentities($_POST['casesolved_u'], ENT_QUOTES);

$casesolved_a = htmlentities($_POST['casesolved_a'], ENT_QUOTES);



//check if empty

if ($date == '' || $casesolved_u == ''||$casesolved_a =='')

{

// if they are empty, show an error message and display the form

$error = 'ERROR: Please fill in all required fields!';

renderForm($date, $casesolved_u, $casesolved_a, $percentage ,$error, $id);

}

else

{

$percentage = ($casesolved_u * 100 / $casesolved_a);

//apply the proper formatting

$casesolved_u = number_format ($casesolved_u, 2);
$casesolved_a = number_format ($casesolved_a, 2);
$percentage = number_format ($percentage,2);



// if everything is fine, update the record in the database

if ($stmt = $mysqli->prepare("UPDATE ae SET date = ?, casesolved_u = ?, casesolved_a=?, percentage=?

WHERE id=?"))

{

$stmt->bind_param("ssssi", $date, $casesolved_u, $casesolved_a, $percentage ,$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

$id = $_GET['id'];



// get the recod from the database

if($stmt = $mysqli->prepare("SELECT id, date, casesolved_u, casesolved_a, percentage FROM ae WHERE id =?"))



{

$stmt->bind_param("i", $id);

$stmt->execute();



$stmt->bind_result($id, $date, $casesolved_u, $casesolved_a, $percentage);

$stmt->fetch();



// show the form

renderForm($date, $casesolved_u, $casesolved_a , $percentage, NULL, $id);



$stmt->close();

}

// show an error if the query has an error

else

{

echo "Error: could not prepare SQL statement";

}

}

// if the 'id' value is not valid, redirect the user back to the view.php page

else

{

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

$date = strtoupper($_POST['date']);

$casesolved_u = htmlentities($_POST['casesolved_u'], ENT_QUOTES);

$casesolved_a = htmlentities($_POST['casesolved_a'], ENT_QUOTES);



// check that no empty value inserted

if ($date == '' || $casesolved_u == '' ||$casesolved_a == '')

{

// if they are empty, show an error message and display the form

$error = 'ERROR: Please fill in all required fields!';

renderForm($date, $casesolved_u, $casesolved_a, $percentage, $error);

}

else

{


$percentage = ($casesolved_u * 100 / $casesolved_a);

//apply the proper formatting

$casesolved_u = number_format ($casesolved_u, 2);
$casesolved_a = number_format ($casesolved_a, 2);
$percentage = number_format ($percentage,2);


// insert the new record into the database

if ($stmt= $mysqli->prepare ("INSERT INTO ae (date, casesolved_u, casesolved_a, percentage) VALUES (?,?,?,?)"))

{

$stmt->bind_param("ssss",$date, $casesolved_u, $casesolved_a, $percentage);

$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();

?>

这是查看代码;

<?php
// connect to the database
include('connect-db.php');

// get the records from the database
if ($result = $mysqli->query("SELECT * FROM ae ORDER BY id"))
{
// display records if there are records to display
if ($result->num_rows > 0)
{
// display records in a table
echo "<table border='1' cellpadding='10'>";

// set table headers
echo "<tr><th>&nbsp Tarikh &nbsp</th><th> &nbsp Discaj Dalam Masa 2 Jam &nbsp </th><th> &nbsp Jumlah Kes &nbsp </th><th>Peratusan</th><th>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp</th><th>&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp</th></tr>";

while ($row = $result->fetch_object())
{
// set up a row for each record
echo "<tr>";

echo "<td>" . $row->date . "</td>";
echo "<td>" . $row->casesolved_u . "</td>";
echo "<td>" . $row->casesolved_a . "</td>";
echo "<td>" . $row->percentage . "</td>";
echo "<td><a href='records.php?id=" . $row->id . "'>Edit</a></td>";
echo "<td><a href='delete.php?id=" . $row->id . "'>Delete</a></td>";
echo "</tr>";
}

echo "</table>";
}
// if there are no records in the database, display an alert message
else
{
echo "Tiada maklumat untuk dipamerkan";
}
}
// show an error if there is an issue with the database query
else
{
echo "Error: " . $mysqli->error;
}

// close database connection
$mysqli->close();

?>
<p>
</p>
<center><a href="records.php"> Tambah Rekod Baru</a></center>
</div>

请指出我哪里做错了,导致我的数据库只将第一个数字存储到数据库中。

最佳答案

number_format 将逗号作为千位分隔符。如果数字 > 1,000(例如 4,000 或 3,245),那么对 MySQL(或大多数其他数据库)的任何插入都会困惑。 number_format 是人们轻松阅读信息所需要的,但数据库不需要。

关于php - Insert 只存储第一个数字,其余的被忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47745243/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com