Mysql rows not being populated

I am using the Python Request Module to post data to my database on line and it seems to be working except the data is not being moved to the database ... meaning it creates new row in the table but there is no data in the rows ...

Rows: 3
key1 	key2
  	 
  	 
  	 

Open in new window


import requests
value1 = "100"
value2 = 'Clay'
payload = {'key1a': value1, 'key2a': value2}
r = requests.post("http://semsnation.com/aaa.php", params=payload)
#r = requests.get("http://httpbin.org/get", params=payload)
print r.url

Open in new window

the Python code generates this data
http://semsnation.com/aaa.php?key2a=Clay&key1a=100

Open in new window


This is  the PHP code I use
<?php

$dbhost = 'Transfer.xxxx.xxx.com';
$dbuser = 'TransferData';
$dbpass = 'xxxx';
$dbname='TransferData';
$tbl_name="DataMove"; // Table name


// Connect to server and select database.
mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");

// Get values from form
$key1 = mysql_real_escape_string($_POST['key1a']);
$key2 = mysql_real_escape_string($_POST['key2a']);

// Insert data into mysql
$sql="INSERT INTO $tbl_name(key1, key2)VALUES('$key1', '$key2')";

$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
}

else {
echo "ERROR";
}
?>

Open in new window

dolamitejenkinsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Tomas Helgi JohannssonCommented:
Hi!

What does $_POST['key1a'] and $_POST['key2a'] return ? What about
$_REQUEST['key1a'] and $_REQUEST['key2a']  ? Have you tried $_GET[]. +
A QueryString  is accessed using $_GET.

Also try to change ' to " like $_POST["key1a"].

You will probably need to have the values transfered to variables before inserting like this

$tempkey1 = $_POST['key1a']
$tempkey2 = $_POST['key2a']
$key1 = mysql_real_escape_string($tempkey1);
$key2 = mysql_real_escape_string($tempkey2);

$sql="INSERT INTO $tbl_name(key1, key2)VALUES('$key1', '$key2')";
....

Regards,
     Tomas Helgi
0
Ray PaseurCommented:
I see nothing wrong with the PHP code.  Suggest we add some debugging techniques to the script.  I cannot test this, so please try it and let us know what you see.
<?php
error_reporting(E_ALL);

// DATA BASE CREDENTIALS
$dbhost = 'Transfer.xxxx.xxx.com';
$dbuser = 'TransferData';
$dbpass = 'xxxx';
$dbname='TransferData';
$tbl_name="DataMove"; // Table name


// Connect to server and select database.
mysql_connect("$dbhost", "$dbuser", "$dbpass")or die("cannot connect");
mysql_select_db("$dbname")or die("cannot select DB");

// SHOW VALUES IN THE FORM
var_dump($_POST);

// Get values from form
$key1 = mysql_real_escape_string($_POST['key1a']);
$key2 = mysql_real_escape_string($_POST['key2a']);

// Insert data into mysql
$sql="INSERT INTO $tbl_name(key1, key2)VALUES('$key1', '$key2')";

$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
    echo "Successful";
    echo "<BR>";
}

else {
    echo "ERROR";
}

