使用PHP访问SQL数据库

时间:2014-12-14 16:54:04

标签: php mysql

我有一个简单的消息和用户数据库。消息包含id,消息和创建日期。我试图在人们点击提交消息之后将这些放入数据库但我得到了一些问题,无论是谁投入,每次都给我0,0-0-0为日期创建。 我正在使用的两个代码文件就是这个。

<?php
    session_start();

    include "validate.php";
    include "src/header.php";
    include "src/mainmenu.php";
?>
    <form method="post" action="message_action.php">
    <fieldset>
    <legend>Message Area</legend>
    <p>
        <label for="message">Message:</label> <textarea name="message" id="message" rows="5" cols="50"></textarea>

    <p class="center"><input value="Send" type="submit" /></p>
    </fieldset>
    </form>

    <p><a href="exit.php">Exit</a></p>
<?php   
    include "src/footer.php";
?>

<?php
    session_start();
    include 'db.php';

    $id_user = $_SESSION['id']; 
    $message = $_POST['message'];

    $sql = "INSERT INTO messages (id_user, message) VALUES ('$id_user','$message');";
    $result = mysqli_query($link, $sql);
    mysqli_close($link);

    if ($result == false) {
        include "src/header.php";
        include "src/mainmenu.php";
        echo '<p>Error: cannot execute query</p>';
        echo '<p><a href="register.php">Try again</a></p>';
        include "src/footer.php";
        exit;
    }
    else {
        header('Location: private.php');
    }
    mysqli_close($link);
?>

创建日期现在有效,但仍然是获取id_user的问题。

数据库

-- phpMyAdmin SQL Dump
-- version 3.4.5
-- http://www.phpmyadmin.net
--
-- Servidor: localhost:33306
-- Tiempo de generación: 31-05-2012 a las 09:56:35
-- Versión del servidor: 5.5.16
-- Versión de PHP: 5.3.8

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

--
-- Base de datos: `minifacebook`
--
CREATE DATABASE `minifacebook2` DEFAULT CHARACTER SET latin1 COLLATE latin1_swedish_ci;
USE `minifacebook2`;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `messages`
--

CREATE TABLE IF NOT EXISTS `messages` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `id_user` int(11) NOT NULL,
  `message` text NOT NULL,
  `date_of_creation` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

-- --------------------------------------------------------

--
-- Estructura de tabla para la tabla `users`
--

CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(200) NOT NULL,
  `info` text NOT NULL,
  `date_of_birth` date NOT NULL,
  `place_of_birth` varchar(20) NOT NULL,
  `date_of_reg` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `email` varchar(200) NOT NULL,
  `password` varchar(32) NOT NULL,
  `nationality` varchar(20) NOT NULL,
  `friend` int(3),
  PRIMARY KEY (`id`),
  UNIQUE KEY `email` (`email`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=23 ;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;

0 个答案:

没有答案