1

I'm working on an app. I've published a few apps, but I only have limited experience with PHP. This app uses a mysql database and a php script to pass data from the app to the database. I've figured out how to use POST to get data from the input fields in the app to the database, but for some reason I can't figure out how to pass a variable created in php to the database, i.e., without using POST.

The variable I'm having trouble with is a user_id variable. I'm going to create it within the registration.php script, which also passes the inputs from the app via POST. Here's the relevant portion of the code. Everything works except the user_id variable never makes it to the database (i.e., the column always shows '0').

EDIT: In the database, the user_id column is INT(11) type.

//I have a whole script prepared for creating the unique user_id, but to keep it simple for 
// testing, I'm just using '0000000'.  
// This part doesn't work.
$query = "INSERT INTO users (user_id) VALUES ('0000000')"; 
mysql_query($query);

// everything from here down works: 
$query = "INSERT INTO users (username, password, email, firstname, lastname) VALUES ( :user, :pass, :email, :firstname, :lastname)";

$query_params = array(
    ':user' => $_POST['username'],
    ':pass' => $_POST['password'], 
    ':email' => $_POST['email'],
    ':firstname' => $_POST['firstName'], 
    ':lastname' => $_POST['lastName'],
);


try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {

    $response["success"] = 0;
    $response["message"] = "Failed to run query: " . $ex->getMessage();
    die(json_encode($response));
}
4
  • Did you get any errors? With an empty table, did your code added a new row with the value 0 for the field user_id? Commented Jul 8, 2016 at 1:34
  • Leading zeroes will be ignored because thw dataType of the column is int. So 00000000 is just 0. Try using 1 and see what happens Commented Jul 8, 2016 at 2:23
  • @s-hunter No errors... the table is not empty. I manually created the new column user_id using phpMyAdmin. Yes, it adds a zero to the column each time a new user is created. Commented Jul 8, 2016 at 12:42
  • @Terminus Damn it! I always screw up things like that! I'll try using 1 instead when I get home. Thanks. Commented Jul 8, 2016 at 12:43

2 Answers 2

2

mysql_query is not part of the PDO class that you use in your working code below.

Use the PDO class to execute that statement too.

$query = "INSERT INTO users (user_id) VALUES (:uid)"; 

$query_params = array(
   ':uid' => '0000000'
);


try {
$stmt   = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {

$response["success"] = 0;
$response["message"] = "Failed to run query: " . $ex->getMessage();
die(json_encode($response));
}

It's also curious why you say that you're inserting '000000' and the result is always 0 - this makes sense.

Sign up to request clarification or add additional context in comments.

1 Comment

I think you're right... Of course 000000 becomes 0 when using an INT datatype! What was I thinking. I'm going to use a 1 and see if that works when I get home. Thanks.
0

For anyone with the same problem, the comments and responses were right... I had two problems. First, '0000000' is treated as '0' when dealing with an INT datatype (DUH!), so of course my database was always receiving '0'. Second, mysql_query is not part of the PDO class I was using. I revised the code and now it works:

$userid = '1'; 

$query = "INSERT INTO users (username, password, email, firstname, lastname, user_id) VALUES ( :user, :pass, :email, :firstname, :lastname, :uid)";

$query_params = array(
    ':user' => $_POST['username'],
    ':pass' => $_POST['password'], 
    ':email' => $_POST['email'],
    ':firstname' => $_POST['firstName'], 
    ':lastname' => $_POST['lastName'],
    ':uid' => $userid
);

try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {

    $response["success"] = 0;
    $response["message"] = "Failed to run query: " . $ex->getMessage();
    die(json_encode($response));
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.