// LOOK FOR THE DATA JUST INSERTED
$sql="SELECT * FROM $tbl_name WHERE key1 = '$key1' ORDER BY key1";
$res = mysql_query($sql);
if (!$res) trigger_error("FAIL: $sql " . mysql_error());
while ($row = mysql_fetch_object($res))
{
    var_dump($row);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Ray PaseurCommented:
Sidebar note.  MySQL is being removed from PHP, so you will need a different data base API.  This article will show you why that is being done and what you must do to keep your scripts running.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
Newly released Acronis True Image 2019

In announcing the release of the 15th Anniversary Edition of Acronis True Image 2019, the company revealed that its artificial intelligence-based anti-ransomware technology – stopped more than 200,000 ransomware attacks on 150,000 customers last year.

dolamitejenkinsAuthor Commented:
array(0) { }
Notice: Undefined index: key1a in /home/content/15/6807515/html/aaa.php on line 18

Notice: Undefined index: key2a in /home/content/15/6807515/html/aaa.php on line 19

Successful
Back to main page

object(stdClass)#1 (2) { ["key1"]=> string(0) "" ["key2"]=> string(0) "" } object(stdClass)#2 (2) { ["key1"]=> string(0) "" ["key2"]=> string(0) "" } object(stdClass)#1 (2) { ["key1"]=> string(0) "" ["key2"]=> string(0) "" } object(stdClass)#2 (2) { ["key1"]=> string(0) "" ["key2"]=> string(0) "" } object(stdClass)#1 (2) { ["key1"]=> string(0) "" ["key2"]=> string(0) "" } object(stdClass)#2 (2) { ["key1"]=> string(0) "" ["key2"]=> string(0) "" } object(stdClass)#1 (2) { ["key1"]=> string(0) "" ["key2"]=> string(0) "" } 

Open in new window

0
Ray PaseurCommented:
OK, it looks like the POST-method request is not reaching the script, since the first var_dump($_POST) showed array(0).

Let's try the same script again, but change $_POST to $_GET in all places.
0
PortletPaulfreelancerCommented:
Strongly endorse reading Ray's excellent article on the deprecated MySQL API!

the MySQL API  is deprecated as at PHP 5.5 and will be removed
(i.e. MySQLi or PDO should be used instead)

(sorry Ray, I get a little anxious when I see MySQL being removed :)
0
Tomas Helgi JohannssonCommented:
Hi!

Check your php.ini for register_globals (as of version 4.2.0 ) that variable is default off.

Also check the variables_order for the $_GET AND $_POST this variable should at least contain "GP" but I recommend that you set it to "EGPCS"
From the PHP manual for variables_order
"Sets the order of the EGPCS (Environment, Get, Post, Cookie, and Server) variable parsing. For example, if variables_order is set to "SP" then PHP will create the superglobals $_SERVER and $_POST, but not create $_ENV, $_GET, and $_COOKIE. Setting to "" means no superglobals will be set. "

Regards,
      Tomas Helgi
0
Ray PaseurCommented:
@PortletPaul: Yes, the removal of the MySQL API is going to be something of an earthquake for the millions of programming novices who have been copying "found bits" of code from the mean streets of the internet, without understanding what the code is actually doing.  Here are some possible scenarios...

1. Their hosting company will upgrade and they will start getting deprecated messages.  Mass panic will ensue.

2. Their code will start failing.  Mass panic will ensue.

3. Their hosting company will branch a PHP 5.3 server to give them time to upgrade, but they will never upgrade.

4. They are hosting their sites on their own servers, and they will never upgrade.

5. They will be forced to refactor because their applications have some economic or social value, and it will be a long, costly exercise.

There is no good way out of this conundrum.  Either PHP continues to support a truly horrible language interface, or PHP takes a step forward into the 21st century.  They have chosen the latter path.  And if that's a bit troublesome for millions of novice web developers, well, tough break.  They all got PHP for "free" and now the price of "free" is going up, way up!

Of course this means a huge amount of business for PHP programmers who can help refactor the old spaghetti code ;-)
0
dolamitejenkinsAuthor Commented:
I am new to PHP ... this is my php5.ini ... I dont see anything  referencing global variables
[PHP]
engine = On
session.bug_compat_warn = 0
AcceptPathInfo
cgi.fix_pathinfo=1
max_execution_time = 300     ; Maximum execution time of each script, in seconds
max_input_time = 600	; Maximum amount of time each script may spend parsing request data
memory_limit = 128M      ; Maximum amount of memory a script may consume (8MB)
post_max_size = 300M
default_mimetype = "text/html"
default_charset = "UTF-8"
file_uploads = On
upload_max_filesize = 400M
allow_url_fopen = On
user_agent="PHP"
default_socket_timeout = 60

Open in new window

0
Ray PaseurCommented:
I do not think there is anything here that interacts with register_globals

You can determine the actual settings of the PHP variables with this script, shown here in its entirety.

<?php phpinfo();

Open in new window

Let's go back to this comment and try the $_GET suggestion.
0
dolamitejenkinsAuthor Commented:
After further testing I discovered this is not a PHP problem ... it is a python Problem... while it appears my code is sending it actually is not transmitting the data ... any Python opinions ?
0
dolamitejenkinsAuthor Commented:
Thanks I learned a lot about PHP debugging this code
0
Ray PaseurCommented:
No Python ideas, but I think there is a Python Zone here at EE (I think), so a question there might get you some insight.  Thanks for the points and good luck with your project, ~Ray
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.