我真的很难理解为什么下面的代码没有插入我发送的日期?
<?PHP
$servername = "servername";
$username = "username";
$password = "passwrd";
$dbname = "dbname";
$conn = new PDO ("MysqL:host=$servername;dbname=$dbname", $username, $password);
$conn ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$Name = $_POST['Name'];
$Last = $_POST['Last'];
$email = $_POST['email'];
$pwd = $_POST['pwd'];
$userID = $_POST['userID'];
$sql = "INSERT INTO Users (Name, Last, email, password, userID) VALUES (:var1, :var2, :var3, :var4, :var5) ";
$q = $conn -> prepare($sql);
$q -> execute (array(':var1' => $Name,
':var2' => $Last,
':var3' => $email,
':var4' => $pwd,
':var5' => $userID));
?>
如果不是
$Name = $_POST['Name'];
等等……我对这些值进行硬编码,所以我可能会:
$Name = 'Jane';
它将数据添加到数据库,但是使用$_POST,它在执行行中失败.
我的Xcode看起来像:
Nsstring *strURL = [Nsstring stringWithFormat:@"http://www.myserver/AddUser.PHP?Name=%@&Last=%@&email=%@&pwd=%@&userID=%@", Name, Last, email, pwd, userID];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
Nsstring *strResult = [[Nsstring alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
我在SO中经历了大量的教程和问题,我无法看出问题所在.
解决方法:
您的HTTP请求是使用GET协议发送的,但您使用POST协议访问数据.你有三个潜在的修复,
>使用GET更改PHP以读取数据
$email = $_GET [’email’];
>将PHP更改为与模式无关(我的偏好)
$email = $_REQUEST [’email’];
>将您的iPhone应用程序更改为使用帖子发送.