Chew Chew - 6 months ago 11
SQL Question

how i insert 1 data with 2 string? and insert into database?

i was doing a insert statement and i want 2 username become 1 data and insert into database. i no sure how to do it.

here is my php and mysql

$target = $_GET['user'];
$id = $_SESSION['id'];

$targetusername = mysqli_query($connection,"SELECT username FROM user WHERE id='$target' ")or die(mysqli_error($connection));
$username= mysqli_query($connection,"SELECT username FROM user WHERE id='$id'") or die(mysqli_error($connection));

$direct_message_room_name ="INSERT INTO direct_message_room(`direct_message_room_name`,`user_1_id`,`user_2_id`) VALUES (('$targetusername' + ' and ' + '$username'),'$id','$target')";

Answer

You can concat two strings in php like so:

$StringAB = $StringA . " something " . $StringB;



$target = $_GET['user'];
$id = $_SESSION['id'];

$targetusername = mysqli_query($connection,"SELECT username FROM user WHERE id='$target' ")or die(mysqli_error($connection));


$username=  mysqli_query($connection,"SELECT username FROM user WHERE id='$id'") or die(mysqli_error($connection));

$rowUserName = mysql_fetch_array($username); 
$rowTargetUserName = mysql_fetch_array($targetusername );

$room_name = $rowUserName["username"] . ' and ' . $rowTargetUserName["username"]; // Create room name (assuming both exist)
$direct_message_room_name ="INSERT INTO direct_message_room(`direct_message_room_name`,`user_1_id`,`user_2_id`) VALUES ('$room_name','$id','$target')";