Link to home
Start Free TrialLog in
Avatar of Dolamite Jenkins
Dolamite JenkinsFlag for United States of America

asked on

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

SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
https://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
Avatar of Dolamite Jenkins

ASKER

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

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.
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 :)
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
@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 ;-)
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

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.
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 ?
Thanks I learned a lot about PHP debugging this code
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