简体   繁体   English

无法使用PHP将数据插入MySQL

[英]Can't Insert data into MySQL using PHP

I am stumped :( I've done searches and have tried many variations but can't seem to pin point my problem so alas here I am. 我很沮丧:(我已经进行了搜索,并尝试了许多变体,但似乎无法指出我的问题,所以我在这里。

  • First off I'm new at this and I am learning through web searches and experimentation so please before you rip me a new one for being a noob just keep in mind that I am a noob and have admitted it and would rather talk about my solution rather than anything else. 首先,我是新手,我正在通过网络搜索和实验来学习,所以请在您成为新手之前将其撕裂之前,请记住我是新手并已接受它,并且愿意谈论我的解决方案而不是其他任何东西。

Project : Trying to make a little web app that allows me to insert comic book information into a mysql database. 项目 :尝试制作一个小的Web应用程序,使我可以将漫画书信息插入mysql数据库。 I have built the form to accept the data and pass it to my php page which does the insert into mysql. 我已经建立了表单来接受数据并将其传递给我的php页面,该页面将插入mysql。

  • This is a self app / This will not have exposure to the outside world / while I know and am currently developing data checks I need to get info into the DB before collection gets out of hand. 这是一个自我应用程序/不会暴露在外界中/在我知道并且正在开发数据检查的同时,我需要在收集失控之前将信息输入数据库。

Here is the code that I currently have. 这是我当前拥有的代码。 I've tried so many variations that I don't know which way is up.... 我已经尝试了很多变化,以至于不知道哪种方法....

$idcomic_db = $_POST['idcomic_db'];
$publisher = $_POST['publisher'];
$comic_name = $_POST['comic_name'];
$comic_num = $_POST['comic_num'];
$comic_cover = $_POST['comic_cover'];
$price_paid = $_POST['price_paid'];
$quantity = $_POST['quantity'];


$sql_insert = "INSERT INTO `comic_info_db`.`comic_db` (`idcomic_db`, `publisher`, `comic_name`, `comic_num`, `comic_cover`, `price_paid`, `quantity`) VALUES ('$idcomic_db', '$publisher', '$comic_name', '$comic_num', '$comic_cover', '$price_paid', '$quantity')";
$mysql_con = mysqli_query($sql_insert, $con);
$error = mysqli_error($mysql_con);

?>

<!-- HTML Header Information  -->

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<link rel="stylesheet" type="text/css" href="/style/style.css">
<head>
    <title>Comic Database Results</title>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
</head>

<!-- HTML Body Area -->

<body>
<?php

echo "$idcomic <br />";
echo "$publisher <br />";
echo "$comic_name <br />";
echo "$comic_num <br />";
echo "$comic_cover <br />";
echo "$price_paid <br />";
echo "$quantity <br />";
echo "<br />";
echo $sql_insert;
echo $error;
?>

<?php
// Close Connection
mysqli_close($con);

?>

I've got some code I was playing with to do checks against values but alot of the values are set in the form itself. 我有一些正在使用的代码可以对值进行检查,但是很多值都是在表单本身中设置的。

Also I was doing those echos at the end just to see the values coming over from the html form and I've also got some other code that is the connection info to the DB as well as the error (if there is one) and that comes back just fine. 我还在最后做这些回声,只是为了查看html表单中的值,我还得到了一些其他代码,即与数据库的连接信息以及错误(如果有的话),并且回来就好了。

Any help would be great. 任何帮助都会很棒。 I apologize again if this is simple I can't seem to get it so I throw myself on the alter. 如果这很简单,我再次道歉,但我似乎无法理解,因此我将自己投入了另一半。

尝试对包含变量的字符串使用双引号(如“值”部分中的INSERT查询字符串)

Since the $con should have the database selected already, all you need to do is tell it the table you want to input the values. 由于$ con应该已经选择了数据库,因此您要做的就是告诉它要输入值的表。 The following string should work for you. 以下字符串应为您工作。

$sql_insert = "INSERT INTO comic_tb (idcomic_db, publisher, comic_name, comic_num, comic_cover, price_paid, quantity) VALUES ('$idcomic_db', '$publisher', '$comic_name', '$comic_num', '$comic_cover', '$price_paid', '$quantity')";

Make sure all your fields and variables are correct. 确保所有字段和变量都正确。

My connection statement was reversed. 我的连接语句被颠倒了。

($con, $sql_insert)
($sql_insert, $con)

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

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