繁体   English   中英

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

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

我一直在为计算项目使用php和mysql,而我刚遇到了这个小问题。 我已经尝试过对这一行代码进行无数种变体,但是我似乎总是收到一个SQL错误。

背景资料:

  • 包含表单的HTML文档,允许管理员上传图像路径,照片名称和注释
  • PHP文档包含运行该代码的SQL。 我已经解决了如何插入图像路径,名称或注释,但不能一次插入所有三个...

这是导致问题的代码行,特别是将数据插入数据库中。

$ sql =“ INSERT INTO image_tbl(图像,名称,注释)值('{$ _POST {[值在这里,语法/方式是什么?]}'')”;

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(整个上传文件,非常小,更改了安全性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')";

您可以将变量设置为关联数组(使用html表单中的名称),并将每个变量都包含在sql查询中。 像这样:

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

分隔变量$ imgpath = $ _ POST [“ path”]; $ imgname = $ _ POST [“ name”]; $ imgcomment = $ _ POST [“ comment”]; 现在插入表中。...插入imgtable(path,name,comment)VALUES('$ imgpath','$ imgname','$ imgcomment')

暂无
暂无

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

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