mysqli php post插入数据库

时间:2013-08-21 22:18:03

标签: php insert mysqli

Hello dear stackoverflow users,

我遇到了一个奇怪的问题。我有一个2页的表格。

第1页是index.php:

<form action="insert.php" method="post">
<div class="slider"><input type="checkbox" onclick="this.form.checkbox1.checked = this.checked;" id="slider" name="10001" value="10001"><label for="slider"></label></div>  
<input type="checkbox" value="127.20" id="checkbox1" name="chk"/>
<input type="submit">
</form>

第2页是insert.php

<?php
$q=$_GET["q"];
// Load Joomla! configuration file
require_once('configuration.php');
// Create a JConfig object
$config = new JConfig();
// Get the required codes from the configuration file
$server = $config->host;
$username   = $config->user;
$password   = $config->password;
$database = $config->db;

$con = mysqli_connect($server,$username,$password,$database);
if (!$con){
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,$database);

$q = mysqli_real_escape_string($con,$q);

// Save form input
$10001 = $POST_['10001'];
$sql_add = "INSERT INTO cypg8_testtest (10001) VALUES ('$10001')";
$result_add = $mysqli->query($sql_add);

// Close connection
mysqli_close($con);
?>

调用数据库表:cypg8_testtest

此表有2列,设置如下:

id | 10001

其中id是列,10001是列。

我一直在互联网上寻找并试用了几种方法,包括:

但我不知道为什么他们都没有工作。我得到各种错误,如意外的10001或意外;

使用上面的代码我得到以下错误: 解析错误:语法错误,意外的'10001'(T_LNUMBER),期望变量(T_VARIABLE)或'$'

我想将值(值= 10001)从第一个复选框保存到表格列10001

感谢您提前提供任何帮助。

编辑1:&lt; ==解决方案就在这里。

需要更改代码和数据库才能使其正常工作。

在数据库中,表格列名称以字母开头,因此从10​​001更改为a10001

然后复选框名称也需要更改为a10001以使其与db表列对应。

保存表单输入需要进行一些更改,这在代码中更容易看到。所以我把下面的代码放在下面以供参考。

第1页是index.php:

<form action="insert.php" method="post">
<div class="slider"><input type="checkbox" onclick="this.form.checkbox1.checked = this.checked;" id="slider" name="a10001" value="10001"><label for="slider"></label></div>  
<input type="checkbox" value="127.20" id="checkbox1" name="chk"/>
<input type="submit">
</form>

第2页是insert.php

<?php
$q=$_GET["q"];
// Load Joomla! configuration file
require_once('configuration.php');
// Create a JConfig object
$config = new JConfig();
// Get the required codes from the configuration file
$server = $config->host;
$username   = $config->user;
$password   = $config->password;
$database = $config->db;

$con = mysqli_connect($server,$username,$password,$database);
if (!$con){
die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,$database);

$q = mysqli_real_escape_string($con,$q);

// Save form input
$a10001 = $_POST['a10001'];
mysqli_query($con,"INSERT INTO cypg8_testtest (a10001) VALUES ('".$a10001."')");

// Close connection
mysqli_close($con);
?>

感谢所有帮助我找到答案的人。特别感谢Arian提供解决方案。

3 个答案:

答案 0 :(得分:1)

 $10001 = $POST_['10001'];

首先,POST不正确,应该是 $ _ POST ,你的下划线位置错误。

其次,变量应始终以字符开头...尝试$ a10001而不是

第三,必须将SQL字符串与变量连接起来。

TRY:

$a10001 = $_POST['10001'];
$sql_add = "INSERT INTO cypg8_testtest (`10001`) VALUES ('".$a10001."')";

最重要的是:

http://dev.mysql.com/doc/refman/5.1/en/identifiers.html

列名不应只包含数字。

答案 1 :(得分:0)

嘿,你不能让$10001反对变量声明的规则。在变量名称中使用一些字母,如

$a10001

答案 2 :(得分:-1)

尝试使用

`col_name`  

用于包含表列名和表名。这是键盘上波浪号(〜)下的字符。推荐使用mysql。

相关问题