[英]php form data not being submitted in database correctly
my database name is login and has a table called users three columns:name,id and post. 我的数据库名称是login,并且有一个名为users的表,该字段分为三列:name,id和post。 when I hit the submit button, the id is incremented and a new record created but no actual data is created in these fields.
当我按下“提交”按钮时,id会增加,并会创建新记录,但在这些字段中不会创建任何实际数据。 please help
请帮忙
<?php
$servername="localhost";
$username="root";
$password="";
$dbname="login";
$conn=new mysqli($servername,$username,$password,$dbname);
if($conn->connect_error){
die("connection error:".$conn->connect_error);
}else{
echo 'connected successfully';
}
#defining varriables
$user_name = isset($_POST['user_name']);
$post = isset($_POST['post']);
#connect this to login details and pick the userrname
$sql="INSERT INTO posts(user_name,post)
VALUES('$user_name','$post')";
if($conn->query($sql)===TRUE){
echo "your post was published";
}?>
form data 表格数据
<form action="" method="post">
<input type="text" name="user_name"/>
<input type="text" name="post"/>
<input type="submit" name="post to cdrone"/>
</form>
PHP's isset()
returns a boolean value. PHP的
isset()
返回一个布尔值。 So your $user_name
and $post
variables are each going to be set to either true
or false
, rather than the values posted from your form. 因此,您的
$user_name
和$post
变量都将被设置为true
或false
,而不是从表单中发布的值。
Consider setting the values conditionally, depending on the value of isset()
. 考虑根据
isset()
的值有条件地设置值。
I prefer using the ternary operator to include some validation: 我更喜欢使用三元运算符来包含一些验证:
if (!empty($_POST)) {
$user_name = !empty(trim($_POST['user_name'])) ? trim($_POST['user_name']) : false;
$post = !empty(trim($_POST['post'])) ? trim($_POST['post']) : false;
if (!$user_name) {
echo "User Name is required.";
} elseif (!$post) {
echo "Post is required.";
} else {
// insert into database
}
}
The expression (expr1) ?
表达式(expr1)? (expr2) : (expr3) evaluates to expr2 if expr1 evaluates to TRUE, and expr3 if expr1 evaluates to FALSE.
(expr2):(expr3)如果expr1计算为TRUE,则计算为expr2,如果expr1计算为FALSE,则为expr3。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.