新聞中心
php插入mysql數(shù)據(jù)庫怎么id自增,求前后臺代碼
2種辦法
羅源網(wǎng)站建設公司成都創(chuàng)新互聯(lián),羅源網(wǎng)站設計制作,有大型網(wǎng)站制作公司豐富經驗。已為羅源千余家提供企業(yè)網(wǎng)站建設服務。企業(yè)網(wǎng)站搭建\成都外貿網(wǎng)站建設要多少錢,請找那個售后服務好的羅源做網(wǎng)站的公司定做!
1.修改你的數(shù)據(jù)庫表,將ID設為自增長,注:只有主鍵才可以設置為自增長
id自增是由mysql負責的,設置id AUTO_INCREMENT 插入時不用插入id值就能實現(xiàn)自增CREATE TABLE `table ` (`id` INT(8) NOT NULL AUTO_INCREMENT, PRIMARY KEY(`id`));
2.代碼中進行處理
Statement ps;
ResultSet rs;
int id = 0;
..................
String sql="select max(id) as id from message";
rs = ps.executeQuery(sql);//查詢出最大ID
if(rs.next){//如果有ID,原有ID+1
id = rs.getInt("id")+1;
}else{//否則 ID 初始為0
id = 0;
}
//下面接你寫的代碼就OK了
PreparedStatement ps;
String sql="insert message (id,name,msg) value(?,?,?)"
ps.setString(1,id);
ps.setString(2,name);
ps.setString(3,msg);
求教php的數(shù)據(jù)庫表的id 如何讓它在網(wǎng)頁顯示從1開始遞增的序號
首先,id要設置成????AUTO_INCREMENT
其次,創(chuàng)建表的時候 ?設置AUTO_INCREMENT=1
怎么在php中用表單修改指定數(shù)據(jù)庫表中指定ID的數(shù)據(jù)
html
headtitle/title/head
body
?php
//數(shù)據(jù)庫查詢,得出下面表單id與name的值。
?
form name="form1" action="" method="post"
input type="hidden" name="userid" value="?php echo $id ?" /
input type="text" name="username" value="?php echo $name ?" /
input type="submit" name="submit" /
/form
/body/html
?php
if(isset($_POST['submit'])){
$link = mysql_connect('localhost','root','123456') or die(‘連接失敗!’);
mysql_select_db('mydb',$link)or die(‘數(shù)據(jù)庫鏈接失敗!’);
$sql = "update users set user_name='$name' where id='$id'";
$res = mysql_query($sql);
if($res)
echo '修改成功';
else
echo '修改失敗';
}
網(wǎng)站標題:php設置數(shù)據(jù)庫id php數(shù)據(jù)庫默認密碼
鏈接分享:http://www.ef60e0e.cn/article/ddopgds.html