<?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>";
}
};
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.
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.