简体   繁体   中英

cant add POST content to mysql database

<?php

require "config.php";

/*
CREATE TABLE  `addnews` (
 `id` INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
 `auther` VARCHAR( 255 ) NOT NULL ,
 `title` VARCHAR( 255 ) NOT NULL ,
 `content` LONGTEXT NOT NULL
) ENGINE = MYISAM ;
*/

$a = $_POST['author'];
$t = $_POST['title'];
$c = $_POST['content'];

if(isset($_POST["add"]) and $_POST["add"] == "news"){
    $insert = mysql_query('INSERT INTO addnews 
    (author,title,content)
    VALUES
    ("$a","$t","$c")') or die("error");
    if (isset($insert )){
        echo "<h3>Done</h3>";
    }
};

echo "
<form action='".$_SERVER['PHP_SELF']."' method='post'>
Author : <input type='text' name='author' /><br>
Title : <input type='text' name='title' /><br>
Content : <textarea name='content'></textarea>
<input type='submit' value='Add news' />
<input type='hidden' name='add' value='news' />
</form>
";


mysql_close($connectdb);
?>

i am getting error from this statment i think

if(isset($_POST["add"]) and $_POST["add"] == "news"){
    $insert = mysql_query('INSERT INTO addnews 
    (author,title,content)
    VALUES
    ("$a","$t","$c")') or die("error happend while trying to add information to database");
    if (isset($insert )){
        echo "<h3>Done</h3>";
    }
};

output is : error happend while trying to add information to database

and no problem with config.php file (the file that connect to database) i am using phpmyadmin

Use && instead of the actual word and:

if(isset($_POST["add"]) && $_POST["add"] == "news"){
    $insert = mysql_query("INSERT INTO addnews 
    (author,title,content)
    VALUES
    ('$a','$t','$c')") or die("error happend while trying to add information to database");
    if (isset($insert )){
        echo "<h3>Done</h3>";
    }
};

Here you go try this one

if(isset($_POST["add"]) and $_POST["add"] == "news"){
    $insert = mysql_query('INSERT INTO addnews 
    (author,title,content)
    VALUES
    ("'. $a .'","'. $t .'","'. $c .'")') or die("error happend while trying to add information to database");
    if (isset($insert )){
        echo "<h3>Done</h3>";
    }
};

used "'. $a .'" instead "$a" .

I think the query statement is wrong, Double quotes inside the single quotes is not valid in php. So you will change the quotes in query like below code,

$insert = mysql_query("INSERT INTO addnews 
    (author,title,content)
    VALUES
    ('$a','$t','$c')") or die("error");

try this..:-)

Please do the correction in your code like as follow:

$insert = mysql_query("INSERT INTO addnews 
    (author,title,content)
    VALUES
    ('$a','$t','$c')") or die(mysql_error($link));//Where $link mysql resource object 

You will get the answer why Mysql not inserting your data.

  1. strings in sql are surrounded by ' (single quote) , not by " (double quote)
  2. strings in php will act two ways
    1. those in ' (single quote) will write literally as tyou typed them ($a stays $a - not $a value)
    2. those in " (double quote) will interpret values inside - so $a will be substituted with $a's value
  3. when failing DB operation - it is usually useful to see what was wrong - use mysql_error for that

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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