如果不在更新查询中工作
if else not working in update query
我在 if else 条件下遇到问题。对不起,我是初学者所以请帮助我。我的代码如下。
$sql1=mysql_query("select * from dbms_master_data where id='".$_GET['eid']."'");
$rst1=mysql_fetch_array($sql1);
$new_user='Contact; '.$rst1['user_type'];
$sqls=mysql_query("update dbms_master_data set user_type='".$new_user."', cid='".$_GET['vid']."' where id='".$_GET['eid']."' and cid IS NULL");
if($sqls)
{
?>
<script>
alert("New contact added!");
window.location='access1.php?vid=1';
</script>
<?php
}
else
{
?>
<script>
alert("This contact is already assigned to another user!");
window.location='access1.php?vid=2';
</script>
<?php
}
即使我的条件失败,我也会收到 'New contact added' 消息。你能告诉我为什么吗?
你的条件有误
if($sqls) 它总是 return true,因为 mysql_query() 响应 returns 资源 ID。
使用mysql_num_rows()
。它用于检查受影响的行数
$sqls=mysql_query("update dbms_master_data set user_type='".$new_user."', cid='".$_GET['vid']."' where id='".$_GET['eid']."' and cid IS NULL");
$row=mysql_num_rows($sqls);
if($row>0)
{
?>
<script>
alert("New contact added!");
window.location='access1.php?vid=1';
</script>
<?php
}
else
{
?>
<script>
alert("This contact is already assigned to another user!");
window.location='access1.php?vid=2';
</script>
<?php
}
如上所述使用 if(mysql_num_rows($sqls) > 0) { 但考虑使用 PDO,PHP 不再推荐 mysql_query 所以值得您在掌握时PDO 如果你正在学习。
还要确保对进入数据库的任何内容进行转义,如果您不这样做,人们可以很容易地注入内容。示例(在您的查询上下文中):
$sql1=mysql_query("select * from dbms_master_data where id=".(int)$_GET['eid']);
$rst1=mysql_fetch_array($sql1);
$new_user='Contact; '.$rst1['user_type'];
$sqls=mysql_query("update dbms_master_data set user_type='".mysql_real_escape_string($new_user)."', cid=".(int)$_GET['vid']." where id=".(int)$_GET['eid']." and cid IS NULL");
需要检查是否更新。
所以请使用
mysql_affected_rows()
我在 if else 条件下遇到问题。对不起,我是初学者所以请帮助我。我的代码如下。
$sql1=mysql_query("select * from dbms_master_data where id='".$_GET['eid']."'");
$rst1=mysql_fetch_array($sql1);
$new_user='Contact; '.$rst1['user_type'];
$sqls=mysql_query("update dbms_master_data set user_type='".$new_user."', cid='".$_GET['vid']."' where id='".$_GET['eid']."' and cid IS NULL");
if($sqls)
{
?>
<script>
alert("New contact added!");
window.location='access1.php?vid=1';
</script>
<?php
}
else
{
?>
<script>
alert("This contact is already assigned to another user!");
window.location='access1.php?vid=2';
</script>
<?php
}
即使我的条件失败,我也会收到 'New contact added' 消息。你能告诉我为什么吗?
你的条件有误
if($sqls) 它总是 return true,因为 mysql_query() 响应 returns 资源 ID。
使用mysql_num_rows()
。它用于检查受影响的行数
$sqls=mysql_query("update dbms_master_data set user_type='".$new_user."', cid='".$_GET['vid']."' where id='".$_GET['eid']."' and cid IS NULL");
$row=mysql_num_rows($sqls);
if($row>0)
{
?>
<script>
alert("New contact added!");
window.location='access1.php?vid=1';
</script>
<?php
}
else
{
?>
<script>
alert("This contact is already assigned to another user!");
window.location='access1.php?vid=2';
</script>
<?php
}
如上所述使用 if(mysql_num_rows($sqls) > 0) { 但考虑使用 PDO,PHP 不再推荐 mysql_query 所以值得您在掌握时PDO 如果你正在学习。
还要确保对进入数据库的任何内容进行转义,如果您不这样做,人们可以很容易地注入内容。示例(在您的查询上下文中):
$sql1=mysql_query("select * from dbms_master_data where id=".(int)$_GET['eid']);
$rst1=mysql_fetch_array($sql1);
$new_user='Contact; '.$rst1['user_type'];
$sqls=mysql_query("update dbms_master_data set user_type='".mysql_real_escape_string($new_user)."', cid=".(int)$_GET['vid']." where id=".(int)$_GET['eid']." and cid IS NULL");
需要检查是否更新。 所以请使用
mysql_affected_rows()