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

时间:2015-11-27 22:25:40

标签: php mysql database forms

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

背景资料:

  • 包含表单的HTML文档,允许管理员上传图片路径,照片名称和评论
  • PHP文档,包含运行此代码的SQL。我已经研究出如何插入图像路径,名称或评论,但不能同时插入所有三个...

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

$ sql =" INSERT INTO image_tbl(image,name,comment)VALUES(' {$ _ POST {[VALUES GO HERE,WHAT SYNTAX / HOW ???]}')& #34 ;;

HTML表单在这里(对任何感兴趣的人):

cProfile

PHP Doc(整个上传文件,非常小,更改了安全xD的密码):

<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>

3 个答案:

答案 0 :(得分:1)

$image = $_POST['image'];
$name = $_POST['name'];
$comment = $_POST['comment'];


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

答案 1 :(得分:0)

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

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

答案 2 :(得分:0)

分离你的变量$ imgpath = $ _ POST [&#34; path&#34;]; $ imgname = $ _ POST [&#34;名称&#34;]; $ imgcomment = $ _ POST [&#34;评价&#34;];现在插入表格.... INSERT INTO imgtable(路径,名称,评论)VALUES(&#39; $ imgpath&#39;,&#39; $ imgname&#39;,&#39; $ imgcomment&#39;)