dsapkqaduj6718493 2017-03-27 12:16 采纳率: 100%
浏览 51
已采纳

Joomla用户插入mysql

I've made a form and would like the data inserted into my database to attache to the user, who's logged in at my website. I've used this form:

<form action="/index.php?option=com_content&view=article&id=41" method="post">

<input type="hidden" name="username" id="username" value="username">

<input type="radio" name="k1" id="k1" value="1">1<input type="radio" name="k1" value="X">X<input type="radio" name="k1" value="2">2<br />

<input type="text" name="k1r" id="k1r">

<div>

<p style="float:left; width:14%; margin-right:2%; background:#eee; color:black; border:1px solid #ccc; padding:10px"><input type="submit" value="Add Records"></p>

</div>

</form>

Ant the 'action' would get this code:

// Check connection
if($mysqli === false){
die("ERROR: Could not connect. " . $mysqli->connect_error);
}

// Escape user inputs for security

$username = $mysqli->real_escape_string($_SESSION['username']);

$k1 = $mysqli->real_escape_string($_POST['k1']);
$k1r = $mysqli->real_escape_string($_POST['k1r']);

// attempt insert query execution
$sql = "INSERT INTO kampbs(username, k1, k1r) VALUES('$user', '$k1', '$k1r')";

if($mysqli->query($sql) === true){
echo "Records inserted successfully.";
} else{
echo "ERROR: Could not able to execute $sql. " . $mysqli->error;
}


// Close connection
$mysqli->close();
?>

The form-data is inserted as it should, but in the 'Username' field in the database it says 'JUser'. What should I do to get the Logged-in username (should've been the username 'DJ').

  • 写回答

1条回答 默认 最新

  • dousi6303 2017-03-27 13:56
    关注

    I actually did it now, thanks to @Bhaskar 's link. Joomla got it's own way to retrieve user-information:

    $user = JFactory::getUser();
    

    Afterwards I made the variable:

    $username = $user->username;
    

    And at last I just inserted it into the $sql like:

    $sql = "INSERT INTO kampbs(username, k1, k1r) VALUES('$username', '$k1', '$k1r')";
    

    That did it! :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法