0

So far, the first $sql = “INSERT INTO MySQLtable statement shown below will insert a new row with static text VALUES ('statictext', 'statictext'). Question is what is right syntax, method, or whatever to set Encodable variable userName to php variable $userName to insert a new row with variable VALUES ('$userName', '$userEmail')? Tried numerous variations, but so far all insert blank column entries:

// This $sql will insert new row with static text entries:
$sql = "INSERT INTO `MySQLtable`(`userName`, `userEmail`) VALUES ('statictext', 'statictext')";

Solution Update: The Swift JSONEncoder().encode(Post) to the database server, of course first requires decoding the Post to set its contents php variables, so that the following completes the Encodable-php path:

session_start();

$PostContents = json_decode(file_get_contents("php://input"), true);
$userName = $PostContents["userName"];
$userEmail = $PostContents["userEmail"];

$sql = "INSERT INTO `MySQLtable`(`userName`, `userEmail`) VALUES ('$userName', '$userEmail')";
ehounder
  • 373
  • 1
  • 2
  • 9
  • PHP variables should have $ at the stat. Your 'userName and userEmail' variables don't. But if they are contained in the 'userInfo[]' array then they should be userInfo['userName'] and userInfo['userEmail'] – CharlesEF Apr 22 '18 at 00:15
  • @CharlesEF Actually userInfo[0] and userInfo[1] are the FaceBook Login respective username and useremail values passed forward from the Login viewController --> which are then assigned to the USERvalues: Encodable {values} --> which are then Posted-encoded to the $sql command --> wherein the syntax (userName, userEmail) without ' 'or `` works in the corresponding GET Decodable $sql calls my app makes ... so that, after filling out the dataTask error fields in my real code and not getting any error message, I'm really wondering what the hang up is... – ehounder 4 mins ago edit – ehounder Apr 22 '18 at 00:45

2 Answers2

0

Your post shows this line:

USERvalues(userName: "Arthur Dent", userEmail: "improbabilitydrive@gmail.com")

If that is the actual name of the variable then you should access them as:

USERvalues['userName'] and USERvalues['userEmail']

But since I have no way of knowing what other things are going on then this suggestion may not help.

CharlesEF
  • 608
  • 1
  • 15
  • 25
  • Well, thanks for trying. SNAFU remains, substituting in $sql USERvalues['userName'] does not work either. Only way new row will insert is with string notation, e.g., `'pfftttt'`. Why the same struct notation `userName` which works fine with Decodable does not work with Encodable seems mysterious. Also I tried posting as a dictionary - but haven't gotten any dictionary variable syntax to work through php either. Any idea why the downvote without comment, other than obviously a lame question? – ehounder Apr 23 '18 at 01:12
0

Solution is the Swift JSONEncoder().encode(Post) to the database server, of course first requires decoding the Post to set its contents php variables, as indicated by @CharlesEF.

Sure this turns out to be basic programming. However for those, including myself, adventuring to make an app without knowing any basic programming, all the basic swift Encodable and Decodable tutorials I've been scouring for days all missed this link in the chain. SO shows it in several places, e.g., file_get_contents("php://input") or $HTTP_RAW_POST_DATA, which one is better to get the body of JSON request?, also edited in above, which completes the Encodable-php path:

session_start();

$PostContents = json_decode(file_get_contents("php://input"), true);
$userName = $PostContents["userName"];
$userEmail = $PostContents["userEmail"];
ehounder
  • 373
  • 1
  • 2
  • 9