我正在尝试使用此代码,但数据未保存并且正在抛出错误
<?php
include 'core/int.php';
include 'includes/overall/header.php';
if(!empty($_POST)){
$add_status = $insert->add_status($user_data['user_id']);
}
?>
<form action = "" method = "POST">
<ul>
<li>
<input name = "question_time" type = "hidden" value = "<?php echo time()?>" />
</li>
<li>
<h2>Post your Question <?php echo $user_data['username'];?></h2>
<textarea name = "question"></textarea>
</li>
<li>
<p><input type = "submit" value = "submit"></p>
</li>
</ul>
</form>
并且函数定义如下:
function add_status($user_id){
mysql_query("INSERT into `post` (user_id, status_time, status_content) VALUES($user_id, '$_POST[question_time]', '$_POST[question]')");
}
投掷和错误
未定义的变量:在第7行的C:\ wamp \ www \ zr \ sheet.php中插入
在第7行的C:\ wamp \ www \ zr \ sheet.php中的非对象上调用成员函数add_status()
第7行是“$ add_status = $ insert-&gt; add_status($ user_data ['user_id']);”
我是PHP和MYSQL的新学习者
提前致谢..
答案 0 :(得分:2)
我会这样做:
function add_status($id, $status_time, $status_content)
{
$db = //your db connection
$query = $mysqli_query('INSERT into table WHERE user_id, status_time, status_content VALUES (?, ?, ?)');
$db->bind_param('i,i,s' $id, $status_time, $status_content);
$db->execute();
}
然后将其称为
if(isset($_POST['submit']))
{
$questionTime = $_POST['question_time'];
$question = $_POST['question'];
$allFields = $questionTime . $question;
if (!empty($allFields)) {
$insert->add_status($user_data['user_id'], $questionTime, $question);
}
}
请记住,始终使用预准备语句,绝不使用mysql_
函数。
答案 1 :(得分:1)
首先,您应该使用POST提取。这是理想的方式..