gpt4 book ai didi

php - 将 PHP 数据添加到 MYSQL 数据库

转载 作者:行者123 更新时间:2023-11-29 06:34:23 24 4
gpt4 key购买 nike

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

<?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);
}

表单.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

最佳答案

确实是简单的拼写错误。您已将数据从 POST 数组移至标量变量,但随后在 INSERT 查询中使用了错误的变量名称

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

查询

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

I have to warn you that Your script is wide open to SQL Injection Attack Even if you are escaping inputs, its not safe! Use prepared parameterized statements in either the MYSQLI_ or PDO API's

关于php - 将 PHP 数据添加到 MYSQL 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54589555/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com