[英]Why am I not able to insert values into my child table through my PHP script?
I am new to PHP and MySQL. 我是PHP和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- 用户-
Clothes- 衣服-
What I want is that, when the user logs in and clicks on the donate button, it will take the user to this page- 我想要的是,当用户登录并单击捐赠按钮时,它将带用户到此页面-
<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. 当用户输入描述并上传图像时,我希望将其存储在我的PhpMyAdmin表中,但要具有用户的各自
id
。 Here is my PHP script- 这是我的PHP脚本-
<?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. 当我
echo
显insert
查询时,我将查询insert into clothes (user_id, description, image) values ( 'Jeans', 'HUMBLE. - Single.jpg')
,这意味着我的查询正在工作。 But I just cannot figure out what the problem is as this is my first experience with foreign keys. 但是我只是无法弄清楚问题出在哪里,因为这是我第一次使用外键。 What am I doing wrong?
我究竟做错了什么?
first of all, you need to correct the code. 首先,您需要更正代码。
<?php
session_start();
include ("includes/db.php");
if (isset($_POST['register'])) {
$user_id = $_SESSION['user_id'];
$description = $_POST['description'];
$image = $_FILES['image']['name'];
$temp = $_FILES['image']['tmp_name'];
move_uploaded_file($temp, "clothes/$image");
$insert = "insert into clothes (user_id, description, image) values ( $user_id, '$description', '$image')";
$run = mysqli_query($con, $insert);
if ($run) {
echo "<script>alert('Clothes Successfully Donated')</script>";
}
}
?>
First you need to verify, whether you have set the logged in user's id in session, If not then set first then this code will work fine. 首先,您需要验证是否已在会话中设置了登录用户的ID。如果未设置,则先进行设置,然后此代码将可以正常工作。 If you have not set then please set $_SESSION['user_id']=[id of loggedin user].
如果尚未设置,请设置$ _SESSION ['user_id'] = [已登录用户的ID]。
Hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.