多次运行 MySQL INSERT Query(将值插入多个表)

Run MySQL INSERT Query multiple times (insert values into multiple tables)(多次运行 MySQL INSERT Query(将值插入多个表))
本文介绍了多次运行 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(将值插入多个表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)