2011-04-04 75 views
0

嘿併發症的傢伙位在這裏,我有一個創建帳戶頁面,它只是將數據插入一個MySQL數據庫:插入到多個表中不知道的主鍵

protected void Button1_Click(object sender, EventArgs e) 
    { 
     OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"); 
     cn.Open(); 
     OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme, username, password) VALUES ('" + TextBox1.Text + "', '" + TextBox2.Text + "', '" + TextBox3.Text + "', '" + TextBox4.Text + "', '" + TextBox5.Text + "', '" + TextBox6.Text + "', '" + TextBox7.Text + "', '" + TextBox8.Text + "')", cn); 

     cmd.ExecuteNonQuery(); 
     { 
      //e.Authenticated = true; 
      Response.Redirect("Login.aspx"); 
      // Event useradded is true forward to login 
     } 
    } 

} 

但這裏是我的問題創建賬戶頁面我添加了一個FileUpload控件,我想上傳圖片,並保存在圖片表中的IMAGEURL:

  string filenameDB = Path.GetFileName(FileUploadControl.FileName); 
      string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + Path.GetFileName(FileUploadControl.FileName); 
      FileUploadControl.SaveAs(fileuploadpath); 
      string fileuploadpaths = ("~/userdata/" + theUserId + "/uploadedimage/") + filenameDB; 
      StatusLabel.Text = "Upload status: File uploaded!"; 


      OdbcCommand cmd = new OdbcCommand("INSERT INTO Pictures VALUES picturepath ='" + fileuploadpaths + "' WHERE UserId = '" + theuserid + "'", cn); 
      cmd.ExecuteNonQuery(); 

的第一個問題是sql語法我需要將文件上傳與我buttonclick結合,從而這將是INSERT INTO兩張表用戶和圖片,但親瑕疵之後是如何得到用戶名如果​​帳戶尚未創建? AHHH笑

表結構:

enter image description here

所以總結起來,我需要插入用戶的詳細信息到用戶表,並上傳到項目文件並插入IMAGEURL到圖片表(存儲像所以〜/ userdata/2/uploadedimages/bla.jpg),你可以看到圖片表與用戶表有一個1-1關係,所以它依賴於用戶標識符be4帳戶被創建,沒有用戶標識符,所以不知道是否有一種錯開代碼的方法,以便首先插入用戶詳細信息,然後使用會話檢索該用戶標識,然後將圖像插入到圖片表中?

或者也許有一些時髦的功能,一些聰明的人已經來到這個問題上,或者它只是一個簡單的SQL語法decomobobulator。

P.S我知道SQL注入風險,請不要發佈這個。多謝你們!

編輯:

protected void Button1_Click(object sender, EventArgs e) 
    { 
     if (FileUpload1.HasFile) 
     { 
      try 
      { 
       OdbcConnection cn = new OdbcConnection("Driver={MySQL ODBC 3.51 Driver}; Server=localhost; Database=gymwebsite2; User=root; Password=commando;"); 
cn.Open(); 

       OdbcCommand cmd = new OdbcCommand("INSERT INTO User (Email, FirstName, SecondName, DOB, Location, Aboutme, username, password) VALUES ('" + TextBox1.Text + "', '" + TextBox2.Text + "', '" + TextBox3.Text + "', '" + TextBox4.Text + "', '" + TextBox5.Text + "', '" + TextBox6.Text + "', '" + TextBox7.Text + "', '" + TextBox8.Text + "')", cn); 
       OdbcCommand sc = new OdbcCommand("SELECT LAST_INSERT_ID()", cn); 
       //convert LAST INSERT into string theUserId 

       string filenameDB = Path.GetFileName(FileUpload1.FileName); 
       string fileuploadpath = Server.MapPath("~/userdata/" + theUserId + "/uploadedimage/") + Path.GetFileName(FileUpload1.FileName); 
       FileUpload1.SaveAs(fileuploadpath); 
       string fileuploadpaths = ("~/userdata/" + theUserId + "/uploadedimage/") + filenameDB; 
       Label10.Text = "Upload status: File uploaded!"; 
       OdbcCommand cm = new OdbcCommand("INSERT INTO Pictures (picturepath, UserId) VALUES ('" + fileuploadpaths + "', " + theUserId + ")", cn); 

       cmd.ExecuteNonQuery(); 
      } 
      catch (Exception ex) 
      { 
       Label10.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message; 

      } 
      //e.Authenticated = true; 
      //Response.Redirect("Login.aspx"); 
      // Event useradded is true forward to login 
     } 
    } 
} 

回答

1

如果圖片與用戶是1:1,是否可以將圖片路徑放在用戶表中?

如果不是,MySQL有一個last_insert_id()函數,允許您從表(本例中爲用戶)獲取最後一個自動增量值 - 通常是主鍵。

+0

Nah我不能把它們放到用戶表中,我怎麼才能使用last_insert方法將我的插入到用戶和圖片表中的所有按鈕單擊下? – 2011-04-04 15:30:51

+1

如果您先執行用戶插入操作,則在執行圖片插入操作之前請抓住最後一個插入ID。 'SELECT LAST_INSERT_ID()'確保在與插入相同的數據庫連接中運行它。 – sreimer 2011-04-04 15:33:20

+0

so SELECT LAST_INSERT_ID()FROM User或SELECT LAST_USERID()FROM User? – 2011-04-04 15:44:01

1

您需要從用戶接入返回新用戶ID。從mysql自動增量文檔:

可以檢索與 LAST_INSERT_ID()SQL函數或 mysql_insert_id()C API函數最近 AUTO_INCREMENT值。 這些函數是連接特定的 ,因此它們的返回值 不受其他 連接的影響,該連接也在執行 插入。

無論如何,您需要存儲此返回並將其傳遞到相關操作。