php - Cannot INSERT INTO from my form -


i have form

<form method="post" action="new_announcement.php"> <fieldset>     <legend>create new announcement</legend> subject :<input type="text" style="float-offset: !important;" name="subject" required="required" /><br> content :<br /> <textarea rows="10" cols="60" name="content"></textarea>   </br></br>  <input type="submit" value="upload"/> </fieldset> </form> 

and want store data form table in database.

my insert into code this...

<?php  include_once 'header.php';  $username = $_session["username"];  if(isset($_post['username'])) {     $subject = $_post['subject'];     $content = $_post['content'];      $sqlinsert = "insert announcements (author,subject,content) values ('$username','$subject','$content')"; }  ?> 

what doing wrong , not store data in database. database table below...

create table announcements (     id int unsigned auto_increment,     author varchar(200),     subject varchar(200),     content varchar(3000),     timestamp int(11) unsigned not null,     primary key (id,author) ) engine=myisam; 

issue 1. form doesn't have input named "username". if(isset($_post['username'])) never match. if expect username form you'll need make one. if session set , correct (which sounds is) use it. issue 2. connection isn't being used in query (as question stated). here's updated navjot answer.

<?php    include_once 'header.php';    $username = mysql_real_escape_string($_session["username"]);    if(!empty($_post)){    if(isset($username)) {         $subject = mysql_real_escape_string($_post['subject']);         $content = mysql_real_escape_string($_post['content']);         $sqlinsert = "insert announcements (author,subject,content) values ('$username','$subject','$content')"; $execute = mysql_query($sqlinsert) or die(mysql_error());    } } ?> 

issue 3. sql injectible, never trust user input. issue 4. mysql functions out of date should switch on mysqli or pdo.

there's tons of other threads on these topics though.

if sanitize username when store session might fine without real escape.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -