0

I'm having difficulties send data to MySQL via PHP and a Jquery ajax call, I'm new to this so apologies if its something obvious - any help would be appreciated.

connect database php

<?php
 DEFINE ('DB_USER', 'user account');
 DEFINE ('DB_PSWD', 'password');
 DEFINE ('DB_HOST', 'localhost');
 DEFINE ('DB_NAME', 'dbname');
 $dbcon = mysqli_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME);     
?>

call.php:

<?php   
include(connect_db.php);
$name = $_POST['name'];
$age = $_POST['age'];

mysql_query("INSERT INTO user ('name', 'age') VALUES ('$name','$age')");
  ?>

ajax call:

The name and age are added via function

function send (name, age) {
 $.ajax({
type: "POST",
url : "scripts/call.php",           
data : "name=" + name + "&age=" + age,
success: function(data) {
    console.log('success');
},
error: function () {
        console.log('failed');
}
  });
 }

I do get a success console.lo however no data is every shown in the database.

Thanks, Stephen

  • If you were to visit the call.php page with the paramaters in the url do you see anything? I.E. [yoursite].com/scripts/call.php?name=bob&age=50. Does the php code actually work for you? – jkw4703 Apr 18 '14 at 18:43
  • @jkw4703 That will only work if the PHP code uses `$_GET` instead of `$_POST`. – showdev Apr 18 '14 at 19:03
  • No it will work either way. That way you can see if it's a PHP error or if it really is an ajax problem like people here think it is. My bet is on a PHP error. See post below for what I use – jkw4703 Apr 18 '14 at 19:06
  • @jkw4703 Here's a demonstration: http://codepad.viper-7.com/zjgHUE?foo=bar Maybe you are referring to `$_REQUEST`. – showdev Apr 18 '14 at 19:13
  • @showdev That I don't know. I'm not an expert but I know that if i go to my test site and put in the url with the code relevent to this post that it will add it to the DB. In my php I am using $_POST to get the values and in my jquery I am also telling it to use post. Above that I don't know. But I admit you may be right. If so can you point me to some references? – jkw4703 Apr 18 '14 at 19:29
  • @jkw4703 Fair enough. Maybe you are using [`register_globals`](http://www.php.net/manual/en/security.globals.php). [`$_POST`](http://www.php.net/manual/en/reserved.variables.post.php) is defined as "An associative array of variables passed to the current script via the HTTP POST method" whereas [`$_GET`](http://www.php.net/manual/en/reserved.variables.get.php) is defined as "An associative array of variables passed to the current script via the URL parameters.". In any case, your idea to troubleshoot the PHP independently from the AJAX is a good one. – showdev Apr 18 '14 at 19:43

3 Answers3

1

You're mixing mysqli_* and mysql_* calls which wont work, you have to use only one. Also I'd suggest using prepared statement with parameterized queries.

How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
Musa
  • 96,336
  • 17
  • 118
  • 137
0

I've never been able to get the mysqli_connect to work. I always use this

$con=mysql_connect(DB_HOST,DB_USER,DB_PASSWORD);
if (!$con) {
    die('Could not connect: ' . mysql_error());
}

Then I use mysql_select_db to select the database like so:

mysql_select_db($database) or die(mysql_error());

Then this runs my query:

$sql = "INSERT INTO user ('name', 'age') VALUES ('$name','$age')";

$query = mysql_query($sql,$con);

if(!$query)
{
  die('Could not enter data: ' . mysql_error());
}

mysql_close($con);

Sorry. I'm not very good at explaining why or how this works, but it works for me. If it doesn't run check out the page by typing in the url in the browser to see if it gives you any more specific information.

jkw4703
  • 352
  • 3
  • 17
  • Thanks for the above - I used this along with both Kalyan and Amit Soni recommendations of how to send the valid data this is now working. – safc2005 Apr 20 '14 at 07:44
-1

first you need to pass valid data

{"name": name, "age" : age}

and then in call.php

$results = json_decode(file_get_contents('php://input'));
$name = $results->name;
$age = $results->age;
Amit Soni
  • 1,437
  • 1
  • 9
  • 18