2

I am trying to take out url from the table users and insert it into url of a table images from the current user logged in. This code is not able to insert the url in the images table.

$uploaduser = $_SESSION["username"];
$selectuser = mysqli_query("SELECT * FROM users WHERE username=$uploaduser",$db1);
$row = mysqli_fetch_array($selectuser);
$pic = $row['url'];

$sql = "INSERT INTO images (image1, image_text1, uploaduser, url, date) VALUES ('$image1', '$image_text1' , '$uploaduser','$pic','$date')";

mysqli_query($db1, $sql);
Isaac Bennetch
  • 11,830
  • 2
  • 32
  • 43
Yash Pawse
  • 41
  • 10

2 Answers2

2

The first argument should be the link ($db1). Then, except if username is a numeric datatype, you should wrap the value using single quotes.

Try this :

$selectuser = mysqli_query($db1, "SELECT * FROM users WHERE username='$uploaduser'");

Important: You should have a look to How can I prevent SQL injection in PHP? to secure your queries.

Syscall
  • 19,327
  • 10
  • 37
  • 52
0

You should use prepared statements, anyway from what it looks like,

$selectuser = mysqli_query("SELECT * FROM users WHERE username=$uploaduser",$db1);

You have an error here. mysqli_query expects param 1 to be the connection variable and param 2 as query string, instead you have passed the second param as connection variable and the first param as the query string.

Also try to concatenate the variables instead of directly passing the variables into the query string.

Instead of

$sql = "INSERT INTO images (image1, image_text1, uploaduser, url, date) VALUES ('$image1', '$image_text1' , '$uploaduser','$pic','$date')";

Try

$sql = "INSERT INTO images (image1, image_text1, uploaduser, url, date) VALUES ('".$image1."', '".$image_text1."' , '".$uploaduser."','".$pic."','".$date."')";

Hope this helps.