![](/img/trans.png)
[英]Insert 2 rows in tables with foreign key relation without knowing the primary key of the main table
[英]insert into multiple tables without knowing the primary key
嘿,伙計們有點復雜,我有一個創建帳戶頁面,它只是將數據插入到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控件,我想上傳一個圖像並將圖像保存在圖片表中:
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語法我需要將fileupload與我的buttonclick結合起來,因此它會INSERT INTO兩個表用戶和圖片,但之后的問題是,如果帳戶尚未創建,我怎么得到用戶ID? AHHH大聲笑
表結構:
總結一下,我需要將用戶詳細信息插入到用戶表中並上傳到項目文件並將imageUrl插入到圖片表中(如此存儲〜/ userdata / 2 / uploadedimages / bla.jpg),如您所見圖片表是與用戶表的1-1關系,因此它依賴於創建帳戶的用戶ID,沒有用戶ID,所以不確定是否有方法錯開代碼,因此首先插入用戶詳細信息然后使用會話檢索該用戶ID然后將imageurl插入圖片表?
或者也許有一些時髦的功能,一些聰明的人已經遇到過這個問題,或者它只是一個簡單的SQL語法decombobulator。
PS我知道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
}
}
}
您需要從用戶插入中返回新的用戶ID。 從mysql自動增量文檔:
您可以使用LAST_INSERT_ID()SQL函數或mysql_insert_id()C API函數檢索最新的AUTO_INCREMENT值。 這些函數是特定於連接的,因此它們的返回值不受另一個也執行插入的連接的影響。
無論如何,您需要存儲此返回並將其傳遞給相關操作。
如果用戶的圖片是1:1,是否可以將圖片路徑放在用戶表中?
如果沒有,MySQL有一個last_insert_id()函數,允許您從表中獲取最后一個自動增量值(在本例中為User) - 通常是主鍵。
我看不到你的表結構,但它就像插入User表一樣簡單,檢索UserID,保留UserID(可能傳遞給查詢字符串上的上傳頁面,或者使用session等),然后使用圖表表中的UserID是否插入? 有關如何從MySQL中的插入行獲取唯一ID的文檔 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.