Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Oct 2012
    Posts
    25
    Thanks
    10
    Thanked 0 Times in 0 Posts

    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();
        }
    }

    ?>

  • #2
    Regular Coder
    Join Date
    Nov 2012
    Posts
    115
    Thanks
    7
    Thanked 12 Times in 12 Posts
    I'd say, do two things:

    1. Comment out the $db->query($sql); and put
    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 }

  • Users who have thanked Thyrosis for this post:

    sakina (01-10-2013)

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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(); 

  • Users who have thanked Fou-Lu for this post:

    sakina (01-10-2013)

  • #4
    New Coder
    Join Date
    Oct 2012
    Posts
    25
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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!

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.

  • Users who have thanked Fou-Lu for this post:

    sakina (01-10-2013)

  • #6
    New Coder
    Join Date
    Oct 2012
    Posts
    25
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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.

  • #7
    New Coder
    Join Date
    Oct 2012
    Posts
    25
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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?

  • #8
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •