[英]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"]."')";
'
是打开MySQL单引号。 "
在PHP中结束字符串。 .
使用$_POST['stu_name']
加入当前的PHP字符串 .
"
。 '
。 ,
输入第二个值 '
在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.