我试过制作以下代码。我想将日期,日期,时间,全天候添加到我的数据库中。但是当我选择表单中的值时,我的数据库中没有注册任何内容。它与mysql连接成功。
代码在这里:
<html>
<head>
</head>
<body>
<form method="post">
<h3>Add your worktime to database</h3><br>
<input type="date" name="date"><br><br>
<input type="text" name="day"><br>
<input type="time" name="fromtime">
<input type="time" name="totime">
<input type="submit" value="submit"><br><br>
</form>
</body>
<?php
$username = "root";
$password = "root";
$hostname = "127.0.0.1:3306";
//connection to the database
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br><br>";
//select a database to work with
$selected = mysql_select_db("danskebank",$dbhandle)
or die("Could not select examples");
$date = $_POST['date'];
$day = $_POST['day'];
$fromtime = $_POST['fromtime'];
$totime = $_POST['totime'];
$sql = "INSERT INTO addWorkTime(date, day, fromtime, totime) VALUES('$date', '$day', '$fromtime', 'totime')";
//execute the SQL query and return records
$result = mysql_query("SELECT date, day, fromtime, totime FROM addWorkTime");
//fetch tha data from the database
while ($row = mysql_fetch_array($result)) {
Print"<h3>Return from database:</h3>";
echo "Date: ".$row{'date'}."<br>"."Day: ".$row{'day'}."<br>"."From Time: ".$row{'fromtime'}."<br>"."To Time: ".$row{'totime'}; //display the results
}
?>
</html>
我希望有人可以帮助我?
最诚挚的问候Mads
答案 0 :(得分:2)
RoyalBG是正确的,您需要在代码中的行$result = mysql_query($sql);
正确运行插入行。此外,您应该阅读SQL注入,因为您当前编写的代码极易受到SQL注入攻击。