简体   繁体   中英

PHP: String won't insert into MySQL database

My string field won't insert into my database.

(The columns follower_username and following_username they are VARCHAR(200) don't insert ) The: follower and following column values insert work.

mysql_query("INSERT INTO `follow` (`follower`, `following`, `follower_username`, `following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."', '".$get_user."')");

Strings:

$get_user = mysql_real_escape_string($row['username']);
$get_user_id = (int)mysql_real_escape_string($row['id']);
$userid = (int)mysql_real_escape_string($user_data['id']);
$username = mysql_real_escape_string($user_data['username']);

I have no idea what to do, whether it is the PHP or the database itself :S

Thanks in advance :)

try this :

mysql_query("INSERT INTO follow (`follower`, `following`, `follower_username`, `following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."', '".$get_user."')");

don't use single quotes around table name.

You could try echoing the mysql statement just before the mysql_query, ie

echo "INSERT INTO `follow` (`follower`, `following`, `follower_username`, `following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."', '".$get_user."')";

and check if the string is what you expected it to be. If it is what you expected, try manually copying the string and pasting it into the mysql console and see if any errors occur.

Try adding mysql_error to your statement to find out what error is it so you can fix it:

mysql_query("INSERT INTO `follow` (`follower`, `following`, `follower_username`,
`following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."',   
'".$get_user."')") or die (mysql_error());

For debug and simple work I recommended you store SQL query in variable.

$query  = "INSERT INTO `follow` (`follower`, `following`, `follower_username`, `following_username`) VALUES ('".$userid."', '".$get_user_id."', '".$username."', '".$get_user."')";
echo "DEBUG:".$query;
mysql_query($query);

Try this:

 $objQuery = mysql_query("INSERT INTO `follow` (`follower`, `following`, `follower_username`,
    `following_username`) VALUES ($userid, $get_user_id, '".$username."',   
    '".$get_user."')") or die (mysql_error());

if(!$objQuery){
 echo "something went wrong!";
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM