简体   繁体   English

将数据插入数据库php错误

[英]Insert data to database php error

Be kind, I am still learning, I have made this code but it didn't work, I have tried everything, it is suppose to when an user is logged in and someone send an image the member name is recorded at the database: 友善,我仍在学习,我编写了这段代码,但是没有用,我已经尝试了所有方法,这是假设当用户登录并向某人发送图像时,其成员名称记录在数据库中:

if (!isset($_SESSION[sforum_.'sforum_logged_in']));

  if(isset($_SESSION[sforum_.'sforum_user_username'])) {
         mysqli_query(INSERT INTO media (membro) VALUES ('$_SESSION[sforum_.'sforum_user_username']'));
      } else {
         Echo "erro"; 
  } 

It gives an error: 它给出了一个错误:

Parse error: syntax error, unexpected T_STRING 解析错误:语法错误,意外的T_STRING

You have 2 issues here. 您在这里有2期。 You need to quote your SQL in the query, and mysqli_query takes TWO parameters: the connection object link and the sql to run. 您需要在查询中引用SQL, mysqli_query带有两个参数:连接对象链接和要运行的sql。

mysqli_query($yourDbConnectionString, "INSERT INTO media (membro) 
VALUES ('" . $_SESSION[sforum_ . 'sforum_user_username'] . "')");

Obviously substitute $yourDbConnectionString with the actual value; 显然用实际值替换$yourDbConnectionString ; you didn't include it in the code sample. 您没有将其包括在代码示例中。

Try this code 试试这个代码

<?php
    if (!isset($_SESSION[sforum_ . 'sforum_logged_in'])) {
        if (isset($_SESSION[sforum_ . 'sforum_user_username'])) {
            mysqli_query("INSERT INTO media (membro) VALUES ('" . $_SESSION[sforum_ . 'sforum_user_username'] . "')");
        } else {
            echo "error";
        }
    }
?>
if(!isset($_SESSION['sforum_logged_in'])) ;
if(isset($_SESSION['sforum_user_username'])) {
    $query = "INSERT INTO media(membro) VALUES (`" . $_SESSION['sforum_user_username'] . "`)";
    mysqli_query($dbConnect, $query); //$dbConnect is database connected object
}
else {
    echo "erro";
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM