简体   繁体   English

为什么我无法通过PHP脚本将值插入子表中?

[英]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- 用户-

  • id (int) PRIMARY KEY and AUTO INCREMENT. id(int)主键和自动增量。
  • Name (varchar) 名称(varchar)
  • E-mail (varchar) 电子邮件(varchar)
  • Password (varchar) 密码(varchar)

Clothes- 衣服-

  • id (int) PRIMARY KEY and AUTO INCREMENT. id(int)主键和自动增量。
  • user_id (int) FOREIGN KEY user_id(int)外键
  • Decription (varchar) 解密(varchar)
  • Image (varchar) 图片(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. 当用户输入描述并上传图像时,我希望将其存储在我的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. 当我echoinsert查询时,我将查询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.

相关问题 为什么我无法通过php脚本“插入”表? - Why can't I “INSERT INTO” my table through my php script? 我想从我的 php 表中提取值。 我在表格中嵌入了表单,但无法使用 $_POST 获取值 - I want to extract values from my table in php. I have embeded form in my table but I am not able to get values using $_POST 为什么我无法连接到数据库? - Why am I not able to connect to my database? 为什么我的SQL插入语句无法通过php脚本执行? - Why is my SQL insert statement failing to execute through my php script? PHP-为什么我不能在表中插入信息? - PHP - Why can't I insert information into my table? 为什么我的 php 脚本没有向 mysql 插入数据 - why my php script not insert data to mysql 为什么我无法在AngularJS,php和MySQL的项目上建立会话? - Why am I not able to establish a session on my project on AngularJS, php & MySQL? 我无法从表单提交调用我的 php 代码,这是为什么? - I am not able to call my php code from form submit, why is this? 我正在尝试通过串行端口与微控制器通信。 但是我的PHP脚本给我一个错误 - I am trying to communicate with a micro controller through a serial port . but my php script is giving me an error 如何使用Ajax PHP将数组值从输入框插入表中 - How can I insert array values from my input box to my table using ajax PHP
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM