Musicdad
Musicdad

Reputation: 55

My variables being passed to AJAX are numeric only

I am trying to pass variables to my ajax call to insert them into my mysql database. Everything seems to be working fine, except its only passing numbers to ajax. if the string is "1486 somewhere st." it only inserts "1486" into the database and inserts 0 in all the alpha only fields. is there a variable rule I am missing. I will post a partial code here.

HTML:

SaveCustomer($("#CustName").val(), $("#CustAddress1").val())
...
function SaveCustomer($Name, $Addr1){     
  $.ajax({ 
    type: "POST", 
    url: "savecust.php",
    data: {"Name": $Name, "Addr1": $Addr1}, 
    dataType: 'json',
    success: function(result){      
    }
  }); 
}

PHP:

$DName = $_POST["Name"];
$DAddr1 = $_POST["Addr1"] ;
$stmt = $con->prepare("INSERT INTO tblCustomers (CustomerName, CustomerAddress) VALUES (?,?)");
$stmt->bind_param("ii", $DName, $DAddr1); 
$stmt->execute(); 
$stmt->close();

Upvotes: 0

Views: 45

Answers (1)

chris85
chris85

Reputation: 23880

You need to update your binding type(s).

$stmt->bind_param("ss", $DName, $DAddr1);

s corresponding variable has type string

http://php.net/manual/en/mysqli-stmt.bind-param.php

This assumes you want both to be strings; if you want address as an integer use i.

Upvotes: 2

Related Questions