我试图使用php文件上的提交按钮向我的数据库中添加一个条目。我添加的表名为 - Fixture,包含:
FixtureID - int PK AUTO_INCREMENT
Competition - varchar
TeamID - int
Against - varchar
Date - date
Time - time
HomeScore - int
AwayScore - int
php的代码如下。
<div id="container">
<div id="content">
<h2>Add New fixture</h2>
<hr />
<form action = "new_fixture.php" method="post" >
<p>Add Fixture :</p><br />
<input type="text" name="comp" size="25" placeholder="competition" />
<br />
<br />
<input type="text" name="vs" size="25" placeholder="against" />
<br />
<br />
<input type="date" name="date" size="25" placeholder="date" />
<br />
<br />
<input type="time" name="time" size="25" placeholder="time" />
<br />
<br />
<input type ="submit" name="updateinfo" id="updateinfo" value="Add player">
</form>
</div>
<div>
<?php
if(isset($_POST['submit'])) {
$query = mysql_query("INSERT INTO Fixture VALUES ('','$comp','9','$vs','$date','$time','','')");
if(!$sql) {
echo ("Could not delete rows" .mysql_error());
}
}
?>
任何想法如何解决这个问题都没有添加到数据库
由于
答案 0 :(得分:0)
更改此
if(isset($_POST['submit'])) {
到
if(isset($_POST['updateinfo'])) {
答案 1 :(得分:0)
使用NULL
作为主键而不是空字符串''
,并使用int
列的数字:
$query = mysql_query("INSERT INTO Fixture VALUES(NULL,'$comp',9,'$vs','$date','$time',0,0)");
编辑关于日期/时间格式的评论中的问题:
$query = mysql_query("INSERT INTO Fixture VALUES(NULL,'$comp',9,'$vs',STR_TO_DATE('$date', '%m/%d/%Y')),STR_TO_DATE('$time','%h:%i'),0,0)");
答案 2 :(得分:0)
在此代码中:
<?php
if(isset($_POST['submit'])) {
$query = mysql_query("INSERT INTO Fixture VALUES ('','$comp','9','$vs','$date','$time','','')");
if(!$sql) {
echo ("Could not delete rows" .mysql_error());
}
}
?>
您检查是否有名为submit sent的POST参数。 您有三种可能的修复方法:
检查是否发送了提交按钮: 变化:
if(isset($ _ POST [&#39; submit&#39;])){
with:
if(isset($_POST['updateinfo'])) {
由于您的输入按钮名称为 updateinfo
将提交按钮的名称更改为提交
检查是否为空($ _ POST)
答案 3 :(得分:0)
试试这个 - 不是专业人士,但我可能会工作。
<?php
if(isset($_POST['updateinfo'])) {
$comp = $_POST['comp'];
$vs = $_POST['vs'];
$date = $_POST['date'];
$time = $_POST['time'];
$query = "INSERT INTO Fixture SET Competition='$comp', Against='$vs', Date='$date', Time='$time' ";
if ($mysqli->query($query)) {
$message = "It worked - Post inserted.";
echo "<script type='text/javascript'>alert('$message');</script>";
}else{
echo 'something went wrong';
}
}