php 更新数据库某一字段

php 更新数据库某一字段,第1张

//经过测试,一个很小的问题,只是你的sql语句写错了。

include_once 'cnn.php'

mysql_query("SET NAMES 'gbk'")//解决汉字 

$result = mysql_query ( "select * from univ_basic_info" )

while ( $row = mysql_fetch_array ( $result ) ) {

    $str1 = strip_tags ($row['univ_kind'])

    var_dump($str1)

    $i= $row['univ_id']

    mysql_query("UPDATE univ_basic_info SET univ_kind='$str1' WHERE 'univ_id'=$i") 

    //注意此sql语句,univ_kind  不应该有引号,而变量$str1则应该带引号

    //你的错误就在于此

}

类页面

<?php

include_once (dirname(__FILE__)."\ConnDB.php")

class SqlHelper{

private $_arr

public $tableName

public function dataBind($key,$value)

{

$this->_arr[$key]=$value

}

public function validate()

{

if(!isset($this->tableName))

{

die("表名不能为空!")

}

if(!isset($this->_arr))

{

die("值与列名未绑定")

}

}

public function update($condition){

foreach($this->_arr as $key =>$value){

$s .= "$key = $value,"

}

$ss = substr($s,0,-1)

$sql = "update $this->tableName set $ss where $condition"

$query = mysql_query($sql)

echo $sql

if($query)

{

echo "数据修改成功"

}

else

{

echo "数据修改失败"

}

}

public function insert()

{

$this->validate()

$keys = ""

$values = ""

foreach($this->_arr as $key=>$value)

{

$keys.="`$key`,"

if($value=="")

{

$values.="null,"

}

else

{

$values.="'$value',"

}

}

$keys = substr($keys,0,strlen($keys)-1)

$values = substr($values,0,strlen($values)-1)

$sql = "insert into `$this->tableName`($keys)values($values)"

$query = mysql_query($sql)

if($query)

{

echo "ok"

}

else

{

echo "no"

}

//echo mysql_error()

//return $query

}

public function Delete($condition)

{

if(!isset($this->tableName))

{

die("表名不能为空!")

}

$sql="delete from $this->tableName where $condition"

$query=mysql_query($sql)

if($query)

{

echo "删除成功!"

}

else

{

echo mysql_error()

}

}

function select($sqls)//读取出符合where子句的所有信息

{

$sql = "select * from `$this->tableName` where $sqls"

$query = mysql_query($sql)

/*

$result = array()

while($rs = mysql_fetch_array($query))

{

$result[]=$rs

}

return $result*/

$num = mysql_num_rows($query)

return $num

}

function selectNew($sqls,$n)//读取出符合where子句的最新的n条信息

{

$sql = "select * from `$this->tableName`

where $sqls

order by `id` desc

limit 0,$n"

$query = mysql_query($sql)

$result = array()

while($rs = mysql_fetch_array($query))

{

$result[]=$rs

}

return $result

}

}

?>

实例化页面

$inserts = new SqlHelper()

$inserts->tableName="user"

$inserts->dataBind("name","bbbb")

$inserts->dataBind("address","")

$inserts->insert()

$inserts = new SqlHelper()

$inserts->tableName="user"

$inserts->dataBind("name","33333")

$inserts->dataBind("address","44444")

$inserts->update("id = 13")

$inserts=new SqlHelper()

$inserts->tableName="user"

$inserts->Delete("id=4")

这是我以前写的类,你看看!


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/6753402.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-27
下一篇 2023-03-27

发表评论

登录后才能评论

评论列表(0条)

保存