多次运行MySQL INSERT查询(将值插入到多个表中) [英] Run MySQL INSERT Query multiple times (insert values into multiple tables)

查看:553
本文介绍了多次运行MySQL INSERT查询(将值插入到多个表中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我有3张桌子;用户和项目,那么我有"users_projects"来允许一对多的形式.当用户添加项目时,我需要存储项目信息,然后将'userid'和'projectid'存储在usersprojects表中.听起来确实很简单,但是我认为语法有问题!?

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!?

就目前而言,我将其作为INSERT查询(将值放入2个不同的表中):

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();

推荐答案

您只是忘记了在每个查询之间执行sql.添加

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)使用在SQL日志(在/var/log/mysql/下)中的tail -f打开的控制台进行测试总是很有帮助的. b.t.w.(2)您的代码中存在严重的安全性问题.
b.t.w(3)您可能要考虑使用PDO/Mysqli,而不是旧的mysql扩展. b.t.w(4)使用某种包装器(一个好的类)来访问数据库,而不是直接在代码中的任何地方都可以使您的工作变得更简单.

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.

这篇关于多次运行MySQL INSERT查询(将值插入到多个表中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