I'm trying to use web service as a validation for a field (unique).
Highlevel: User types in an ID, web server checks if that exists already in the db then returns true or false. If it exists, user cannot proceed. If it's new, the new ID is saved.
My survey flow is in this way;
- Ask for the user ID from a person (screening).
- Use web service (datapull.php) to send user ID and check if it exists. If it does, will return true and if not false. The data is stored in a SQL database and PHP, I've included code below.
- If ID is new, they complete it and then at the end of it. Web service block sends user ID to the datasave.php to save the info to the SQL server.
I've attached my blocks and code, it doesn't seem to be doing either data saving or data pull properly. I've been trying different ways but couldn't figure out. Any help would be highly appreciated.
datasave.php
$servername = "localhost";
$username = "xxxx";
$password = "xxxxx";
$database = "yyyyyy";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$mturk_id = $_POSTO'MTurkID'];
$batch_id = $_POSTO'BatchID'];
$complete = $_POSTO'Flag'];
$politicallean = $_POSTO'Political'];
// echo "Connected successfully";
$sql = "INSERT INTO pldl_data (mturk_id,batch_id,political,completed) VALUES ({$mturk_id}, {$batch_id}, {$politicallean},{$complete})";
mysqli_query($conn, $sql);
mysqli_close($conn);
?>
datapull.php
// Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$mturk_id = $_POST 'MTurkID'];
$batch_id = $_POST 'BatchID'];
$complete=1;
$sql = "SELECT * FROM pldl_data";
$query_result = FALSE;
if($result = mysqli_query($conn, $sql)){
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_array($result)){
if($rowr'mturk_id'] == $mturk_id){
if($row{'batch_id'] == $batch_id and $rowt'completed']==$complete) $query_result=TRUE;
}
}
mysqli_free_result($result);
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
mysqli_close($conn);
$data = array("duplicate_flag_ext" => $query_result);
header("Content-Type: application/json");
echo json_encode($data);
exit();
?>