Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-08-2013, 07:54 AM   PM User | #1
sakina
New Coder

 
Join Date: Oct 2012
Posts: 25
Thanks: 10
Thanked 0 Times in 0 Posts
sakina is an unknown quantity at this point
mySQL call not going through

Hello everyone,

I am trying to create a webhook that will pull data from the form on my Unbounce.com landing page and store it in my mySQL database. I am using piece of modified code, and I am stumped on what could be preventing it from posting the info to the database. Can someone please help me learn what I am doing wrong here?

PHP Code:
<?php

define
('DB_NAME''sisbro_sav-unbounc');
define('DB_USER''sisbro_unbounc');
define('DB_PASS''xxx');

function 
db_unbounce_log($data)
{
    
$db = new mysqli('localhost'DB_USERDB_PASSDB_NAME);
    if ( ! 
mysqli_connect_errno())
    {
        
$data['name'] = $db->real_escape_string($data['name']);
        
$data['email'] = $db->real_escape_string($data['email']);
        
$data['phone'] = $db->real_escape_string($data['phone']);
        
$data['city_state'] = $db->real_escape_string($data['city_state']);
        
$data['years_of_experience'] = $db->real_escape_string($data['years_of_experience']);
        
$data['referrer'] = $db->real_escape_string($data['referrer']);
        
        
$sql "INSERT INTO `unbounce_log` (name, email, phone, city_state, years_of_experience, referrer) VALUES ";
        
$sql .= sprintf("('%s', '%s', '%s', '%s', '%s', '%s',)"$data['name'], $data['email'], $data['phone'], $data['city_state'], $data['years_of_experience'], $data['referrer']);
        
$db->query($sql);
        
$db->close();
    }
}

?>
sakina is offline   Reply With Quote
Old 01-08-2013, 08:42 AM   PM User | #2
Thyrosis
New Coder

 
Join Date: Nov 2012
Posts: 72
Thanks: 4
Thanked 11 Times in 11 Posts
Thyrosis is on a distinguished road
I'd say, do two things:

1. Comment out the $db->query($sql); and put
Quote:
echo $sql;
in it's place, so you can see what the query actually is.

2. Add some error handling to $db->query($sql); by replacing it with the following:

PHP Code:
$result $db->query($sql);
if(!
$result) {
  echo 
$mysqli->error;


Last edited by Thyrosis; 01-08-2013 at 08:43 AM.. Reason: remove additional }
Thyrosis is offline   Reply With Quote
Users who have thanked Thyrosis for this post:
sakina (01-10-2013)
Old 01-08-2013, 02:37 PM   PM User | #3
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Printing the error should indicate you have a syntactical fault on that last comma.
This is way more work than you need. Since you are using mysqli you may as well make use of prepared statements instead.
I also wouldn't consider constructing a new connection on each function call. Pass the MySQL connection in with it.
PHP Code:
function db_unbounce_log($dataMySQLi $con)
{
    
$sQry "INSERT INTO unbounce_log (name, email, phone, city_state, years_of_experience, referrer) VALUES (?, ?, ?, ?, ?, ?)";
    if (
$stmt $con->prepare($sQry))
    {
        
// assumed that years_of_experience is the only integer
        
$stmt->bind_param('ssssis'$data['name'], $data['email'], $data['phone'], $data['city_state'], $data['years_of_experience'], $data['referrer']);
        
$stmt->execute();
        
$stmt->close();
    }
}

$con = new MySQLi(...);
db_unbounce_log($yourdata$con);
$con->close(); 
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
sakina (01-10-2013)
Old 01-09-2013, 11:26 PM   PM User | #4
sakina
New Coder

 
Join Date: Oct 2012
Posts: 25
Thanks: 10
Thanked 0 Times in 0 Posts
sakina is an unknown quantity at this point
Thank you both very much for your help!

Thyrosis, I made these changes and I am still having the same problems.

Foui-Lu, when I replace my code with yours, Dreamweaver is picking up an error on line 19 ($con = new MySQLi(...); ). Is there something missing on this line?

I should have been more clear about the error from the beginning. The error message is coming from the Unbounce webhook section, and it is saying "400 Bad Request for url: http://driveforsisbro.com/saveform.php".

Thank you both very much for your help!
sakina is offline   Reply With Quote
Old 01-09-2013, 11:52 PM   PM User | #5
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
I guess it would help if I told you the parameters instead. The ... was for the parameter replacements; $con = new MySQLi('localhost', DB_USER, DB_PASS, DB_NAME); would do it. Its the same as you have but I moved it out of the function and made it a required second parameter.

The 400 is a completely different problem. This indicates that your post data (or get data, it doesn't really matter where $data came from) from the form was deemed malformed by the server and rejected since it doesn't understand what you are trying to do. If you are just using a standard input form:
Code:
<form method="post" action="yourscript.php">
    <input type="text" name="name" />
    <input type="text" name="email" />
    <input type="text" name="phone" />
    <input type="text" name="city_state" />
    <input type="text" name="years_of_experience" />
    <input type="text" name="referrer" />
    <input type="submit" />
</form>
And nothing else, than this is definitely an issue with the host.

My suggestion is to install a local environment to test in. There are several options out there, but I'm afraid I cannot really give recommendations (I've always installed Apache, PHP and MySQL separately, but there are packages such as WAMP for these purpose). Several other developers here use the prepackaged ones, so they may have some advice on these.
If these work on your local environment (configure it the same as your remote environment), than the problem is on the host end.
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
sakina (01-10-2013)
Old 01-10-2013, 12:50 AM   PM User | #6
sakina
New Coder

 
Join Date: Oct 2012
Posts: 25
Thanks: 10
Thanked 0 Times in 0 Posts
sakina is an unknown quantity at this point
I am using a different host, and I am still getting the same error.

I will call my hosting provider and see if they will be able to help point me in the right direction.
sakina is offline   Reply With Quote
Old 01-10-2013, 08:41 AM   PM User | #7
sakina
New Coder

 
Join Date: Oct 2012
Posts: 25
Thanks: 10
Thanked 0 Times in 0 Posts
sakina is an unknown quantity at this point
I am no longer getting an error from Unbounce, but the data is still not being saved. What could be causing it to not send anything to the database?
sakina is offline   Reply With Quote
Old 01-10-2013, 02:51 PM   PM User | #8
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This question is too open. There are dozens of things that could stop data from being written, so you need to start with it from the top down.
How do you call the db_ubounce_log? Did you split up the connection as well? If so, make sure its provided to the function call as the second argument.
Check the error logs as well, there may be more information to work with there.
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:43 PM.


Advertisement
Log in to turn off these ads.