嗨,每当我执行将表单提交到数据库时,我都会遇到问题。它确实将数据提交到表单,但这是一个错误,而不是插入的数据。 index.php
<div id="postForm">
<!-- Username of the user posting the Language Learning Object -->
<div id="postUserName" data-role="field-contain">
<label for="Username of user">Username of user </label>
<input type="text" name="post_username" value="<?php echo $_SESSION['post_username']; ?>" readonly="readonly" id="post_username"/>
</div>
<!-- Word of the language learning object -->
<div id="postWord" data-role="field-contain">
<label for="Word for Language Learning Object">Word for Language Learning Object </label>
<input type="text" name="post_word" id="post_word" />
</div>
<!-- Description of the word -->
<div id="postDescription" data-role="field-contain">
<label for="Word description">Word description </label>
<textarea name="word_description" id="word_description"></textarea>
</div>
<!-- Categorization of the given word -->
<div id="postCategorization" data-role="field-contain">
<label for="Categorization of Word">Categorization of Word </label>
<select name="word_categorization" id="word_categorization">
<option value=""> Select </option>
<option value="Adverb"> Adverb </option>
<option value="Adjective"> Adjective </option>
<option value="Noun"> Noun </option>
<option value="Verb"> Verb </option>
</select>
</div>
<!-- Origin of the given word -->
<div id="postOrigin" data-role="field-contain">
<label for="Origin of Word">Origin of Word </label>
<input type="text" name="word_origin" id="word_origin" />
</div>
<!-- Getting the user's location -->
<div id="userLocation" data-role="field-contain">
<label for="user location">User location</label>
<input type="text" name="user_loc" value="<?php echo $geo->city;?>" />
</div>
<!-- Submit Button -->
<div id="submitPost" data-role="field-contain">
<button name="submit_post" type="submit" id="submit_post">Create post</button>
</div>
</div> <!-- end of Form -->
post.php
<?php
include_once( 'connect.php' );
class ManagePost {
protected $link;
/* Connection to database */
function __construct(){
$db_connect = new ManageDatabase;
$this->link = $db_connect->databaseconnect();
return $this->link;
}
/* creating language learning objects/posts */
function addPost($username,$word,$description,$categorization,$origin,$user_loc,$date,$time) {
$query = $this->link->prepare("INSERT INTO post (username, word, description, categorization, origin, user_loc, date, time) VALUES (?,?,?,?,?,?,?,?)");
$values = array($username,$word,$description,$categorization,$origin,$user_loc,$date,$time);
$query->execute($values);
$rowCount = $query->rowCount();
return $rowCount;
}
}
dbpost.php
<?php
session_start();
include('../class/post.php');
$post_init = new ManagePost;
if($_POST)
{
$username = $_POST['post_username'];
$word = $_POST['post_word'];
$description = $_POST['word_description'];
$categorization = $_POST['word_categorization'];
$origin = $_POST['word_origin'];
$user_loc = $_POST['user_loc'];
$date = date("Y-M-D");
$time = date ("H:i:s");
$submit_post = $post_init->addPost($username, $word, $description, $categorization, $origin, $user_loc, $date, $time);
if($submit_post == 1)
{
echo 'true';
}
else
{
echo 'failed to submit';
}
}
?>
createpost.js
$('form').submit(function() {
if(validateWord() && validateDescription() && validateCategorization() && validateOrigin())
{
$.post('lib/dbpost.php', $('form').serialize(), function(response) {
if(response == 'true')
{
showSuccess('Post submitted');
}
else
{
showError(response);
}
})
}
});
当我检查 phpMyAdmin 中的表帖子时,它只是显示为这样的错误
<br /><font size='1'><table class='xdebug-error xe
在属性中
"word, description, origin, and user_loc"
最佳答案
您可以尝试更改 add_post 函数,如下所示:
function addPost($username, $word, $description, $categorization, $origin, $user_loc, $date,$time){
$query = $this->link->prepare("INSERT INTO post (username,word,description,categorization,origin,user_loc,date,time)
VALUES (:username,:word,:description,:categorization,:origin,:user_loc,:date,:time)");
$query->bindParam(':username', $username);
$query->bindParam(':word', $word);
$query->bindParam(':description', $description);
$query->bindParam(':categorization', $categorization);
$query->bindParam(':origin', $origin);
$query->bindParam(':user_loc', $user_loc);
$query->bindParam(':date', $date);
$query->bindParam(':time', $time);
$query->execute();
$rowCount = $stmt->rowCount();
return $rowCount;
}
关于php - 将数据插入数据库返回为 <br/> <font size ='1' ><table class='xdebug-error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29813469/