I am new to PHP and MySQL. I simply want to register users to my site (which I did successfully) and once they login, they can donate clothes. I have two tables-
Users-
- id (int) PRIMARY KEY and AUTO INCREMENT.
- Name (varchar)
- E-mail (varchar)
- Password (varchar)
Clothes-
- id (int) PRIMARY KEY and AUTO INCREMENT.
- user_id (int) FOREIGN KEY
- Decription (varchar)
- Image (varchar)
What I want is that, when the user logs in and clicks on the donate button, it will take the user to this page-
Fill The Clothes Details
<form action="clothes.php" method="post" enctype="multipart/form-data">
<table align="center" width="760px" border="2">
<td>Clothes Details</td>
<td><textarea cols="60" rows="10" name="description" ></textarea></td>
</tr>
<tr>
<td>Image</td>
<td><input type="file" name="image" /></td>
</tr>
<tr>
<td><input type="submit" name="register" value="Submit" ></td>
</tr>
</table>
</form>
</body>
</html>
When the user enters the description and uploads the image, I want it to be stored in my PhpMyAdmin table, but with the respective id
of the user. Here is my PHP script-
<?php
session_start();
include ("includes/db.php");
if (isset($_POST['register'])) {
$description = $_POST['description'];
$image = $_FILES['image']['name'];
$temp = $_FILES['image']['tmp_name'];
move_uploaded_file($temp, "clothes/$image");
$insert = "insert into clothes (description, image) values ( '$description', '$image')";
$run = mysqli_query($con, $insert);
if ($run) {
echo "<script>alert('Clothes Successfully Donated')</script>";
}
}
?>
And as obvious it may seem, the data is not inserted into the table. When I echo
the insert
query, I am getting the query insert into clothes (user_id, description, image) values ( 'Jeans', 'HUMBLE. - Single.jpg')
, which means my query is working. But I just cannot figure out what the problem is as this is my first experience with foreign keys. What am I doing wrong?