嗨我试图将PHP代码连接到mysql到会话用户,但我得到错误,我不知道是谁。 我测试了这个没有运气
SELECT * FROM .$_SESSION ["username"] WHERE Datouid......
SELECT * FROM .$_SESSION ["username"]. WHERE Datouid......
SELECT * FROM $_SESSION ['username'] WHERE Datouid......
SELECT * FROM .$_SESSION ['username']. WHERE Datouid......
SELECT * FROM $_SESSION ['username']. WHERE Datouid......
SELECT * FROM $_SESSION = ['username']. WHERE Datouid......
SELECT * FROM '$_SESSION = ['username']' WHERE Datouid......
SELECT * FROM '$_SESSION ['username']' WHERE Datouid......
我收到此错误:
注意:数组转换为字符串 第43行/home/ar/public_html/TL/inc/incinput1.php 致命错误:未捕获mysqli_sql_exception:您的SQL中有错误 句法;查看与您的MariaDB服务器版本对应的手册 在' ['用户名']附近使用正确的语法。在哪里Datouid =' 1''在第1行
这是我需要提供错误的代码
$servername = "";
$username="";
$password="";
$dbname="";
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
//connect to mysql database
try{
$conn =mysqli_connect($servername,$username,$password,$dbname);
}catch(MySQLi_Sql_Exception $ex){
echo("error in connecting");
}
//get data from the form
function getData()
{
$data = array();
$data[0]=$_POST['Datouid'];
$data[1]=$_POST['Prosjekt'];
$data[2]=$_POST['Bruker'];
$data[3]=$_POST['Dag'];
$data[4]=$_POST['Dato'];
$data[5]=$_POST['Timer'];
$data[6]=$_POST['Km'];
$data[7]=$_POST['Uke'];
$data[8]=$_POST['Mnd'];
$data[9]=$_POST['Innstempling'];
$data[10]=$_POST['Utstempling'];
return $data;
}
//search
if(isset($_POST['search']))
{
$info = getData();
$search_query="SELECT * FROM $_SESSION = ['username']. WHERE Datouid =
'$info[0]'";
$search_result=mysqli_query($conn, $search_query);
if($search_result)
{
if(mysqli_num_rows($search_result))
{
while($rows = mysqli_fetch_array($search_result))
{
$Datouid = $rows['Datouid'];
$Prosjekt = $rows['Prosjekt'];
$Bruker = $rows['Bruker'];
$Dag = $rows['Dag'];
$Dato = $rows['Dato'];
$Timer = $rows['Timer'];
$Km = $rows['Km'];
$Uke = $rows['Uke'];
$Mnd = $rows['Mnd'];
$Innstempling = $rows['Innstempling'];
$Utstempling= $rows['Utstempling'];
}
}else{
echo("no data are available");
}
} else{
echo("result error");
}
}
//insert
if(isset($_POST['insert'])){
$info = getData();
$insert_query="INSERT INTO `$_SESSION = ['username']`(`Prosjekt`, `Bruker`,
`Dag`, `Dato`, `Timer`, `Km`, `Uke`, `Mnd`, `Innstempling`, `Utstempling`)
VALUES
('$info[1]',
'$info[2]','$info[3]','$info[4]','$info[5]','$info[6]','$info[7]','$
info[8]','$info[9]','$info[10]')";
try{
$insert_result=mysqli_query($conn, $insert_query);
if($insert_result)
{
if(mysqli_affected_rows($conn)>0){
echo("data inserted successfully");
}else{
echo("data are not inserted");
}
}
}catch(Exception $ex){
echo("error inserted".$ex->getMessage());
}
}
//delete
if(isset($_POST['delete'])){
$info = getData();
$delete_query = "DELETE FROM `$_SESSION = ['username']` WHERE Datouid =
'$info[0]'";
try{
$delete_result = mysqli_query($conn, $delete_query);
if($delete_result){
if(mysqli_affected_rows($conn)>0)
{
echo("data deleted");
}else{
echo("data not deleted");
}
}
}catch(Exception $ex){
echo("error in delete".$ex->getMessage());
}
}
//edit
if(isset($_POST['update'])){
$info = getData();
$update_query="UPDATE `$_SESSION = ['username']` SET
`Bruker`='$info[2]',Timer='$info[5]'
,Dag='$info[3]',Dato='$info[4]',
Km='$info[6]',Prosjekt='$info[1]',Uke='$info[7]'
,Mnd='$info[8]',Innstempling='$info[9]',Utstempling='$info[10]'WHERE Datouid
= '$info[0]'";
try{
$update_result=mysqli_query($conn, $update_query);
if($update_result){
if(mysqli_affected_rows($conn)>0){
echo("data updated");
}else{
echo("data not updated");
}
}
}catch(Exception $ex){
echo("error in update".$ex->getMessage());
}
}
{unset($_POST);}
答案 0 :(得分:-4)
检查出来:
$username = $_SESSION['username'];
$search_query="SELECT * FROM $username WHERE Datouid = '$info[0]'";