繁体   English   中英

将此SQL查询更改为php

[英]Change this SQL query to php

我有以下MySQL查询需要传递给query() 我无法理解它。

INSERT INTO admin (student_name, student_email, student_city) VALUES ('mark','mark@mark.com','newark');

我从中获取脚本的地方给出了以下内容,

$sql = "INSERT INTO students (student_name, student_email, student_city) VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";

我理解的部分是('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')

那里发生了什么? 所有那些引号和句号都让我感到困惑。

这里使用的是连接SQL . 用PHP。

所以,让我们来看看这个:

// 12        3          45678
// vv        v          vvvvv
  ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";
  1. 在括号之后,单引号'是打开MySQL单引号。
  2. 然后双引号"在PHP中结束字符串。
  3. 然后,您使用PHP . 使用$_POST['stu_name']加入当前的PHP字符串
  4. 然后使用它将它连接到另一个PHP字符串.
  5. 使用双引号打开PHP字符串"
  6. 最后一旦打开,你需要关闭你打开的MySQL字符串'
  7. 逗号,输入第二个值
  8. 单引号'在MySQL中打开一个字符串。 然后这个过程重复进行。

这是一个很好的评论:

('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";

整个查询需要用双引号扭曲,但是当你想要连接一个变量 - >时

('".$_POST["stu_name"] <-- this part is leaving the query as
('Value
('".$_POST["stu_name"]."', <-- this part is leaving the query as
('Value',

逗号中的每个值都需要在它们的两边连接成两个单引号,因此单引号符号。 每个点(。)将变量连接到现有字符串并返回到字符串中。

试试这个,你只有引号:

["stu_name"] chnaged this to ['stu_name']

$sql = "INSERT INTO students (student_name, student_email, student_city) VALUES ('".$_POST['stu_name']"','".$_POST['stu_email']."','".$_POST['stu_city']."')";

如果使用POST方法

$stu_name = $_POST["stu_name"]          //mark
$stu_email = $_POST["stu_email"]        //mark@mark.com
$stu_city = $_POST["stu_city"]         //newark

$sql = "INSERT INTO students (student_name, student_email, student_city) VALUES ('$stu_name','$stu_email','$stu_city')";

以上同样如此

$sql = "INSERT INTO admin (student_name, student_email, student_city) VALUES ('mark','mark@mark.com','newark')";

当你在我的SQL查询数据库中插入一个字符串时,你必须加上“或”字符

根据您的问题,查询子句是:

 $sql = "INSERT INTO students (student_name, student_email, student_city) VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')"; 

$_POST["stu_name"]$_POST["stu_email"]$_POST["stu_city"]是使用$_POST方法通过表单收到的变量

最好的问候,Tuyen

只需在查询后面添加一行即可

echo "INSERT INTO students (student_name, student_email, student_city) VALUES ('".$_POST["stu_name"]."','".$_POST["stu_email"]."','".$_POST["stu_city"]."')";

它将使用值打印SQL查询。 注意'在值中。 在这里,您将字符串值传递给表,因此您使用'和逗号分隔值。 希望这能帮助您快速理解。

注意:请勿在生产服务器上使用它。 在本地服务器上使用它。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM