【问题标题】:Add the image and path to the database for the specific users为特定用户添加图像和路径到数据库
【发布时间】:2021-02-01 06:02:25
【问题描述】:
protected void Upload(object sender, EventArgs e)
    {
        //Extract Image File Name.
        string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

        //Set the Image File Path.
        string filePath = "~/Uploads/" + fileName;

        //Save the Image File in Folder.
        FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));
        string mycon = "server =localhost; Uid=root; password = ; persistsecurityinfo = True; database =ovs; SslMode = none";

        MySqlConnection con1 = new MySqlConnection(mycon);
      //  string sql = "INSERT INTO candidate VALUES(@Name, @Path)";
        MySqlCommand cmd = null;
        cmd = new MySqlCommand("INSERT INTO candidate(candidateImage,path where studentID ='" + Session["UserName"] + "') VALUES (@Name,@Path)", con1);

                cmd.Parameters.AddWithValue("@Name", fileName);
                cmd.Parameters.AddWithValue("@Path", filePath);
        con1.Open();
        cmd.ExecuteNonQuery();
        con1.Close();
    }

当用户点击上传按钮时,图片和路径将被保存到属于特定用户的数据库中。比如ID为R1001的用户登录自己的账号,点击上传按钮,图片和路径就会保存在数据库中的R1001下。我显示错误消息,提示存在语法错误,但我无法找到它

database

【问题讨论】:

  • 你的错误信息是什么?
  • 它说 cmd = new MySqlCommand("INSERT INTO Candidate(candidateImage,path where studentID ='" + Session["UserName"] + "') VALUES (@Name, @Path)", con1);
  • 为什么你在 (candidateImage,path where studentID ='" + Session["UserName"] + "') 插入时间在哪里?
  • StudentID Name Path R1001 当前数据库是这样的,我的意图是当用户点击上传按钮时,名称和路径将被添加。 StudentID 名称 路径 R1001 image1 uploads/ images1
  • 分享你的表格架构。我的意思是表格的字段,以便知道哪些字段是必需的

标签: c# asp.net gridview


【解决方案1】:

当您想向表中插入数据时,您不需要Where。 我在您的command 中删除了where,并为sessionId 使用了一个参数。

     protected void Upload(object sender, EventArgs e)
    {
        //Extract Image File Name.
        string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);

        //Set the Image File Path.
        string filePath = "~/Uploads/" + fileName;

        //Save the Image File in Folder.
        FileUpload1.PostedFile.SaveAs(Server.MapPath(filePath));
        string mycon = "server =localhost; Uid=root; password = ; persistsecurityinfo = True; database =ovs; SslMode = none";

        MySqlConnection con1 = new MySqlConnection(mycon);
        //  string sql = "INSERT INTO candidate VALUES(@Name, @Path)";
        SqlCommand cmd = null;
        string command = "update candidate set candidateImage=@Name ,path = @Path where StudentID=@studnetId";
        cmd = new MySqlCommand(command, con1);
        
        cmd.Parameters.AddWithValue("@Name", fileName);
        cmd.Parameters.AddWithValue("@Path", filePath);
        cmd.Parameters.AddWithValue("@studnetId", Session["UserName"]??""); // if student id is accept number in database, you must convert it to int
        con1.Open();
        cmd.ExecuteNonQuery();
        con1.Close();
    }

【讨论】:

  • 你拼错了@studentId 并输入了两次@Name 而不是@Path
  • 数据库中有3列,StudentID、name和path。数据库中已经存在studentID R1001,名称和路径不存在。当用户点击上传按钮时,想在studentID R1001下存储名字和路径
  • @KekwYc 您正在更新一行并使用 WHERE,或者您正在添加一个新行但不添加 WHERE。但正如 osman Rahimi 所说,没有带有 WHERE 子句的 Insert 这样的东西。
  • @KekwYc 所以你需要更新。用户总是有记录吗?
  • 我将命令更改为更新: cmd = new MySqlCommand("UPDATE SET Candidate(candidateImage,path where studentID ='" + Session["UserName"] + "') VALUES (@Name,@ Path)", con1);,但它仍然显示错误消息说语法错误
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-05-09
  • 1970-01-01
  • 1970-01-01
  • 2013-07-30
  • 2019-11-16
相关资源
最近更新 更多