I have a script that is uploading a photo, writing photo file names to a database table and creating an automatic ID (primary key).
Then, in the same script, there is a query to create an entry in the ‘activity’ table - at present it writes user data and image data but in order for the image to be linked to and work within the rest of my web app I need to pull in the id that was automatically created by the query before it and to write it into the activity table’s activity_relatedid
column.
This is the working code, before I started to add the image id to activity table;
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="gallery-photos/";
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
// Write photo details to DB
$sql="INSERT INTO photos(user_id,username,photo_file,photo_type,photo_size) VALUES('$_POST[user_id]','$_POST[username]','$final_file','$file_type','$new_size')";
mysql_query($sql);
?>
<?php
// Generate URLs for activity feed
//$profileurl = 'girl.php?' . $_POST[username] . '&uid=' . $_POST[user_id];
//$imgurl = 'viewphoto.php?' . $_POST[username] . '&uid=' . $_POST[user_id] .'&img=' . $final_file;
// Write activity details to DB
$sql="INSERT INTO user_activity(user_id,user_name,activity_type,activity_meta,activity_relatedlink,activity_relatedid) VALUES('$_POST[user_id]','$_POST[username]','gallery-photo','$_POST[username] added a new photo to their gallery!','$final_file','$activity_relatedid')";
mysql_query($sql);
?>
and these are the changes I’ve been trying to make to capture and insert the id - it works but creates a duplicate record in the photos table;
<?php
include_once 'dbconfig.php';
if(isset($_POST['btn-upload']))
{
$file = rand(1000,100000)."-".$_FILES['file']['name'];
$file_loc = $_FILES['file']['tmp_name'];
$file_size = $_FILES['file']['size'];
$file_type = $_FILES['file']['type'];
$folder="gallery-photos/";
// new file size in KB
$new_size = $file_size/1024;
// new file size in KB
// make file name in lower case
$new_file_name = strtolower($file);
// make file name in lower case
$final_file=str_replace(' ','-',$new_file_name);
if(move_uploaded_file($file_loc,$folder.$final_file))
{
// Write photo details to DB
$sql="INSERT INTO photos(user_id,username,photo_file,photo_type,photo_size) VALUES('$_POST[user_id]','$_POST[username]','$final_file','$file_type','$new_size')";
mysql_query($sql);
if (mysqli_query($conn, $sql)) {
$last_id = mysqli_insert_id($conn);
//echo "New record created successfully. Last inserted ID is: " . $last_id;
// Write activity details to DB
$query="INSERT INTO user_activity(user_id,user_name,activity_type,activity_meta,activity_relatedlink,activity_relatedid) VALUES('$_POST[user_id]','$_POST[username]','gallery-photo','$_POST[username] added a new photo to their gallery!','$final_file','$last_id')";
mysql_query($query);
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
?>
for info my connection script (dbconfig.php) is;
<?php
$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "lxv2";
mysql_connect($dbhost,$dbuser,$dbpass) or die('cannot connect to the server');
mysql_select_db($dbname) or die('database selection problem');
// Create connection
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
So effectively, I have the code almost spot on but how can’t see what is creating the dublicate record in the first query.