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.

Community
  • 1
  • 1

2 Answers2

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.

Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
  • 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. –  Aug 21 '13 at 22:40
  • @hennysmafter add the following before line 38... echo $sql_add; and paste what it says here. – Arian Faurtosh Aug 21 '13 at 22:46
  • 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. –  Aug 21 '13 at 22:48
  • @hennysmafter So it isn't picking up the $a10001 value. – Arian Faurtosh Aug 21 '13 at 22:50
  • try setting it yourself to see if it works... $a10001 = '1'; instead of $a10001 = $_POST['10001']; – Arian Faurtosh Aug 21 '13 at 22:50
  • Yes i dont know why it is not picking it up! This is the checkbox: . And i made sure i checked it on the page and then clicked on submit. So it should have no problem with the name and value field. I am going to try your suggestion –  Aug 21 '13 at 22:52
  • I just tried your suggestion and i get the following echo and error(i checked the database and nothing is inserted):INSERT INTO cypg8_testtest (10001) VALUES ('10001') Fatal error: Call to a member function query() on a non-object. When you want to chat than i would be happy to do so. –  Aug 21 '13 at 22:55
  • is this part ($result_add = $mysqli->query($sql_add);) necesarry because i copy paste it from a webpage. i dont need the $result_add part i only need it to do the insert. –  Aug 21 '13 at 22:57
  • @hennysmafter Try the new thing I pasted above in the answer section... I change it... – Arian Faurtosh Aug 21 '13 at 23:03
  • still the same error. echo is INSERT INTO cypg8_testtest (10001) VALUES ('10001') and the Fatal error. Can we chat please! –  Aug 21 '13 at 23:03
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/35952/discussion-between-arian-and-hennysmafter) – Arian Faurtosh Aug 21 '13 at 23:03
  • The solution came from the chat and here is it: $a10001 = $_POST['a10001']; mysqli_query($con,"INSERT INTO cypg8_testtest (a10001) VALUES ('".$a10001."')"); Also the database table column needed to be changed to a10001 and the checkbox name as well to a10001 Arian thanks for all the help. –  Aug 21 '13 at 23:17
-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.

Sujit Poudel
  • 541
  • 7
  • 19
  • 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. –  Aug 21 '13 at 22:44