mysql 一次提交多行

发布于 2024-11-02 05:49:15 字数 1630 浏览 6 评论 0原文

我有一个 php 生成的表单:

 <form action='step2.php' method="post" name='frm<? echo $socialid; ?>' id="frm_step2">
       <?
      $result = mysql_query("SELECT * FROM socials WHERE network not in (SELECT network FROM networks WHERE user='$_SESSION[user_id]') ");

while($socialrow = mysql_fetch_assoc($result)){

    $socialid=$socialrow['id'];
    $socialnet=$socialrow['network'];
    ?>
   <a href="#" class="edit_<? echo $socialnet;?>"><img src="smallicons/<? echo $socialnet;?>.png" width="30" alt="<? echo $socialnet;?>" /></a>
    <div class="table_<? echo $socialnet;?>">the full url to your <? echo $socialnet;?> profile:<br />
    <input type='hidden' value='<? echo $socialnet;?>' name='network'/>
     <input type='text' name='urltonet'/> <br/>
    </div>

<?
}
    ?>
  <br />  <input type='submit' value='enter' />
  </form>  

基本上,对于数据库中的每个社交网络,它都会创建一个表单,用户需要在其中填写其个人资料的 url 因此,如果用户仍然需要填写 2 个网络 url,它将显示两个表单,每个表单有一个提交按钮,

因此,如果用户填写两个文本框,则需要将这些值插入同一个数据库中,

任何想法或帮助请问如何插入这些值? 在我的数据库下面:

CREATE TABLE IF NOT EXISTS `networks` (
`user` int(30) NOT NULL,
`network` varchar(30) NOT NULL,
`username` varchar(30) NOT NULL default 'not known',
`url` varchar(300) NOT NULL,
`user_name` varchar(30) NOT NULL,
`netid` int(11) NOT NULL auto_increment,
PRIMARY KEY (`netid`),
UNIQUE KEY `netid` (`netid`),
UNIQUE KEY `netid_2` (`netid`),
UNIQUE KEY `netid_3` (`netid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=239 ;

i have a php generated form:

 <form action='step2.php' method="post" name='frm<? echo $socialid; ?>' id="frm_step2">
       <?
      $result = mysql_query("SELECT * FROM socials WHERE network not in (SELECT network FROM networks WHERE user='$_SESSION[user_id]') ");

while($socialrow = mysql_fetch_assoc($result)){

    $socialid=$socialrow['id'];
    $socialnet=$socialrow['network'];
    ?>
   <a href="#" class="edit_<? echo $socialnet;?>"><img src="smallicons/<? echo $socialnet;?>.png" width="30" alt="<? echo $socialnet;?>" /></a>
    <div class="table_<? echo $socialnet;?>">the full url to your <? echo $socialnet;?> profile:<br />
    <input type='hidden' value='<? echo $socialnet;?>' name='network'/>
     <input type='text' name='urltonet'/> <br/>
    </div>

<?
}
    ?>
  <br />  <input type='submit' value='enter' />
  </form>  

basicly for every social network in the database it creates a form where the user needs to fill in their url to their profile
so if the user still has to fill in 2 network urls, it will show two forms , one for each with one submit button

so if the user fills in both text boxes it will need to insert these values in the same database

any idea or help on how to insert these values please?
below my database:

CREATE TABLE IF NOT EXISTS `networks` (
`user` int(30) NOT NULL,
`network` varchar(30) NOT NULL,
`username` varchar(30) NOT NULL default 'not known',
`url` varchar(300) NOT NULL,
`user_name` varchar(30) NOT NULL,
`netid` int(11) NOT NULL auto_increment,
PRIMARY KEY (`netid`),
UNIQUE KEY `netid` (`netid`),
UNIQUE KEY `netid_2` (`netid`),
UNIQUE KEY `netid_3` (`netid`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=239 ;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

千鲤 2024-11-09 05:49:15

就语法而言,您可以使用它,不知怎的,我感觉这不是您想要知道的

insert into networks (user,network,url) values (1,'network1','username1'), (1,'network2','title2')

as far as syntax goes it's you can use this, somehow i have feeling that is not what you wnat to know

insert into networks (user,network,url) values (1,'network1','username1'), (1,'network2','title2')
第几種人 2024-11-09 05:49:15

SQL Server 和 Oracle 上的技巧是使用union all

INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5

我想这也适用于 MySQL

The trick on SQL Server and Oracle is to use union all

INSERT INTO MyTable (FirstCol, SecondCol)
SELECT 'First' ,1
UNION ALL
SELECT 'Second' ,2
UNION ALL
SELECT 'Third' ,3
UNION ALL
SELECT 'Fourth' ,4
UNION ALL
SELECT 'Fifth' ,5

I guess this would also work on MySQL

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文