mysql insert 多表,多次运行MySQL INSERT查询(将值插入到多个表中)

basically, I have 3 tables; users and projects, then I have 'users_projects' to allow the one-to-many formation. When a user adds a project, I need the project information stored and then the 'userid' and 'projectid' stored in the usersprojects table. It sounds like its really straight forward but I'm having problems with the syntax I think!?

As it stands, I have this as my INSERT queries (values going into 2 different tables):

$projectid = $_POST['projectid'];

$pname = $_POST['pname'];

$pdeadline = $_POST['pdeadline'];

$pdetails = $_POST['pdetails'];

$userid = $_POST['userid'];

$sql = "INSERT INTO projects (projectid, pname, pdeadline, pdetails) VALUES

('{$projectid}','{$pname}','{$pdeadline}','{$pdetails}')";

$sql = "INSERT INTO users_projects (userid, projectid) VALUES

('{$userid}','{$projectid}')";

$result = mysql_query($sql, $connection)

or die("MySQL Error: ".mysql_error());

header("Location: frontview.php");

exit();

解决方案

You simply forgot to execute the sql between each query. Add the

mysql_query($sql, $connection)

or die("MySQL Error: ".mysql_error());

between each query and you are supposed to be fine.

b.t.w (1) it always helpful to test with a console open with tail -f on the sql log (under /var/log/mysql/ )

b.t.w.(2) You are having heavy security issues in your code.

b.t.w (3) You might want to consider using PDO/Mysqli and not the old mysql extension.

b.t.w (4) It would make your life simpler to use some kind of wrapper (a good class) to approach the DB and not do it directly everywhere in your code.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值