Reputation: 383
I can't figure out why this isn't working. It says "note added" but it doesn't actually add it to the database.
<?php
$notetitle = $_POST['title'];
$notebody = $_POST['body'];
if ($notetitle && $notebody){
mysql_query("insert into notes values
('$user_id', '', '$subject', '$notetitle', '$note_type' '$notebody')");
echo "Note \"" . $notetitle . "\" added.";
}
?>
Upvotes: 2
Views: 105
Reputation: 8629
You have missed a comma between "'$note_type' '$notebody'",
In better way you should to write like this:=
$notetitle = $_POST['title'];
$notebody = $_POST['body'];
if ($notetitle && $notebody){
mysql_query("insert into notes
set userid='$user_id',
subject = '$subject',
notetitle = '$notetitle',
note_type = '$note_type',
notebody = '$notebody' ");
}
Actually it will not conflict your column name and values. :)
Upvotes: 0
Reputation: 48357
If you checked if the insert worked and reported errors on failure then you'd know why it wasn't working....
if ($notetitle && $notebody){
$qry="insert into notes values
('$user_id', '', '$subject', '$notetitle', '$note_type' '$notebody')";
if (mysql_query($qry)) {
echo "Note \"" . $notetitle . "\" added.";
} else {
print "Failed: $qry\n" . mysql_error();
}
}
Not handling errors and exceptions is very bad programming. Not declaring your columns in an insert statement is very bad practice. Not using an explicit database handle is messy. Not commenting your code is bad practice.
Upvotes: 0
Reputation: 13166
You have some mistakes in your code. use the code below and check it. I have added something more to be sure about your data accuracy:
<?php
$notetitle = $_POST['title'];
$notebody = $_POST['body'];
if ($notetitle != '' && $notebody !='') {
$myQuery = mysql_query("INSERT INTO notes VALUES
('$user_id', '', '$subject', '$notetitle',
'$note_type', '$notebody')");
// verify your database query and then show the message below
if (mysql_affected_rows()) {
echo "Note \"" . $notetitle . "\" added.";
}
}
?>
Pay attention that you forgot a comma before '$notebody' in your MySQL query.
Upvotes: 1
Reputation: 39
Your SQL query is not quite valid - you also have to add to it the column names which you want to set through INSERT
:
mysql_query("insert into notes (id, smth, subject, ... etc) values
('$user_id', '', '$subject', '$notetitle', '$note_type', '$notebody')");
Upvotes: 0
Reputation: 1372
You missed a comma:
('$user_id', '', '$subject', '$notetitle', '$note_type', '$notebody')");
Upvotes: 7