简体   繁体   English

MySQL“插入”不起作用

[英]MySQL “INSERT” doesn't work

I am trying to create a PM system on my website however the sending does not work... 我正在尝试在我的网站上创建PM系统,但是发送不起作用...

Here is my code: 这是我的代码:

<?php
$from = $_POST['from'];
$to = $_POST['to'];
$content = $_POST['content'];
$date = date("Y-n-j H:i:s");
$id = rand(0000,9999);

include('sql_connect.php');
mysql_query("INSERT INTO pm (`from`, `to`, `content`, `date`, `id`) VALUES ('".$from."', '".$to."', '".$content."', '".$date."', '".$id."')");
mysql_close($con);
header('Location: members.php?sent');
?>

Can someone tell me why it doesn't work? 有人可以告诉我为什么它不起作用吗?

EDIT: I added backtits but the result is this: 编辑:我添加了backtits,但结果是这样的:

NULL 
Warning: Cannot modify header information - headers already sent by (output started at /home/quebecen/public_html/new/do_pm.php:10) in /home/quebecen/public_html/new/do_pm.php on line 15

EDIT 2: I added $result before que mysql_query() (I forgot it) and now I have this code: 编辑2:我在que mysql_query()之前添加了$ result(我忘记了),现在我有了以下代码:

<?php
$from = $_POST['from'];
$to = $_POST['to'];
$content = $_POST['content'];
$date = date("Y-n-j H:i:s");
$id = rand(0000,9999);

include('sql_connect.php');
$result = mysql_query("INSERT INTO pm (`from`, `to`, `content`, `date`, `id`) VALUES ('".$from."', '".$to."', '".$content."', '".$date."', '".$id."')", $con);

var_dump($result);

if(!$result) {
    echo mysql_error();
}

mysql_close($con);
header('Location: members.php?sent');
?>

Make sure the query isn't dying. 确保查询没有死。

$result = mysql_query("...");
var_dump($result); //If $result is false, then there was an error executing the query.

if(!$result) {
    echo mysql_error();
}

You might also not be using the correct connection. 您可能还没有使用正确的连接。 You might need to add $con to mysql_query . 您可能需要将$con添加到mysql_query

mysql_query("...", $con);

The error your getting is because your outputting the buffer & then trying to add a header; 您得到的错误是因为您输出了缓冲区,然后尝试添加标题; (This can be a single space or a carriage return or the error message its self, but in your case its the var_dump) . (这可以是单个空格,也可以是回车符,也可以是错误消息本身,但是在您的情况下,可以是var_dump)。 Also You have sql injections vulnerabilities from not escaping the post variables, I do suggest you forget about the mysql_* family of functions and move to PDO prepared statements: 另外,由于没有转义post变量,您还有sql注入漏洞,我建议您不要忘记mysql_ *系列函数,而转而使用PDO准备的语句:

<?php
//Start a session to hold the status
session_start();
try {
    $dbh = new PDO("mysql:host=localhost;dbname=yourDB", 'dbUser', 'dbPass');

    /*** set the error reporting attribute ***/
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    /*** some variables ***/
    $from = $_POST['from'];
    $to = $_POST['to'];
    $content = $_POST['content'];
    $date = date("Y-n-j H:i:s");

    $id = rand(0000,9999); //? This is wrongly implemented if you want a uniqie id, you should be using the AUTO INCREMENT value

    /*** prepare the SQL statement ***/
    $stmt = $dbh->prepare("INSERT INTO pm (`from`, `to`, `content`, `date`, `id`)VALUES(:from, :to, :content, :date, :id)");

    /*** bind the paramaters ***/
    $stmt->bindParam(':id', $id, PDO::PARAM_INT);
    $stmt->bindParam(':from', $from, PDO::PARAM_STR, strlen($from));
    $stmt->bindParam(':to', $to, PDO::PARAM_STR, strlen($to));
    $stmt->bindParam(':content', $content, PDO::PARAM_STR, strlen($content));
    $stmt->bindParam(':date', $date, PDO::PARAM_STR, strlen($date));


    /*** execute the prepared statement ***/
    $stmt->execute();
    /*** close the database connection ***/
    $dbh = null;

    //Set as sent, redirect to members cleanly and then check session var is true
    $_SESSION['sent']=true;
    header('Location: ./members.php');
}
catch(PDOException $e)
{
    //Something went wrong
    $_SESSION['sent']=false;
    //You should log this error, and not output any internal errors to the user, just advise them something went wrong.
    error_log("FATAL MSG ERROR:".$e->getMessage(), 3, "/home/quebecen/logs/my-errors.log");

    header('Location: ./members.php');
}
?>

1) the header already sent error is pretty obvious, don't echo anything before using header() so the code should be : 1)标头已经发送的错误非常明显,在使用header()之前不要回显任何内容,因此代码应为:

if(!$result) {
    echo mysql_error();
    mysql_close($con);
} else {
    mysql_close($con);
    header('Location: members.php?sent');
}

2) try using $date = date("Ymd H:i:s"); 2)尝试使用$date = date("Ymd H:i:s");

Warning: Cannot modify header information - headers already sent by (output started at /home/quebecen/public_html/new/do_pm.php:10) in /home/quebecen/public_html/new/do_pm.php on line 15 警告:无法修改标头信息-第15行/home/quebecen/public_html/new/do_pm.php中已经发送过的标头(输出始于/home/quebecen/public_html/new/do_pm.php:10)

If you are going to use header function, make sure that there is no html contents will be displayed before that function. 如果要使用标题功能,请确保在该功能之前没有html内容显示。

Better you can use ob_start(); 更好的是可以使用ob_start(); function before redirecting or first line of your page. 在重定向或页面的第一行之前起作用。

Hope it helps 希望能帮助到你

This issue may occurred due to the web authoring tool you are using. 由于您使用的Web创作工具,可能会发生此问题。

You try following. 您尝试跟随。

  1. Copy the file name to somewhere. 将文件名复制到某个位置。 And backup your file. 并备份您的文件。

  2. Delete your existing file 删除现有文件

  3. Create a new php file using notepad or notepad++ (just text editor). 使用记事本或记事本++(仅文本编辑器)创建一个新的PHP文件。 And rename it for previous file name. 并将其重命名为以前的文件名。

  4. Copy your code to your file using the text editor. 使用文本编辑器将代码复制到文件中。 Copy exactly <?php to ?> with no whitespace. 完全将<?php复制到?> ,不带空格。

Make sure you have no whitespace before <?php . 确保<?php之前没有空格。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM