php - 返回并添加到数据库

标签 php mysql

我尝试编写以下代码。我想将日期、日期、从时间、到时间添加到我的数据库中。但是当我选择表单中的值时,我没有在数据库中注册任何内容。至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>

Database Structure

我希望有人能帮助我?

最诚挚的问候麦兹

最佳答案

RoyalBG 是正确的,您需要在代码中的正确位置添加行 $result = mysql_query($sql); 才能实际运行插入行。此外,您还应该阅读有关 SQL 注入(inject)的内容,因为您目前编写的代码极易受到 SQL 注入(inject)的攻击。<​​/p>

关于php - 返回并添加到数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29335269/

相关文章:

php - 如何用星号替换某些字符

php - NULL 分配给 mysql_fetch_array 输出?

php - 将数据库中的数据显示到下拉菜单 CodeIgniter

php - 如何从 id 回显数据

PHP 脚本超时耗时太长

mysql - 在 MySQL 中更新 View

mysql - 在 TEXT 数据类型 mysql 中连接一个字符串

mysql - 从子内连接选择引用父字段

php - 警告 : file_get_contents: failed to open stream: Redirection limit reached, 中止

php - Laravel 如何从 3 个表中获取数据