2017-05-30 439 views
1

INSERT INTO不会运行,除非发现$ re未定义,否则不会出现错误。它曾经工作过,但不再有效。MySQL查询不运行

$servername = "localhost"; 
$username = "id728908_njaohnt"; 
$password = "pass"; 
$dbname = "id728908_sub"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
     die("Connection failed: " . $conn->connect_error); 
} 
if (!$resu=$conn->query("SELECT confirmed FROM subscribers WHERE 
email='".$email."'")) 
{echo $conn->error;} 

while ($row = $resu->fetch_assoc()) { 
$resul=$row['confirmed'];} 
$sql = "INSERT INTO subscribers (email) VALUES ('".$email."')"; 

    if ($resul==null || $resul==0) 
    { 
    if ($resul!=0) 
{ 
    if ($conn->query($sql) === TRUE) 
    { 
$res=$conn->query("SELECT A_I FROM subscribers WHERE email='".$email."'"); 
while ($row = $res->fetch_assoc()) { 
$re=$row['A_I']; 
... 

$电子邮件是 $电子邮件= @trim(的stripslashes($ _ GET [ '电子邮件']));

+0

您应该添加代码来检查INSERT INTO上的错误。另外考虑使用PDO而不是mysqli。 –

+0

Mysqli是好的,但你确实需要使用参数化查询 – Strawberry

+0

请使用预先准备好的语句以及'$ email'在哪里或什么地方? –

回答

1

$ email定义在哪里?如果这是用户输入的,那么确保在将其放入SQL字符串之前对其进行了清理。

重构此:

if ($resul==null || $resul==0) 
{ 
    if ($resul!=0) 
    { 

这样:

if ($resul==null) { 

其它条件是无用的。

消息“$ re is undefined”在行“$ re = $ row ['A_I'];”?

+0

re是未定义的,它将$ re放入一个字符串中。 “$ text。”$ re。“more text” – njaohnt

+0

最终,如果($ resul!= 0)停止了经历......我不确定为什么,因为它曾经工作过,但我一定会简化我的代码在下次尝试其他事情之前! – njaohnt