我尝试编写以下代码。我想将日期、日期、从时间、到时间添加到我的数据库中。但是当我选择表单中的值时,我没有在数据库中注册任何内容。至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>
我希望有人能帮助我?
最诚挚的问候麦兹
最佳答案
RoyalBG 是正确的,您需要在代码中的正确位置添加行 $result = mysql_query($sql);
才能实际运行插入行。此外,您还应该阅读有关 SQL 注入(inject)的内容,因为您目前编写的代码极易受到 SQL 注入(inject)的攻击。</p>
关于php - 返回并添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29335269/