简体   繁体   English

代码不会插入数据库

[英]Code won't insert into database

I have the following code that should collect the filled values from a former page and insert them in a MySQLi database. 我有以下代码,应从前一页收集填充值并将其插入MySQLi数据库。 This does not work and I only get a blank page as a result, without any messages. 这是行不通的,结果我只得到空白页,没有任何消息。 I can't figure out what I'm doing wrong. 我不知道我在做什么错。

 <?php ini_set('display_errors',1); error_reporting(E_ALL); if(mysqli_connect_errno()) { echo mysqli_connect_error(); } $company_name = $_POST['company_name']; $description = $_POST['description']; $welcome_text = $_POST['welcome_text']; $thanks_message = $_POST['thanks_message']; $image = addslashes (file_get_contents($_FILES['image']['tmp_name'])); $logo = getimagesize($_FILES['image']['tmp_name']); $image_type = $logo['mime']; $q = "INSERT INTO project VALUES('','$company_name','$description','$image','$image_type','$welcome_text','$thanks_message')"; $r = mysqli_query($mysqli,$q); if($r) { echo "<h1>Projektet är skapat!</h1><br> Tryck på knappen nedan för att ta dig till Dashboard.<br><br> <a href='dashboardadmin.php'><button id='projectbutton'>Dashboard</button></a>"; } else { echo mysqli_errno($mysqli) . ": " . mysqli_error($mysqli) . "\\n"; } ?> 

Correct syntax of INSERT is: INSERT的正确语法是:

INSERT INTO table_name (column1,column2,column3,...) VALUES (value1,value2,value3,...);

Please try entering column names before your values first. 请尝试在输入值之前先输入列名称。 Also check your $_POST values, whether $_FILES['image'] is available and confirm your mysqli connection. 还要检查您的$_POST值, $_FILES['image']是否可用,并确认您的mysqli连接。

Edits: 编辑:

Is the first value (empty one) your primary key? 第一个值(空值)是您的主键吗? If so, can you omit that bit in your code and try again? 如果是这样,您可以在代码中忽略该位,然后重试吗? (Assuming pid is integer and auto incrementing value.) (假设pid是整数,并且是自动递增的值。)

INSERT INTO project (project_name, description, image, image_type, welcome_text, thanks_message) VALUES('$company_name','$description','$image','$image_type','$welcome_text',‌​'$thanks_message')

Somehow I don't think this would be Azure specific issue as per your comment. 按照您的评论,以某种方式我认为这不是Azure特定的问题。

Can you see any errors in logs etc? 您可以在日志等中看到任何错误吗? Also try echoing the query before you run it and check if you run it directly on your phpmyadmin etc to see if it'd work. 另外,在运行查询之前,请尝试回显该查询,并检查是否直接在phpmyadmin等上运行它,以查看其是否有效。

Please also try using echo mysqli_errno($mysqli) . ": " . mysqli_error($mysqli) . "\\n"; 也请尝试使用echo mysqli_errno($mysqli) . ": " . mysqli_error($mysqli) . "\\n"; echo mysqli_errno($mysqli) . ": " . mysqli_error($mysqli) . "\\n"; at if($r){..} else { //here } to see if you get an error. if($r){..} else { //here }查看是否收到错误。

Latest Update: 最新更新:

$q = "INSERT INTO project (project_name, description, image, image_type, welcome_text, thanks_message) VALUES('".$company_name."','".$description."','".$image."','".$image_type."','".$welcome_text."','".$thanks_message."')";

尝试此操作,因为您的主键值是自动递增的。

$q = "INSERT INTO project VALUES('$company_name','$description','$image','$image_type','$welcome_text','$thanks_message')";

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

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