我的mysql表在许多字段上接受NULL值,我更新记录,我的桌面应用程序正在创建一个http字符串,如下所示并发送到php脚本。
www.webpage/script.php?firstval=48.345345&secondval=234&thirdval=&fourthval=simon
db thirdval上的已经为NULL 但http字符串中的参数可能包含也可能不包含值
我需要: A)将http字符串中的参数传递给 b)将httpstring中的参数传递给 c)满足php脚本中的空值( d)根本不在http字符串中包含参数 或者是其他东西 我的phpscript是这样的:
?php
DEFINE ('DBUSER', 'generic01');
DEFINE ('DBPW', 'genpass');
DEFINE ('DBHOST', 'mysql4.xxxxxxxxx.com');
DEFINE ('DBNAME', '_Places');
$dbc = mysqli_connect(DBHOST,DBUSER,DBPW);
if (!$dbc) {
die("Database connection failed: " . mysqli_error($dbc));
exit();
}
$dbs = mysqli_select_db($dbc, DBNAME);
if (!$dbs) {
die(" Database selection bit failed: " . mysqli_error($dbc));
exit();
}
$lat = mysqli_real_escape_string($dbc, $_GET['lat']);
$lng = mysqli_real_escape_string($dbc,$_GET['lng']);
$prox = mysqli_real_escape_string($dbc,$_GET['prox']);
$description = mysqli_real_escape_string($dbc,$_GET['description']);
$id = mysqli_real_escape_string($dbc,$_GET['id']);
$direction = mysqli_real_escape_string($dbc,$_GET['direction']);
$avoiddays = mysqli_real_escape_string($dbc,$_GET['avoiddays']);
$validfrom = mysqli_real_escape_string($dbc,$_GET['validfrom']);
$validto = mysqli_real_escape_string($dbc,$_GET['validto']);
$gefid = mysqli_real_escape_string($dbc,$_GET['gefid']);
$expiry = mysqli_real_escape_string($dbc,$_GET['expiry']);
$query = "UPDATE places SET rt_lat = '$lat',rt_lng= '$lng',rt_prox = '$prox', rt_description = '$description', rt_direction = '$direction',rt_avoiddays = '$avoiddays',rt_validto = '$validto',rt_validfrom = '$validfrom',rt_gefid = '$gefid',rt_expiry='$expiry' WHERE rt_id = '$id'";
$result = mysqli_query($dbc, $query) or trigger_error("Query MySQL Error: " . mysqli_error($dbc));
mysqli_close($dbc);
?>
所有帮助表示赞赏,
答案 0 :(得分:0)
PHP不了解SQL null。如果您希望空白/未设置的$ _GET值在数据库中变为空,那么您必须采取特殊步骤:
if(isset($_GET['lat']) || ($_GET['lat'] == '')) {
$lat = 'NULL'; // a plain PHP string with the word "null" in it
} else {
$lat = "'" . mysqli_real_escape_string($dbc, $_GET['lat']) . "'"; // note the extra quotes
}
$sql = "INSERT ... VALUES ($lat, ....)"
如果您以其他方式执行此操作,例如(仅作为示例,是的,它的sql-injection易受攻击):
$sql = "INSERT ... VALUES ('$_GET[lat]', ...)";
然后,对于空$_GET['lat']
,您的查询实际上是
INSERT ... VALUES ('', ...)
你要插入一个空字符串,而不是sql null。
答案 1 :(得分:0)
您不需要在http请求中包含它,但您必须捕获它,否则会出现E_NOTICE错误。
对于所有可以为null的字段:
if (isset($_GET['gefid'])) {
$gefid = mysqli_real_escape_string($dbc,$_GET['gefid']);
} else {
$gefid = null;
}