如何使用PHP在MySQL数据库中输入日期

时间:2019-04-01 15:56:14

标签: php html mysql sql insert

好,所以我已经有一些插入脚本在工作,但是当我尝试使用此脚本时,什么也没发生。我没有收到错误或任何错误,它只是不会插入数据库。我有一种感觉,它的日期参数,但是不确定如何使其可识别?

<?php //SETTING SESSION VARIABLES FOR ROUND 1 TEAMS AND SCORE

session_start();

  if (isset($_POST['go1'])) { // MATCHUP 1

    include_once 'dbcon.php';

    $_SESSION['t_team1'] = $_POST['team-1'];
    $_SESSION['t_team2'] = $_POST['team-2'];
    $_SESSION['s_score1'] = $_POST['score-1'];
    $_SESSION['s_score2'] = $_POST['score-2'];

    $team1winner = mysqli_real_escape_string($conn, $_POST['team-1']);
    $team2winner = mysqli_real_escape_string($conn, $_POST['team-2']);
    $date1 = mysqli_real_escape_string($conn, $_POST['date-1']);

    $sql = "INSERT INTO knockout (knockout_team1, knockout_team2, knockout_date)
    VALUES ('$team1winner', '$team2winner', '$date1');";

    header("Location: ../tables.php?tables=winner");
}

//日期html

<input type="date" name"date-1" value="date" class="date">

2 个答案:

答案 0 :(得分:1)

首先,您将SQL命令放入字符串中,但实际上并没有执行该命令。

第二,如果您正在执行命令但未在此处显示该命令并且您的int partition(int low, int high, int arr[]) { int pivot = arr[low]; int i = low; for (int j = low + 1; j <= high; ++j) { if (arr[j] < pivot) { ++i; swap(arr[j], arr[i]); } } swap(arr[low], arr[i]); return i; } void quickSort(int low, int high, int arr[]) { if (low < high) { int pivot = partition(low, high, arr); quickSort(low, pivot, arr); quickSort(pivot + 1, high, arr); } } 文件正常工作,则很可能是日期格式问题。

最后,您需要在准备好的语句中执行所有命令,尤其是dbcon.php命令,以防止SQL注入绞车非常重要。

这是您的代码的外观:

INSERT
  

<?php //SETTING SESSION VARIABLES FOR ROUND 1 TEAMS AND SCORE session_start(); if (isset($_POST['go1'])) { // MATCHUP 1 include_once 'dbcon.php'; $_SESSION['t_team1'] = $_POST['team-1']; $_SESSION['t_team2'] = $_POST['team-2']; $_SESSION['s_score1'] = $_POST['score-1']; $_SESSION['s_score2'] = $_POST['score-2']; $team1winner = mysqli_real_escape_string($conn, $_POST['team-1']); $team2winner = mysqli_real_escape_string($conn, $_POST['team-2']); $date1 = mysqli_real_escape_string($conn, $_POST['date-1']); $TeamsStat = $conn->prepare("INSERT INTO knockout (knockout_team1, knockout_team2, knockout_date) VALUES (?, ?, ?)"); $TeamsStat->bind_param("sss", $team1winner, $team2winner, $date1); $TeamsStat->execute(); $TeamsStat->close(); header("Location: ../tables.php?tables=winner"); } 是数据库连接的对象。

由于准备好的语句不支持日期类型,并且由于日期不是自由输入值,因此$conn列应为字符串,变量knockout_date也应为字符串。

希望对您有帮助。

答案 1 :(得分:0)

HTML中的日期格式为dd-mm-yyyy,但是在将其插入数据库时​​,需要确保其YYYY-mm-dd类型。因此,请确保在插入之前进行转换

Ex:

interface TreeNodeProps {
    id: string
    title: string
}

const TreeNode: React.SFC<TreeNodeProps > = (props) => {
  return (
    <div key={props.id}>
        <div>{props.title}</div>
        <div>{props.children}</div>
    </div>
  )
}

const TreeView: React.SFC<{treeData: any, TreeNode: JSX.Element}> = (props) => {
  // here's the logic where each TreeNode's props will be fed with graph's props.
    const buildTreeRecursively = (treeData) =>
        treeData.map((node) => {                
            if (node.children.length > 0) {
                return (
                    <TreeNode id={node.id} title={node.title}>
                        {buildTreeRecursively(node.children)}
                    </TreeNode>
                )
            }
            return <TreeNode id={node.id} title={node.title} />
        })

  return (
        <div className={styles.treeView}>
            {buildTreeRecursively(props.treeData)}
        </div>
  )
}

或者通过手册查找替代功能进行转换