Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9172679
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:24:26+00:00 2026-06-17T16:24:26+00:00

I was trying to set a foreign key so that I can associate the

  • 0

I was trying to set a foreign key so that I can associate the author of the photo to the username of users table , but I’m unable to create table.

UPDATE I got the unable to create table error resolved but the data still doesn’t get in to the tables.

CREATE TABLE IF NOT EXISTS `users` (
`username` varchar(30) NOT NULL ,
`password` varchar(40) default NULL,
`usersalt` varchar(8) NOT NULL,
`userid` varchar(32) default NULL,
`userlevel` tinyint(1) unsigned NOT NULL,
`email` varchar(50) default NULL,
`timestamp` int(11) unsigned NOT NULL,
`actkey` varchar(35) NOT NULL,
`ip` varchar(15) NOT NULL,
regdate` int(11) unsigned NOT NULL,
PRIMARY KEY  (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



CREATE TABLE IF NOT EXISTS photos (
ref int(10) unsigned NOT NULL auto_increment,
    photo varchar(75),
Firstname varchar(35),
    Lastname varchar(35),
    Age INT(3),
    author varchar(30) NOT NULL,
PRIMARY KEY (ref)

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

here is what I tried to add FK after creating the table..

ALTER TABLE photos
ADD CONSTRAINT FK_photos
FOREIGN KEY (author) REFERENCES users(username)
ON UPDATE CASCADE
ON DELETE CASCADE;

here is the code to insert photo and some info along with it to database…

<?php
include("/include/session.php");
if(!$session->logged_in){ header("Location: ../main.php"); } else {
}
?>



<?php
$sub=0;

ini_set( "display_errors", 0);
if(isset($_REQUEST['submited'])) {
// your save code goes here

$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 2097152)
&& in_array($extension, $allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
}
else
{
echo "";
if (file_exists("pictures/" . $_FILES["file"]["name"]))
{
echo "<font size='4' color='red'><b>We are sorry, the file you trying to upload already exists.</b></font>";
  }

else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"pictures/" . $_FILES["file"]["name"]);
$sub= 1;
$mysqli = new mysqli("localhost", "root", "", "secure_login");

// TODO - Check that connection was successful.

$photo= "pictures/" . $_FILES["file"]["name"];
$fname = $_POST["fname"];
$lname = $_POST["lname"];
$age   =$_POST["age"];
$stmt = $mysqli->prepare("INSERT INTO photos (photo, Firstname, Lastname, Age) VALUES (?, ?, ?, ?)");

// TODO check that $stmt creation succeeded

// "s" means the database expects a string
$stmt->bind_param("ssss", $photo, $fname, $lname, $age);

$stmt->execute();

$stmt->close();

$mysqli->close();



echo "<font size='7' color='white'><b> Success! Your item has been listed.</b></font>";
echo '<meta http-equiv="refresh" content="2;url=home.php">';
}

}
}
else
{
echo "<font size='4' color='red'><b>We are sorry, the file you trying to upload is not an image or it exceeds 2MB in size.</b></font><br><font color='blue'><i>Only images under size of 2MB are allowed</i></font>.";
}
}


?>

<form action="" method="post" enctype="multipart/form-data">
<input type="hidden" name="submited" value="true" />


<?php
ini_set( "display_errors", 0);
if($sub==0)
{
?> 
<label  for="file"><font  size="5"><b>Choose Photo:</b></font></label>
<input id="shiny" type="file" name="file" onchange="file_selected = true;" required><br>
Last Name:<input  type="text" name="fname" value="<?php echo (isset($_POST['fname']) ? htmlspecialchars($_POST['fname']) : ''); ?>"required><br> 
Last Name:<input  type="text" name="lname" required><br> 
Age:<input type="text" name="age" required><br>
<input id="shiny" type="submit" value="Submit" name="submit">
<?php
}
?>


</form>
</div>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T16:24:28+00:00Added an answer on June 17, 2026 at 4:24 pm

    It looks like your table definition needs a comma after photo varchar(75)

    CREATE TABLE IF NOT EXISTS photos (
        ref int(10) unsigned NOT NULL auto_increment,
        photo varchar(75),
        Firstname varchar(35),
        Lastname varchar(35),
        Age INT(3),
        author varchar(30) NOT NULL,
    PRIMARY KEY (ref)
    
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a constraint on table A to check if a set
I'm trying to retrieve a user object from a foreign key reference but each
I am trying to set up a foreign key in Mysql workbench. I used
I'd like to setup a foreign key to a default membership table but I'm
Im trying set the single table inheritance model type in a form. So i
I have 2 Entities that I am trying to set up using a @OneToOne
Using cake acl I am trying to set permissions for logged in users. I
I'm still learning what hibernate can do and this time i'm trying something that
I'm trying to map a collection (of type map) using a foreign key and
I just trying to use a update in my application, but i just can't.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.