0

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 :)

11
  • Are you sure that the strings get assigned? Which variables don't get inserted? What happens if you echo them on their own? Do the correct values display? Commented Jan 8, 2014 at 17:59
  • I suppose you've read about mysql_query() in a blog article or something like that. Just head to the manual page, scroll down the "do not use this extension" warning and check the examples provided; they both provide details on how to do error checking. Commented Jan 8, 2014 at 17:59
  • Is there any more code? Specifically where your getting the user_data array from? Commented Jan 8, 2014 at 17:59
  • The arrays work if I echo them, and they work in other queries, which is odd.. Commented Jan 8, 2014 at 18:00
  • 1
    Also make sure you're connected to your DB and the columns do in fact exist. Commented Jan 8, 2014 at 18:05

5 Answers 5

1

You could try echoing the mysql statement just before the mysql_query, i.e.

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

0

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());

Comments

0

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);

Comments

0

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!";
}

3 Comments

@Billy_2lgit_2qt : can you try this?
Do read my comments that have been given under your original question. @Billy_2lgit_2qt
@Billy_2lgit_2qt : i have updated my answer. can you try again? also check Fred -ii- comments( check db connection)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.