-2

I am trying to insert form value into database, but the data is not going into mysql. I spent lots of hour but could not resolve it... this is my code:

$query = "insert into $tabl (userid, username, question,  board, class, subject, article, status, uniq_id,date,activation  ) values('$userid', '$username', '$question', '$board', '$class', '$subject', '$article', 'unread', '$uniq',CURDATE(), '0')";
$row = mysql_query($query);

if($row)
{
     header("location:ask-questions.php?msg=saved");
}
else
{
    header("location:ask-questions.php?msg=not been saved");
}
//////////   (1)End of the Code /////////////
?>

all things are ok, and i echo $sql, this also working fine... any help would be appreciate....

vivek salve
  • 991
  • 1
  • 9
  • 20
Tracy
  • 49
  • 1
  • 5
  • try `$row = mysql_query($query) or die(mysql_error());` Also check http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php why you shouldnt use mysql – Hugo Delsing Jun 17 '13 at 09:55
  • one of your values could contain a single quote `'` which can cause the query to fail. use `mysql_real_escape_string($val)` before inserting – DevZer0 Jun 17 '13 at 09:55
  • y downvote..................? – Tracy Jun 17 '13 at 09:56
  • Check your field type i.e. data type if it is date then insert the date in the proper format. also change table field name from date to some thing else or put it in backtick ``in your query – vivek salve Jun 17 '13 at 10:01
  • echo the `$sql` and run it directly on the db, if it works it means you have a connection error, if it doesn't it means you have a syntax error or missing table/field in the query. – MrCode Jun 17 '13 at 10:03

1 Answers1

2

date is reserved word in mysql. use backtick ` around that kind of reserved words when used as column name.

 $query = "insert into $tabl (userid, username, question,  
 board, class, subject, article, status, uniq_id, `date`,activation  ) 
 values('$userid', '$username', '$question', '$board', '$class', '$subject', '$article', 'unread', '$uniq',CURDATE(), '0')";

 $row = mysql_query($query) OR die(mysql_error());

Also stop using mysql_* functions, they are deprecated. Start mysqli OR PDO.

Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
  • Not true, `date` is not a reserved word in MySQL. Because of so many people using `date` as a field name, the developers removed it as a reserved word. http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html – MrCode Jun 17 '13 at 10:01
  • okkk, but still not inserting into database, $sql carry data but i dont know y the data not going into table – Tracy Jun 17 '13 at 10:01
  • @Tracy put `$row = mysql_query($query) OR die(mysql_error());` for checking the error. Is `$tabl` defined anywhere. – Yogesh Suthar Jun 17 '13 at 10:04
  • ya...thats all okk, ok i m rying ur code...wait, n y people downvoting my question – Tracy Jun 17 '13 at 10:06
  • @Tracy I think they are downvoting because you are deprecated `mysql_*` functions. – Yogesh Suthar Jun 17 '13 at 10:09
  • okk, in future i'll try to use mysqli_*, n thanks yogesh i found my error... :) – Tracy Jun 17 '13 at 10:12
  • a field name cousing problem n i fixed that, n now data is going into mysql, yogesh where can i learn to use mysqli_* function – Tracy Jun 17 '13 at 10:16
  • @Tracy It's official site http://php.net/manual/en/mysqli.query.php It is same as `mysql_*`. – Yogesh Suthar Jun 17 '13 at 10:18