php mysqli插入和更新查询 [英] php mysqli insert and update queries
问题描述
我正在创建一个反馈表单,允许用户编写他们的反馈信息,并使用php和mysqli将这些反馈信息存储在基于用户名的数据库中
i确实插入了数据,但没有用户名问题是:
当我写更新查询时,我变得无法插入任何数据,任何人都可以帮助我?
feedback_form.php
<?php
session_start();
$ login =($ _SESSION ['login']);
$ userid =($ _SESSION ['user_id']);
$ login_user =($ _SESSION ['username']);
$ fname =($ _SESSION ['first_name']);
$ lname =($ _SESSION ['last_name']);
$ sessionaddres =($ _ SESSION ['address']);
?>
<!DOCTYPE html PUBLIC - // W3C // DTD XHTML 1.0 Transitional // ENhttp://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">
< html xmlns =http://www.w3.org/1999/xhtml>
< head>
< meta http-equiv =Content-Typecontent =text / html; charset = utf-8/>
< title>反馈页面< / title>
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js\">< ;; script>
< link href =style / stylesheet.css =stylesheettype =text / css/>
< script type =text / javascript>
$(function(){
$('#submit')。click(function(){
$('#container')。append('< img src =images /loading.gifalt =目前正在加载id =loading/>');
var comment = $('#comments')。val();
$ .ajax({
url:'feedback_process.php',
type:'POST',
data:{comments:comments},
success:function(result){
$('#response')。remove();
$('#container')。append('< p id =response>'+ result +'< / p>') ;
$('#loading')。fadeOut(500,function(){
$(this).remove();
});
}
});
return false;
});
});
< / script>
< / head>
<?php require_once('header.php'); ?>
< body>
< form action =feedback_form.phpmethod =post>
< div id =container>
< h2><?php echo $ login_user?>< / h2>
< label for =评论>评论< / label>
< br />
< / div>
< / form>
< input type =submitname =submitid =submitvalue =send feedBack/>
< / body>
< / html>
feedback_process.php
<?php
session_start();
$ login =($ _SESSION ['login']);
$ userid =($ _SESSION ['user_id']);
$ login_user =($ _SESSION ['username']);
$ fname =($ _SESSION ['first_name']);
$ lname =($ _SESSION ['last_name']);
$ sessionaddres =($ _ SESSION ['address']);
$ conn = new mysqli('localhost','root','','lam_el_chamel_db');
echo< pre>;
print_r($ _ POST);
echo< / pre>;
if(isset($ _ POST ['comments'])){
$ comments = $ _POST ['comments'];
$ b $ query =插入反馈(feedback_text user_name,)VALUES(?,?);
$ stmt = $ conn-> stmt_init();
if($ stmt-> prepare($ query))
{
$ stmt-> bind_param('ss',$ comments,$ login_user);
// $ stmt-> execute();
$ query2 =更新反馈SET(feedback =?,user_name =?)WHERE user_name ='$ login_user';
$ stmt = $ conn-> stmt_init();
if($ stmt-> prepare($ query))
{
$ stmt-> bind_param('ss',$ comments,$ login_user);
$ stmt-> execute();
if($ stmt){
echo谢谢。我们会尽快与您联系< br />;
}
else {
echo出现错误,请稍后重试。
}
}
其他
回声这是一个很大的错误;
?>
有多个错误/错误。 p>
请参阅以下代码段:
第一个
$ query =INSERT into feedback(feedback_text user_name,)VALUES(?,?);
您没有,
(逗号)分隔领域。使用:
$ query =INSERT into feedback(feedback_text,user_name)VALUES(?,?);
下一页
// $ stmt-> execute();
您没有执行此语句。使用
$ stmt-> execute();
另一个
$ query2 =UPDATE feedback SET(feedback =?,user_name =?)WHERE user_name ='$ login_user';
字段名称在 INSERT
查询。也许尝试
$ query2 =更新反馈SET feedback_text =?,user_name =?WHERE user_name =?;
$ stmt = $ conn-> stmt_init();
if($ stmt-> prepare($ query)){
$ stmt-> bind_param('sss',$ comments,$ login_user,$ login_user);
$ stmt-> execute();
}
i am creating a feedback form that allow users to write their feedback and using php and mysqli i stored these feedback in the database based on the username of the user i did insert data successfully but without username of the user so the problem is : when i wrote an update query i become unable to insert any data can anyone help me ??
feedback_form.php
<?php
session_start();
$login = ($_SESSION['login']);
$userid = ($_SESSION['user_id']);
$login_user = ($_SESSION['username']);
$fname = ($_SESSION['first_name']);
$lname = ($_SESSION['last_name']);
$sessionaddres =($_SESSION['address']);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>feedback page</title>
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<link href="style/stylesheet.css"rel="stylesheet" type="text/css"/>
<script type = "text/javascript">
$(function(){
$('#submit').click(function(){
$('#container').append('<img src = "images/loading.gif" alt="Currently loading" id = "loading" />');
var comments = $('#comments').val();
$.ajax({
url: 'feedback_process.php',
type: 'POST',
data: {"comments": comments},
success: function(result){
$('#response').remove();
$('#container').append('<p id = "response">' + result + '</p>');
$('#loading').fadeOut(500, function(){
$(this).remove();
});
}
});
return false;
});
});
</script>
</head>
<?php require_once('header.php'); ?>
<body>
<form action = "feedback_form.php" method = "post">
<div id = "container">
<h2><?php echo $login_user ?></h2>
<label for = "comments">Comments</label>
<textarea rows = "5"cols = "35" name = "comments" id = "comments"></textarea>
<br />
</div>
</form>
<input type = "submit" name = "submit" id = "submit" value = "send feedBack" />
</body>
</html>
feedback_process.php
<?php
session_start();
$login = ($_SESSION['login']);
$userid = ($_SESSION['user_id']);
$login_user = ($_SESSION['username']);
$fname = ($_SESSION['first_name']);
$lname = ($_SESSION['last_name']);
$sessionaddres =($_SESSION['address']);
$conn = new mysqli('localhost', 'root', '', 'lam_el_chamel_db');
echo"<pre>";
print_r($_POST);
echo"</pre>";
if(isset($_POST['comments'])){
$comments = $_POST['comments'];
$query = "INSERT into feedback (feedback_text user_name,) VALUES(?,?)";
$stmt = $conn->stmt_init();
if($stmt->prepare($query))
{
$stmt->bind_param('ss', $comments, $login_user);
//$stmt->execute();
}
$query2 = "UPDATE feedback SET (feedback = ?, user_name = ?) WHERE user_name = '$login_user' ";
$stmt = $conn->stmt_init();
if($stmt->prepare($query))
{
$stmt->bind_param('ss', $comments, $login_user);
$stmt->execute();
}
if($stmt){
echo "thank you .we will be in touch soon <br />";
}
else{
echo "there was an error. try again later.";
}
}
else
echo"it is a big error";
?>
There are more than one errors/mistakes.
See the following code segments:
First
$query = "INSERT into feedback (feedback_text user_name,) VALUES(?,?)";
You don't have a ,
(comma) separating the fields. Use:
$query = "INSERT into feedback (feedback_text, user_name) VALUES(?, ?)";
Next
//$stmt->execute();
You are not executing this statement. Use
$stmt->execute();
Another
$query2 = "UPDATE feedback SET (feedback = ?, user_name = ?) WHERE user_name = '$login_user' ";
The field name is different here and above in INSERT
query. Maybe try
$query2 = "UPDATE feedback SET feedback_text = ?, user_name = ? WHERE user_name = ? ";
$stmt = $conn->stmt_init();
if($stmt->prepare($query)) {
$stmt->bind_param('sss', $comments, $login_user, $login_user);
$stmt->execute();
}
这篇关于php mysqli插入和更新查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!