我試圖使用PHP將值插入到MySQL表中。使用PHP將值插入MySQL表
PHP代碼:
<?php
$host = "localhost";
$user = "xcode";
$pass = "xcode";
$db = "xcode";
$r = mysql_connect($host, $user, $pass);
if (!$r) {
echo "Could not connect to server\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "Connection established\n";
}
echo mysql_get_server_info() . "\n";
$r2 = mysql_select_db($db);
if (!$r2) {
echo "Cannot select database\n";
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "database selected\n";
}
$sql = "INSERT INTO name(name) VALUES('$_POST[name]')";
if (!mysql_query($sql)) {
trigger_error(mysql_error(), E_USER_ERROR);
} else {
echo "1 record added";
}
mysql_close();
?>
Objective-C代碼:
NSString *strURL = [NSString stringWithFormat:@"http://192.168.1.12:81/priya/sample.php?name=%@", txtname.text];
NSData *dataURL = [NSData dataWithContentsOfURL:[NSURL URLWithString:strURL]];
NSString *strResult = [[NSString alloc] initWithData:dataURL encoding:NSUTF8StringEncoding];
NSLog(@"%@", strResult);
我的數據庫連接成功,但價值不被插入。錯誤在哪裏?
你得到什麼錯誤? – Rikesh
回顯查詢,直接在數據庫環境中運行它。看看有什麼錯誤。此外,如果這是你的代碼的確切副本,那麼應該在查詢附近有一個空格:..(name)values .. –
沒有錯誤,但值不插入sql表中 –