我遇到了 $homework_date 在 MySQL 中显示为 0000-00-00 的问题。我不知道为什么……我一遍又一遍地写代码,找不到错误。
这是表格。
<SCRIPT language=Javascript>
<!--
function isNumberKey(evt)
{
var charCode = (evt.which) ? evt.which : event.keyCode;
if (charCode != 46 && charCode > 31
&& (charCode < 48 || charCode > 57))
return false;
return true;
}
//-->
</SCRIPT>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
<script src="http://code.jquery.com/jquery-1.9.1.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script type="text/javascript"></script>
<script>
$(function() {
$( "#datepicker" ).datepicker({
altField: "#alt-datepicker",
altFormat: "yy-mm-dd",
dateFormat: "DD, MM d, yy"
});
});
</script>
<?php
require_once('/home/jeremyohmann/www/projects/homework/classes/homework.php');
$homework = new Homework();
$con=mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"Select
classes.class_name,
classes.class_id
From
classes
Where
classes.user_id = '".$_SESSION['user_id']."'");
echo '<p>Add homework to one of your classes below.</p><br />';
echo "<form method='post' action=''>";
echo "<select name='AddHomeworkClass'>";
echo "<option value='' >Select Class</option>";
while($row = mysqli_fetch_array($result))
{
$value=$row['class_name'];
$id =$row['class_id'];
echo "<option value='" . $id . "'>" . $value . "</option>";
}
echo "</select><br />";
echo "Homework Name: <input type='text' name='AddHomeworkHomework' placeholder='Homework' /><br />";
echo "Total Points Avaliable for Assignment: <input type='text' name='AddHomeworkPointsAvaliable' placeholder='Points Avaliable' onkeypress='return isNumberKey(event)'/><br />";
echo "Points Earned: <input type='text' name='AddHomeworkPointsEarned' placeholder='Points Earned' onkeypress='return isNumberKey(event)'/><br />";
echo "Date: <input type='text' id='datepicker' placeholder='Date'/><br />";
echo "<input type='text' id='alt-datepicker' name='AddHomeworkDate' />";
echo "<input type='submit' name='AddHomework' value='Add Homework' />";
echo "</form>";
mysqli_close($con);
?>
这是处理表单的类。
<?php
class Homework {
private $db_connection =null; //Database connection
private $userID =""; //User's ID
private $class_id =""; //Class id this homework is from
private $homework_name =""; //Homework name
private $avaliable_points =""; //Total points that are avaliable on this homework
private $earned_points =""; //Score that the homework recieved
private $homework_date =""; //Date the homework was turned in
public $homework_add_successful =false;
public $errors =array(); //Stores error messages
public $messages =array(); //Stores other messages
public function __construct(){
if (isset($_POST['AddHomework'])) {
$this->userID = $_POST['user_id'];
$this->add_homework();
}
}
/**
* add_homework
*
* This will handle the whole process of adding homework.
*/
private function add_homework() {
if (empty($_POST['AddHomeworkClass'])) {
$this->errors[] = "Please select a class";
} elseif (empty($_POST['AddHomeworkHomework'])) {
$this->errors[] = "Please type in a name for your homework";
} elseif (empty($_POST['AddHomeworkPointsAvaliable'])) {
$this->errors[] = "Please type in points avaliable";
} elseif (empty($_POST['AddHomeworkPointsEarned'])) {
$this->errors[] = "Please type in points earned";
} elseif ($_POST['AddHomeworkPointsAvaliable'] <= 0) {
$this->errors[] = "You cannot have negative points avaliable";
} elseif ($_POST['AddHomeworkPointsEarned'] <= 0) {
$this->errors[] = "You cannot have negative points earned";
} elseif ($_POST['AddHomeworkDate'] = "") {
$this->errors[] = "Please select a date";
} else {
//Create database connection
$this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (!$this->db_connection->connect_errno) {
//Escape Characters
$this->class_id = $this->db_connection->real_escape_string($_POST['AddHomeworkClass']);
$this->homework_name = $this->db_connection->real_escape_string($_POST['AddHomeworkHomework']);
$this->avaliable_points = $this->db_connection->real_escape_string($_POST['AddHomeworkAvaliablePoints']);
$this->earned_points = $this->db_connection->real_escape_string($_POST['AddHomeworkEarnedPoints']);
$this->homework_date = $this->db_connection->real_escape_string($_POST['AddHomeworkDate']);
//Remove HTML tags
$this->class_id = strip_tags($this->class_name);
$this->homework_name = strip_tags($this->homework_name);
$this->avaliable_points = strip_tags($this->avaliable_points);
$this->earned_points = strip_tags($this->earned_points);
$this->homework_date = strip_tags($this->homework_date);
//cut down data
$this->class_id = substr($this->class_name, 0, 5);
$this->homework_name = substr($this->homework_name, 0, 64);
$this->avaliable_points = substr($this->avaliable_points, 0, 5);
$this->earned_points = substr($this->earned_points, 0, 5);
//Check if homework name already exists
$query_check_homework = $this->db_connection->query
("Select
homework.homework_name
From
classes Inner Join
homework On classes.class_id = homework.class_id
Where
homework.class_id = '".$this->class_id."' And
homework.homework_name = '".$this->homework_name."' And
classes.user_id = '".$this->user_id."'");
if ($query_check_homework->num_rows == 1) {
$this->errors[] = "Sorry, You already have homework in this class with that name. Please choose a different name.";
} else {
//write new homework into the datebase
$query_new_homework = $this->db_connection->query("INSERT INTO homework (class_id, homework_name, avaliable_points, earned_points, date) VALUES ('".$this->class_id."', '".$this->homework_name."', '".$this->avaliable_points."', '".$this->earned_points."', '".$this->homework_date."')");
if (query_new_homework) {
$this->messages[] = "Your homework has been successfully added to the database.";
$this->homework_add_successful = true;
} else {
$this->errors[] = "Sorry, your homework was not successfully added to the database. Please go back and try again.";
}
}
} else {
$this->errors[] = "Sorry. no database connection.";
print '<script type="text/javascript">';
print 'alert("Please enter a class name.")';
print '</script>';
}
}
if ($this->errors) {
foreach ($this->errors as $error) {
print '<script type="text/javascript">';
print 'alert("'.$error.'")';
print '</script>';
}
}
if ($this->messages) {
foreach ($this->messages as $message) {
print '<script type="text/javascript">';
print 'alert("'.$message.'")';
print '</script>';
}
}
}
}
?>