-1

I am trying to gather information via CURL and store that data in my database. I have setup my script as follows:

//extract data from the post
//set POST variables

$cookie_name = "drcuserid";

if(isset($_COOKIE[$cookie_name]))
{
  $cookie = $_COOKIE[$cookie_name];
}
$url = 'http://dirtrif.loc/test.php';
$fields['username'] = $vbulletin->userinfo[username];
$fields['userid'] = $vbulletin->userinfo[userid];
$fields['email'] = $vbulletin->userinfo[email];
$fields['addontitle'] = $info['title'];
$fields['addonversion'] = $info['version'];
$fields['bburl'] = $vbulletin->options[bburl];
$fields['bbtitle'] = $vbulletin->options[bbtitle];
$fields['webmasteremail'] = $vbulletin->options[webmasteremail];
$fields['cookie'] = $_COOKIE[$cookie_name];

//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');

//open connection
$ch = curl_init();

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

All of this data is exactly what I need to get, echoing out $fields_string shows me all the data I requested, echoing $fields shows me it is an array.

With that portion complete I endeavoured on my journey to store this data to my database. This is not stuff I am good at working with so I have been struggling every bit of the way, I have found numerous tutorials and videos but I just cant seem to get this to store the data.

My database is created within the install DB (for testing purposes), this is the query I ran to create my database:

CREATE TABLE IF NOT EXISTS drc_installs
(
  id INT UNSIGNED NOT NULL AUTO_INCREMENT,
  username varchar(255)    NOT NULL,
  userid INT UNSIGNED NOT NULL DEFAULT '0',
  email varchar(255)    NOT NULL,
  addontitle varchar(255)    NOT NULL,
  addonversion varchar(255)    NOT NULL,
  bburl varchar(255)    NOT NULL,
  bbtitle varchar(255)    NOT NULL,
  webmasteremail varchar(255)    NOT NULL,
  cookie varchar(255)    NOT NULL,
  dateline INT UNSIGNED NOT NULL DEFAULT '0',
  PRIMARY KEY (id),
  KEY userid (userid)
)

That all seems to be set up well.

So onto the text.php portion that is supposed to store this data. I've watched a couple tutorials on youtube, I've tried a few different tutorials. I am able to connect to my database, I'm just struggling to insert this data since the way the array returns is not like any of the tutorials I have seen.

After trial and error this is currently the contents of test.php:

$conn = mysqli_connect("localhost", "root", "password", "install") or die("Connection Error: " . mysqli_error($conn));
mysqli_query($conn, "INSERT INTO drc_installs (username,userid,email,addontitle,addonversion,bburl,bbtitle,webmasteremail,cookie) VALUES ('" . $fields['username']. "', '" . $fields['userid']. "', '" . $fields['email']. "', '" . $fields['addontitle']. "', '" . $fields['addonversion']. "', '" . $fields['bburl']. "', '" . $fields['bbtitle']. "', '" . $fields['webmasteremail']. "', '" . $fields['cookie']. "')");

I have played with things like:

if(isset($_POST['username']))

Yet only became more frustrated because I'm failing to comprehend what exactly I should be grabbing to store the data (and why). The end goal is just to store the data requested; each item into its own column in the database. Please help me see what I am failing to understand, this is all new to me.

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
DrCustUmz
  • 378
  • 4
  • 18
  • 2
    You're going to need to be more specific about where your issue is. The tutorials/videos you followed are using poor practices. You should disregard them and use a newer guide implementing prepared statements, parameterized queries, and proper error reporting. With error reporting the driver will tell you the issues you are running into. – user3783243 Jun 07 '20 at 05:04

1 Answers1

-2

(Answering as if I were someone else after I messed around a little)

You are trying to use variables that are defined for curl, when the data submitted to test.php should be referenced as $_POST['ArrayValue'] you cant call variables if they dont exist. Replacing your $fields variables with $_post will allow the data to be submitted to the database.

test.php:

if(isset($_POST['username'])){
    $conn = mysqli_connect("localhost", "root", "password", "install") or die("Connection Error: " . mysqli_error($conn));
    mysqli_query($conn, "INSERT INTO drc_installs (username,userid,email,addontitle,addonversion,bburl,bbtitle,webmasteremail,cookie) VALUES ('" . $_POST['username']. "', '" . $_POST['userid']. "', '" . $_POST['email']. "', '" . $_POST['addontitle']. "', '" . $_POST['addonversion']. "', '" . $_POST['bburl']. "', '" . $_POST['bbtitle']. "', '" . $_POST['webmasteremail']. "', '" . $_POST['cookie']. "')");
}

Although leaving yourself wide open to injections by submitting raw post data is not a great idea, may want to look into that.

DrCustUmz
  • 378
  • 4
  • 18
  • Because a variable from one PHP script doesn't appear magically in another? – Your Common Sense Jun 07 '20 at 05:29
  • 2
    There is nothing personal. The voting is solely for the extremely bad practice featured in your answer. The PHP community is endeavoring to get rid of its bad legacy, and the way a query is executed in your answer is one of the worst. Someone may find your answer and this practice will be spread further. Hence we are trying to make answers with bad practices look actually bad. – Your Common Sense Jun 07 '20 at 05:38
  • could you point me to a thread that has a more updated version of connecting and inserting to a database, If you never would have said anything I wouldn't have known any better =) – DrCustUmz Jun 07 '20 at 05:45
  • 2
    Here is [how to connect](https://stackoverflow.com/questions/61867193/best-practices-most-practical-ways-to-implement-mysqli-connections/61868620#61868620) and here is [how to insert](https://stackoverflow.com/questions/7537377/how-to-include-a-php-variable-inside-a-mysql-statement/7537500#7537500) – Your Common Sense Jun 07 '20 at 05:54