2
Hello dear stackoverflow users,

I am having a strange problem. I have a 2 page form.

page 1 is 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>

page 2 is 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);
?>

The database table is called: cypg8_testtest

This table has 2 columns and is setup as this:

id | 10001

Where id is a column and 10001 is a column.

I have been looking on the internet and tried using several methods including:

But i do not know why none of them are working. I get all kinds of errors like unexpected 10001 or unexpected ;

With the code above i get the following error: Parse error: syntax error, unexpected '10001' (T_LNUMBER), expecting variable (T_VARIABLE) or '$'

I want to save the value(value=10001) from the first checkbox to table column 10001

Thanks for any help in advance.

EDIT 1: <== Solution is in here.

The code and database needed to change to make it work.

In the database the table column name needed to start with a letter so this is changed from 10001 to a10001

Then the checkbox name needed to change also to a10001 to make it correspond with the db table column.

The Save form input needed some changes as well this is easier to see in code. So i put both codes below for easy reference.

page 1 is 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>

page 2 is 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);
?>

Thank you to everyone who helped getting me to the answer. And special thanks for Arian with supplying the solution.

2 Answers 2

2
 $10001 = $POST_['10001'];

First, POST is incorrect, it should be $_POST, your underscore is in the wrong place.

Second, variables should always start with characters... try $a10001 instead

Third, you must concatenate your SQL string with your variable.

TRY:

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

Most important:

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

Column names should not consist of only numbers.

Sign up to request clarification or add additional context in comments.

12 Comments

I am sorry for my late response. I was called away from the computer. I tried your solution but it gives me another error: Fatal error: Call to a member function query() on a non-object on line 38. line 38 is $result_add = $mysqli->query($sql_add); Thank you for answer.
@hennysmafter add the following before line 38... echo $sql_add; and paste what it says here.
INSERT INTO cypg8_testtest (10001) VALUES ('') is what is says. so it does not add the value from the checkbox. i dont know why not.
@hennysmafter So it isn't picking up the $a10001 value.
try setting it yourself to see if it works... $a10001 = '1'; instead of $a10001 = $_POST['10001'];
|
-1

Try using

`col_name`  

to enclose your table column name as well as table name. That's character under the tilde(~) on keyboard. Recommended by mysql usage.

1 Comment

i have added your suggestion to the changes i made with the solution from Arian and i get the same error as described in the comment from Arian. Thank you for answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.