将PHP mysqli插入表时出错

时间:2018-11-23 14:10:46

标签: php mysql mysqli error-handling

我将表列

log_id
设置为primary_key auto_increment。

我是否需要在INSERT INTO语句中指定它?

尝试将值插入数据库时​​出现下一个错误:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(log_session,log_ip,log_vrijeme,log_model) my_log VALUES ('270043ae1526e4' at line 1 INSERT INTO (log_session,log_ip,log_vrijeme,log_model) my_log VALUES ('270043ae1526e44967889b4382ff69fd','93.142.54.135','2018-11-23 14:06:15','1402')

我的代码是:

% Simple iterations
clc, clear all
A=[ 1 -1  0  0;
-1  2 -1  0;
0  -1  2 -1;
0   0 -1  2]
b=[2;0;0;0]
A=[
 5 11 7 0;
 2 6 17 2;
 2 1  5 1;
 4 12 1 7;
] % coefficient matrix
b=[50; 75; 30; 171] % right-hand side vector
n=size(A,1)
Astarting=A;

method='simple_iterations';
alpha=[100; 20; 1; 1];  % freely chosen method's parameters

Atld=diag(1./diag(A))*A-diag(alpha)
btld=diag(1./diag(A))*b

nitmax=1000;
eps=1e-12;
x=zeros(n,1);x1=zeros(n,1);
fprintf(1,'\n Solving using simple iterations method:'); 
for it=1:nitmax
   x1=(btld-Atld*x)./alpha; 
  end
  prec(it)=norm(x1-x)/(norm(x)+norm(x1));
  fprintf(1,'Iteration Number. %d,  Precision  %g\n',it,prec(it))
  if prec(it) < eps, break, end
  x=x1;
end
x
disp('Check')
Astarting*x-b

semilogy([1:length(prec)],prec,'r.');grid on,hold on

表结构:

<?php
$_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";

$log_session = $_SESSION['compare_hash'];
$log_ip = $_SERVER['REMOTE_ADDR'];
$log_vrijeme = date("Y-m-d H:i:s");
$log_model = "1402";

$con = mysqli_connect("localhost","user","password","databse");
$sql = "
    INSERT INTO (log_session,log_ip,log_vrijeme,log_model) my_log
    VALUES ('$log_session','$log_ip','$log_vrijeme','$log_model')
";
$rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
mysqli_close($con);
?>

谢谢您的帮助。

1 个答案:

答案 0 :(得分:1)

您应将my_log放在INSERT INTO之后:

<?php
$_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";

$log_session = $_SESSION['compare_hash'];
$log_ip = $_SERVER['REMOTE_ADDR'];
$log_vrijeme = date("Y-m-d H:i:s");
$log_model = "1402";

$con = mysqli_connect("localhost","user","password","databse");
$sql = "
    INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)
    VALUES ('$log_session','$log_ip','$log_vrijeme','$log_model')
";
$rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
mysqli_close($con);
?>

但是,您的代码有几个缺点,我将在稍后描述它们。

1.不要将未转义的变量传递给查询

直到您别无选择。使用mysqli_real_escape_string($con, $your_var)可以防止SQL注入,而使用PDO会使情况变得更好。

2.使用HEREDOC语法确保以后的编辑过程中查询的一致性

通过这种方式,即使您当中有人将"进行查询,它也仍然可以按预期运行。

3.如果您在下方没有任何内容,请省略PHP文件末尾的结束标记

这将防止发生意外错误。

<?php
$_SESSION['compare_hash'] = "270043ae1526e44967889b4382ff69fd";
$con = mysqli_connect("localhost","user","password","databse");

$log_session = mysqli_real_escape_string($con, $_SESSION['compare_hash']);
$log_ip = mysqli_real_escape_string($con, $_SERVER['REMOTE_ADDR']);
$log_vrijeme = mysqli_real_escape_string($con, date("Y-m-d H:i:s"));
$log_model = mysqli_real_escape_string($con, "1402");

$sql = <<<SQL
    INSERT INTO my_log (log_session,log_ip,log_vrijeme,log_model)
    VALUES ('${log_session}','${log_ip}','${log_vrijeme}','${log_model}')
SQL;

$rez = mysqli_query($con, $sql) or die(mysqli_error($con)."<br>$sql");
mysqli_close($con);