0

thanks for taking the time to look at this.

No errors pop up, but nothing writes to the database.

Can someone help me out with what im doing wrong?

<?php
$subject = $_POST['subject'];
$comment = $_POST['comment'];

if(isset($_POST['submit']))
{
$connect = mysql_connect('localhost','<USER>','<PASSWORD>');
mysql_select_db("rebeler_comment");

    $query = mysql_query("INSERT INTO `cdb` ('',subject', 'comment' VALUES    ('',$subject','$comment')");


echo "Data successfully written to DB";
}

else{
echo "Sorry, there was a problem.";

}
?>
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • 3
    Please don't use `mysql_*` functions anymore, they are deprecated. See [Why shouldn't I use mysql_* functions in PHP?](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) for details. Instead you should learn about [prepared statements](http://bobby-tables.com/php.html) and use either [PDO](http://php.net/pdo) or [MySQLi](http://php.net/mysqli). If you can't decide which, [this article](http://php.net/manual/en/mysqlinfo.api.choosing.php) will help you. If you pick PDO, [here is a good tutorial](http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers). – Marcel Korpel Nov 05 '13 at 15:52
  • 2
    BTW, you're open to SQL injections. And you should use backticks for column names. – Marcel Korpel Nov 05 '13 at 15:53
  • Use `mysql_error()`. It would tell you exactly what is wrong. – John Conde Nov 05 '13 at 15:54
  • Your MySQL syntax looks very awful... – idmean Nov 05 '13 at 15:54
  • 1. What @MarcelKorpel said. 2. Are you getting anything printed on the screen? Is "Data successfully written to DB" being written? – Brian Warshaw Nov 05 '13 at 15:55
  • 1
    You forgot a closing `)` in `('',subject', 'comment'` and a `'`. Change to `('', 'subject', 'comment')` – Funk Forty Niner Nov 05 '13 at 15:56
  • @Fred-ii- And many more… – Marcel Korpel Nov 05 '13 at 15:59
  • @MarcelKorpel Yes I just noticed. – Funk Forty Niner Nov 05 '13 at 15:59
  • @brian yes it does show data successfully written – Moses Bolton Nov 05 '13 at 16:00
  • 1
    Of course it shows that, as there's no error catching in your code. – Marcel Korpel Nov 05 '13 at 16:01
  • @wumm, ty lol, I have little idea about what im doing in php, still learning, but need this up asap for a website I built for my team at work – Moses Bolton Nov 05 '13 at 16:01
  • Don't put this code live, or you will be hacked! – Marcel Korpel Nov 05 '13 at 16:03
  • Also this `('',$subject','$comment')` change to `('','$subject','$comment')` yet you should use prepared statements, ***urgently.*** – Funk Forty Niner Nov 05 '13 at 16:03
  • If you only have 2 columns to update, then all you need to do is use `('subject', 'comment') VALUES ('$subject','$comment')` if you have 3, then you will need to adjust accordingly. And wrap the first (2) in backticks, SO won't let me show them. – Funk Forty Niner Nov 05 '13 at 16:08

3 Answers3

7

Of course there's no errors. You don't check for any, so you've missed the glaring syntax error in your insert query:

$query = mysql_query("INSERT INTO `cdb` ('',subject', 'comment' VALUES    ('',$subject','$comment')");
                                         ^^---invalid field
                                            ^---unbalanced quote
                                                               ^---missing )

You can NOT quote field names with ' quotes. That turns them into strings, not field names.

Your code should at absolute bare minimum have the following structure:

$result = mysql_query(...) or die(mysql_error());
                          ^^^^^^^^^^^^^^^^^^^^^^

In short, your SQL is a disaster.

Marc B
  • 356,200
  • 43
  • 426
  • 500
3

PDO:

$pdo = new PDO('mysql:host=localhost;dbname=database', '<USER>', '<PASSWORD>');
$stmt = $pdo->prepare('INSERT INTO `cdb` (`subject`, `comment`) VALUES (:subject, :$comment)');
$stmt->execute(array(':comment' => $comment, ':subject' => $subject));

These extensions have built-in functions for creating prepared queries which let you use quotes and apostrophes without any problems. It is way better than using the deprecated mysql_* extension.

-1

Try

$subject = mysql_real_escape_string($subject);
$comment = mysql_real_escape_string($comment);
$query = mysql_query("INSERT INTO `cdb` (`subject`, `comment`) VALUES ('$subject','$comment')");

For checking error check mysql_errno() (is not 0 than error), mysql_error() contains error message

CreatoR
  • 1,654
  • 10
  • 14
  • 1
    You can't surround column names with apostrophes (well, at least, you shouldn't). Use backticks instead (or nothing if they're not reserved words). – Marcel Korpel Nov 05 '13 at 16:00
  • @CreatoR, ur awesome, thanks...at everyone else, thank you for posting, I love this place, I will work on getting this secured and all as well, and I will get better at php I promise wumm and marc, lol thanks guys/gals for the help – Moses Bolton Nov 05 '13 at 16:15
  • @MosesBolton There's usually a ***"happy ending"***, and this one was one of them, cheers. – Funk Forty Niner Nov 05 '13 at 16:15