问题描述
基本上,我有 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(将值插入多个表)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!



大气响应式网络建站服务公司织梦模板
高端大气html5设计公司网站源码
织梦dede网页模板下载素材销售下载站平台(带会员中心带筛选)
财税代理公司注册代理记账网站织梦模板(带手机端)
成人高考自考在职研究生教育机构网站源码(带手机端)
高端HTML5响应式企业集团通用类网站织梦模板(自适应手机端)