将数据从PHP添加到MYSQL数据库

时间:2019-02-08 09:40:27

标签: php mysql

首先,很抱歉,如果该主题重复。但是我尝试了许多解决方案,但仍然失败。 基本上我想将输入数据添加到mysql表。首先,单击提交时,所有输入均为空白。经过几次尝试和错误后,我设法将输入数据键输入到db表中,但是某些列仍然为空。需要你们的帮助,我该如何解决。

<?php
$servername = "localhost";
$username = "test";
$password = "alltest123";
$dbname = "test";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// Get values from form 
$ixname=($_POST['ixp']);
$peername=($_POST['test2']);
$asname=($_POST['asname']);
$ipv4=($_POST['ipv4']);
$ipv6=($_POST['ipv6']);
$descr=($_POST['test3']);
$node=($_POST['node']);
$category=($_POST['category']);

$sql = "INSERT INTO tbl_peer (ixname, peername, asname, ipv4, ipv6, descr, node, category)
VALUES ('$ixp', '$test2', '$asname', '$ipv4', '$ipv6', '$test3', '$node', '$category')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

form.php

<table width="300" border="0" align="center" cellpadding="0" cellspacing="1">
<tr>
<td><form name="form1" method="post" action="insert_ac.php">
<table width="100%" border="0" cellspacing="1" cellpadding="3">
<tr>
<td colspan="3"><strong>Test Form </strong></td>
</tr>
<tr>
<td width="71">IX Name</td>
<td width="6">:</td>
<td width="301"><input name="ixp" type="text" id="ixp"></td>
</tr>
<tr>
<td>Peer Name</td>
<td>:</td>
<td><input name="test2" type="text" id="test2"></td>
</tr>
<tr>
<td>ASN</td>
<td>:</td>
<td><input name="asname" type="text" id="asname"></td>
</tr>
<tr>
<td>Ipv4 Address</td>
<td>:</td>
<td><input name="ipv4" type="text" id="ipv4"></td>
</tr>
<tr>
<td>Ipv6 Address</td>
<td>:</td>
<td><input name="ipv6" type="text" id="ipv6"></td>
</tr>
<tr>
<td>Description</td>
<td>:</td>
<td><input name="test3" type="text" id="test3"></td>
</tr>
<tr>
<td>Node</td>
<td>:</td>
<td><input name="node" type="text" id="node"></td>
</tr>
<tr>
<td>Category</td>
<td>:</td>
<td><input name="category" type="text" id="category"></td>
</tr>
<tr>
<td colspan="3" align="center"><input type="submit" name="Submit" value="Submit"></td>
</tr>
</table>
</form>
</td>
</tr>
</table>  

PHP和MYSQL版本

PHP 7.0.33-0+deb9u1 (cli) (built: Dec  7 2018 11:36:49) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
    with Zend OPcache v7.0.33-0+deb9u1, Copyright (c) 1999-2017, by Zend Technologies
    with Xdebug v2.5.0, Copyright (c) 2002-2016, by Derick Rethans

mysql  Ver 15.1 Distrib 10.1.37-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2 

错误日志

PHP Notice:  Undefined variable: ixp in /var/www/insert_ac.php on line 25, referer: testb.php
PHP Stack trace:, referer:testb.php
PHP   1. {main}() /var/www/insert_ac.php:0, referer: testb.php
PHP Notice:  Undefined variable: test2 in /var/www/insert_ac.php on line 25, referer: testb.php
PHP Stack trace:, referer: testb.php
PHP   1. {main}() /var/www/insert_ac.php:0, referer: testb.php
PHP Notice:  Undefined variable: test3 in /var/www/insert_ac.php on line 25, referer:testb.php
PHP Stack trace:, referer: testb.php
PHP   1. {main}() /var/www/insert_ac.php:0, referer: testb.php

3 个答案:

答案 0 :(得分:1)

真的是错字。您已将数据从POST数组移至标量变量,但随后在INSERT查询中使用了错误的变量名

$ixname=($_POST['ixp']);
$peername=($_POST['test2']);
$descr=($_POST['test3']);

查询

VALUES ('$ixp', '$test2', '$asname', '$ipv4', '$ipv6', '$test3', '$node', '$category')";
         ^^^     ^^^^^^                                 ^^^^^^^
  

我必须警告您,您的脚本可以向SQL Injection Attack开放   甚至if you are escaping inputs, its not safe!   在MYSQLI_PDO API中使用prepared parameterized statements

答案 1 :(得分:1)

您没有使用正确的变量。您应该像下面那样更改您的SQL查询:

$sql = "INSERT INTO tbl_peer (ixname, peername, asname, ipv4, ipv6, descr, node, category)
    VALUES ('$ixname', '$peername', '$asname', '$ipv4', '$ipv6', '$descr', '$node', '$category')";

此外,您应该在SQL查询中绑定变量,以避免SQL注入。检查链接以获取更多信息:https://www.w3schools.com/php/php_mysql_prepared_statements.asp

希望它可以为您提供帮助。

答案 2 :(得分:0)

嘿,而不是将所有值存储在一个单独的变量中,只需使用 extract()方法,即可得到结果。

// Get values from form 
extract($_POST);

$sql = "INSERT INTO tbl_peer (ixname, peername, asname, ipv4, ipv6, descr, node, category)
VALUES ('$ixp', '$test2', '$asname', '$ipv4', '$ipv6', '$test3', '$node', '$category')";

if (mysqli_query($conn, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

更多参考http://php.net/manual/en/function.extract.php