我的任务是使用php+mysql将消息添加到数据库中。我用html创建了表单,并创建了php脚本,但什么都不起作用。它将我重定向到php文件,我没有看到任何东西,只有空白页。旅馆地址线
localhost/twitter/lib/addTweet.php
我的数据库表中没有任何更改。请帮我解决问题。非常感谢。
HTML
<form action="lib/addTweet.php" method="post">
<textarea class="m_twitt" name="t_message" placeholder=" Write your twitt there."/></textarea><br>
<input class="fright" type="submit" name="addTweet" value="Add"/>
</form>
PhP
<?
session_start();
include "connect.php";
if(!empty($_POST['t_message'])){
if(isset($_POST['addTweet'])){
$userId = $_SESSION['userId'];
$tweet = $_POST['t_message'];
// $curTime = date("Y-m-d H:i:s");
// $query = mysql_query("INSERT INTO `111212_tweets` (`user_id`,`text`,`pubdate`) VALUES ('$userId','$tweet','$curTime')",$connect) ;
$query = mysql_query("INSERT INTO `111212_tweets` (`user_id`,`text`) VALUES ('$userId', '$tweet'");
if (!$query){
die('Error: ' . mysql_error());
}else{
header('Location: ../main.php');
}
}
}else{
// header('Location: ../index.php');
echo 'KOLOBOK';
}
您放错了"
。正确的查询应该是这样的
("INSERT INTO `111212_tweets` (`user_id`,`text`) VALUES ('$userId', '$tweet')")
和
<?php ?>// you missed the php in your code
如果您的addTweet.php和html页面在同一目录或同一页面中,则
<form action="addTweet.php" method="post">
因为您正在执行action="lib/addTweet.php",所以这意味着它正在提交到addTweet.php,该文件位于当前html页面所在的lib文件夹中。
我在代码中看到的另一件事是没有在查询中添加结束括号")"。
$query = mysql_query("INSERT INTO `111212_tweets` (`user_id`,`text`) VALUES ('$userId', '$tweet');");
这是一个建议,如果出现问题,请告诉我们。