I want to make a post form. After users login in my space, they can input something in the database. My database fields are: id(automatic raise 1) ,user_id, key, data.
When the database field ‘data’ is empte, insert the $post directly, when the database field ‘data’ has already generated, erase the last 4 character then insert the $post.
my code is:
mysql_select_db($db, $link);
$result = mysql_query("SELECT * FROM book1 where user_id='$user_id' and key='14' and data IS NULL");
if(mysql_num_rows($result) > 0) {
$result = mysql_query("update book1 set data=(concat(left(data,char_length(data)-4),'$post'))where user_id='$user_id' and key='14'");
}else{
$result = mysql_query("INSERT INTO book1 VALUES ( '', '$user_id', '14', '$post')");
}
But now I can only insert a new value, I can’t update the database.
What’s wrong in my “update concat” code or because of the automatic id field? Pls help me. Thanks.
Thanks for your quick reply.
When the users first input something ,the value of the ‘data’ column is NULL, so just use INSERT INTO; But when they want to input more, the value of the ‘data’ column is always use update method(delete last four character and input new $post.
Thanks for Guido, I will cancel the echo statements.
But I still can not update the database.
It always insert a new record with field ‘id’ increase 1.
my database table:
id | use_id | key | data
17| 2 | 14 | aaaaaa
16| 2 | 14 | bbbbbb
The echo is:
select query: SELECT * FROM book1 WHERE user_id = ‘2’ AND wkey = ‘14’ AND data IS NULL
number of rows returned: 0
insert query: INSERT INTO book1 VALUES ( ‘’, ‘2’, ‘14’, ‘bbbbbb’)
select query: SELECT * FROM book1 WHERE user_id = ‘2’ AND wkey = ‘14’ AND data IS NULL
number of rows returned: 0
insert query: INSERT INTO book1 VALUES ( ‘’, ‘2’, ‘14’, ‘aaaaaa’)