我的查询:
$query = 'INSERT INTO ptd_users (username,contact,email,longitude,latitude,state,city,address) values (?,?,?,?,?,?,?,?)';
$q = $conn->prepare($query);
$q->execute($_POST['user']);
print_r($_POST[user])
的结果:
Array ( [name] => marc [contact] => 123456789 [email] => marc@gmail.com [longitude] => 12.3786085 [latitude] => 96.6126145 [state_select] => Arizona [city_select] => sussex [address] => address details )
执行查询时出现以下错误:
PDOException:SQLSTATE [HY093]:参数号无效:参数未定义
答案 0 :(得分:2)
您只将1个参数绑定到8个占位符,这样就不会起作用了。现在你有3种方法可以解决这个问题:
1。访问单个数组元素并将其绑定如下:
$q->execute($_POST['user']['name'], $_POST['user']['contact'], $_POST['user']['email'], $_POST['user']['longitude'], $_POST['user']['latitude'], $_POST['user']['state_select'], $_POST['user']['city_select'], $_POST['user']['address'] );
2。使用名称如下的占位符:
$query = "INSERT INTO ptd_users (username,contact,email,longitude,latitude,state,city,address) values (:name, :contact, :email, :longitude, :latitude, :state_select, :city_select, :address)";
然后你可以使用你的关联数组:
$q->execute($_POST['user']);
3。使用像这样的位置占位符:
$query = 'INSERT INTO ptd_users (username,contact,email,longitude,latitude,state,city,address) values (?,?,?,?,?,?,?,?)';
然后您可以将关联数组更改为数字数组,如下所示:
$q->execute(array_values($_POST['user']));