【发布时间】:2013-08-24 11:42:18
【问题描述】:
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 列,设置如下:
标识 | 10001
其中 id 是一列,10001 是一列。
我一直在网上寻找并尝试使用几种方法,包括:
- http://www.mustbebuilt.co.uk/php/insert-update-and-delete-with-mysqli/
- http://codular.com/php-mysqli
- mysqli does not save to my database
- http://www.w3schools.com/php/php_mysql_insert.asp
- Send html form data to sql database via php (using mysqli)
但我不知道为什么它们都不起作用。我收到各种错误,例如意外 10001 或意外;
使用上面的代码,我得到以下错误: 解析错误:语法错误、意外的 '10001' (T_LNUMBER)、期望变量 (T_VARIABLE) 或 '$'
我想将第一个复选框中的值(value=10001)保存到表列 10001
提前感谢您的帮助。
编辑 1:
需要更改代码和数据库才能使其正常工作。
在数据库中,表列名需要以字母开头,因此将其从 10001 更改为 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 提供的解决方案。
【问题讨论】: