简体   繁体   English

通过PHP将数据插入SQL表的更正

[英]Correction on Inserting data into SQL Table via PHP

I've been using php and mysql for my computing project and I've just run into this small problem. 我一直在为计算项目使用php和mysql,而我刚遇到了这个小问题。 I've tried countless numbers of variations to this line of code but I always seem to receive an SQL Error. 我已经尝试过对这一行代码进行无数种变体,但是我似乎总是收到一个SQL错误。

Background Information: 背景资料:

  • HTML Document that contains a form, allowing the Admin to upload an image path, a name for the photo and a comment as well 包含表单的HTML文档,允许管理员上传图像路径,照片名称和注释
  • PHP Document containing SQL that runs the code for this. PHP文档包含运行该代码的SQL。 I've worked out how to insert either an image path, a name or a comment but not all three at once... 我已经解决了如何插入图像路径,名称或注释,但不能一次插入所有三个...

Here's the line of code that causes the problem, specifically inserting the data into the database. 这是导致问题的代码行,特别是将数据插入数据库中。

$sql="INSERT INTO image_tbl (image, name, comment) VALUES ('{$_POST{[ VALUES GO HERE, WHAT SYNTAX/HOW??? ]}')"; $ sql =“ INSERT INTO image_tbl(图像,名称,注释)值('{$ _POST {[值在这里,语法/方式是什么?]}'')”;

The HTML Form is here (for anyone interested): HTML表单在这里(适用于任何有兴趣的人):

<form action="insertTest.php" method="POST" enctype="multipart/form-data">

Image: <input type="text" name="image" /><br>

Name: <input type="text" name="name" /><br>

Comment: <input type="text" name="comment" /><br>

<input type="submit">

</form>

PHP Doc (whole upload file, quite small, changed password for security xD): PHP Doc(整个上传文件,非常小,更改了安全性xD的密码):

<?php

$servername = "localhost";
$username = "root";
$password = "password";
$dbname = "admin_db";


// Create connection
$conn = new mysqli($servername = "localhost", $username = "root", $password = "password", $dbname = "admin_db");
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 


$sql="INSERT INTO image_tbl (image, name, comment) VALUES ('{$_POST{[]}')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>
$image = $_POST['image'];
$name = $_POST['name'];
$comment = $_POST['comment'];


$sql="INSERT INTO image_tbl (image, name, comment) VALUES ('$image', '$name', '$comment')";

you can set variables to the associative array (with name from html form) and include each in the sql query. 您可以将变量设置为关联数组(使用html表单中的名称),并将每个变量都包含在sql查询中。 Something like this: 像这样:

$image = $_POST['image'];
$name = $_POST['name'];
$comment = $_POST['comment'];
$sql="INSERT INTO image_tbl (image, name, comment) VALUES ('$image','$name','$comment')";

Separate your variables $imgpath=$_POST["path"]; 分隔变量$ imgpath = $ _ POST [“ path”]; $imgname=$_POST["name"]; $ imgname = $ _ POST [“ name”]; $imgcomment=$_POST["comment"]; $ imgcomment = $ _ POST [“ comment”]; now insert into table.... INSERT INTO imgtable(path,name,comment) VALUES('$imgpath','$imgname','$imgcomment') 现在插入表中。...插入imgtable(path,name,comment)VALUES('$ imgpath','$ imgname','$ imgcomment')

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

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