我是php新手并创建了一个个人网站,但我遇到了将数据插入数据库的问题。
这是我的sql表:
CREATE TABLE IF NOT EXISTS `TBL_VIDEOS` (
`VID` varchar(15) NOT NULL DEFAULT '0',
`VNAME` varchar(255) DEFAULT NULL,
`VDATE` date DEFAULT NULL,
`VTYPE` varchar(15) DEFAULT NULL,
`VGENRE` varchar(255) DEFAULT NULL,
`VSTATUS` varchar(20) DEFAULT NULL,
`VWIDTH` varchar(5) DEFAULT NULL,
`VHEIGHT` varchar(5) DEFAULT NULL,
`VLENGHT` time DEFAULT NULL,
`VEXTENSION` varchar(10) DEFAULT NULL,
`VSIZE` varchar(15) DEFAULT NULL,
`VSERIES` varchar(50) DEFAULT NULL,
`VLANGUAGE` varchar(30) DEFAULT NULL,
`VLINK` varchar(255) DEFAULT NULL,
PRIMARY KEY (`VID`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1;
这是我的表单和php代码
<?php
require 'connections/connections.php';
?>
<?php
session_start();
if(isset($_SESSION["UID"])) {
} else {
header('Location: index.php');
}
?>
<?php
if(mysqli_connect_errno()){
echo "failed".mysqli_connect_error();
} else {
if(isset($_POST['VideoInsert'])) {
session_start;
$VNAME = $_POST['VNAME'];
$VDATE = $_POST['VDATE'];
$VTYPE = $_POST['VTYPE'];
$VGENRE = $_POST['VGENRE'];
$VSTATUS = $_POST['VSTATUS'];
$VWIDTH = $_POST['VWIDTH'];
$VHEIGHT = $_POST['VHEIGHT'];
$VLENGTH = $_POST['VLENGTH'];
$VEXTENSION = $_POST['VEXTENSION'];
$VSIZE = $_POST['VSIZE'];
$VSERIES = $_POST['VSERIES'];
$VLANGUAGE = $_POST['VLANGUAGE'];
$VLINK = $_POST['VLINK'];
mysqli_query($database,"INSERT INTO TBL_VIDEOS (VNAME, VDATE, VTYPE, VGENRE, VSTATUS, VWIDTH, VHEIGHT, VLENGTH, VEXTENSION, VSIZE, VSERIES, VLANGUAGE, VLINK)Values('{$VNAME}', '{$VDATE}', '{$VTYPE}', '{$VGENRE}', '{$VSTATUS}', '{$VWIDTH}', '{$VHEIGHT}', '{$VLENGTH}', '{$VEXTENSION}', '{$VSIZE}', '{$VSERIES}', '{$VLANGUAGE}', '{$VLINK}')") or die(mysqli_error($database));
header('Location: enter_videos.php');
}
}
?>
<!doctype html>
<html>
<head>
<link href="css/master.css" rel="stylesheet" type="text/css" />
<link href="css/menu.css" rel="stylesheet" type="text/css" />
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Videos Archive</title>
</head>
<body>
<div class="Container">
<div class="Header"></div>
<div class="Menu"></div>
<div class="LeftBody">
<form action="" method="post" name="Insert_Videos" id="Insert_Videos">
<div class="FormElement">
<input name="VNAME" type="text" required="required" class="TField" id="VNAME" placeholder="Video Name">
</div>
<div class="FormElement">
<input name="VDATE" type="text" required="required" class="TField" id="VDATE" placeholder="Release Date">
</div>
<div class="FormElement">
<input name="VTYPE" type="text" required="required" class="TField" id="VTYPE" placeholder="Type">
</div>
<div class="FormElement">
<input name="VGENRE" type="text" required="required" class="TField" id="VGENRE" placeholder="Genre">
</div>
<div class="FormElement">
<input name="VSTATUS" type="text" required="required" class="TField" id="VSTATUS" placeholder="Status">
</div>
<div class="FormElement">
<input name="VWIDTH" type="text" required="required" class="TField" id="VWIDTH" placeholder="Width">
</div>
<div class="FormElement">
<input name="VHEIGHT" type="text" required="required" class="TField" id="VHEIGHT" placeholder="Height">
</div>
<div class="FormElement">
<input name="VLENGTH" type="text" required="required" class="TField" id="VLENGTH" placeholder="Length">
</div>
<div class="FormElement">
<input name="VEXTENSION" type="text" required="required" class="TField" id="VEXTENSION" placeholder="Extension">
</div>
<div class="FormElement">
<input name="VSIZE" type="text" required="required" class="TField" id="VSIZE" placeholder="Size">
</div>
<div class="FormElement">
<input name="VSERIES" type="text" required="required" class="TField" id="VSERIES" placeholder="Series">
</div>
<div class="FormElement">
<input name="VLANGUAGE" type="text" required="required" class="TField" id="VLANGUAGE" placeholder="Language">
</div>
<div class="FormElement">
<input name="VLINK" type="text" required="required" class="TField" id="VLINK" placeholder="Link">
</div>
<div class="FormElement">
<input name="InsertVideos" type="submit" class="Button" id="InsertVideos" value="Send Data">
</div>
</form>
</div>
<div class="RightBody"></div>
<div class="Footer"></div>
</div>
我认为问题在于日期和时间。
答案 0 :(得分:1)
放session_start()
;在<?php
标记之后的开头。
变化:
If(isset($_POST['VideoInsert']){
session_start; //this is invalid
为:
If(isset($_POST['InsertVideos'])
因为你正在检查 在session_start
<input name="InsertVideos" type="submit" class="Button" id="InsertVideos" value="Send Data">
您的代码容易受到SQL Injection / XSS的攻击。</ p>
修改强>
尝试删除header();
和会话,然后试一试,例如:
<?php
require ('connections/connections.php');
//我假设您已完成所有设置,包括正确的$database
if(mysqli_connect_errno()){
echo "failed".mysqli_connect_error();
} else {
if(isset($_POST['InsertVideos'])) {
$VNAME = $_POST['VNAME'];
$VDATE = $_POST['VDATE'];
$VTYPE = $_POST['VTYPE'];
$VGENRE = $_POST['VGENRE'];
$VSTATUS = $_POST['VSTATUS'];
$VWIDTH = $_POST['VWIDTH'];
$VHEIGHT = $_POST['VHEIGHT'];
$VLENGTH = $_POST['VLENGTH'];
$VEXTENSION = $_POST['VEXTENSION'];
$VSIZE = $_POST['VSIZE'];
$VSERIES = $_POST['VSERIES'];
$VLANGUAGE = $_POST['VLANGUAGE'];
$VLINK = $_POST['VLINK'];
mysqli_query("INSERT INTO TBL_VIDEOS (VNAME, VDATE, VTYPE, VGENRE, VSTATUS, VWIDTH, VHEIGHT, VLENGTH, VEXTENSION, VSIZE, VSERIES, VLANGUAGE, VLINK) VALUES ('$VNAME', '$VDATE', '$VTYPE', '$VGENRE', '$VSTATUS', '$VWIDTH', '$VHEIGHT', '$VLENGTH', '$VEXTENSION', '$VSIZE}', '$VSERIES', '$VLANGUAGE', '$VLINK')") or die(mysqli_error($database)); //mysqli_error($connection); which means it requires a connection to be passed in as a parameter, make sure you're not messing up there.
}
}
?>
尝试将DATE列更改为varchar / text并查看是否有帮助 因为您将表格中的日期作为type =&#34; text&#34;
答案 1 :(得分:0)
我认为你没有一个字段可以获得$ _POST [&#39; VideoInsert&#39;]的价值),它应该是$ _POST [&#39; InsertVideos&#39;]