Run MySQL INSERT Query multiple times (insert values into multiple tables)(多次运行 MySQL INSERT Query(将值插入多个表))
问题描述
基本上,我有 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.
btw (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 Query(将值插入多个表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:多次运行 MySQL INSERT Query(将值插入多个表)
基础教程推荐
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01