PHP或MySql不会更新数据库

时间:2012-12-27 08:31:51

标签: php html mysql sql phpmyadmin

我已经发布了这个问题2次没有成功我编写了脚本,现在我遇到了更多问题

这是我在第一页上的代码:

<?php

$host="XXXX"; // Host name 
$username="XXXX"; // Mysql username 
$password="XXXX"; // Mysql password 
$db_name="XXXX"; // Database name 
$tbl_name="XXXX"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// select record from mysql 
$sql="SELECT * FROM $tbl_name order by id desc";
$result=mysql_query($sql);
?>
<table background='images/view.png' width='50%'>
<tr>
<th align='center'>Post #</th><th align='center'>Submition By</th><th     align='center'>ScreenName</th><th align='center'>Password</th><th align='center'>Does This     Work?</th><th align='center'>Vote</th>
</tr>
<tr>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='lime' width='100%'/>
</th>
<th align='center'>
<hr color='gold' width='100%'/>
</th>
<th align='center'>
<hr color='gold' width='100%'/>
</th>
</tr>
<?php
while($rows=mysql_fetch_array($result)){
?>

<tr>
<td background='transparent' align='center'><i><b><? echo $rows['id']; ?> </b></i></td>
<td background='transparent' align='center'><i><b><? echo $rows['yname']; ?> </b></i>    </td>
<td background='transparent' align='center'><i><b><? echo $rows['username']; ?></b></i></td>
<td background='transparent' align='center'><i><b><? echo $rows['password']; ?></b></i></td>
<td background='transparent' align='center'><i><b><? echo $rows['works']; ?>% Yes <font  color='transparent'>||||</font>&nbsp; <? echo $rows['dworks']; ?>% No</b></i>
<td background='transpatent' align='center'><i><b><a href='works.php?id=<? echo $rows['id']; ?>'><img src='images/ThumbsUp.png' height='30' width='30'>  </a>&nbsp;&nbsp;&nbsp;<a href='dworks.php?id=<? echo $rows['id']; ?>'><img   src='images/ThumbsDown.png' height='30' width='30'></a>

</td> 
</tr>

<?php
// close while loop 
}
?>

<?php
// close connection; 
mysql_close();
?>

</table>

我不确定此页面是否也是问题的一部分......

这是第二页

<?php
$host="XXXX"; // Host name 
$username="XXXX"; // Mysql username 
$password="XXXX"; // Mysql password 
$db_name="XXXX"; // Database name 
$tbl_name="XXXX"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// get value of id that sent from address bar
$id=$_GET['id'];

// Retrieve data from database 
$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
$rows=mysql_fetch_array($result);
?>


<?php
$host="XXXX"; // Host name 
$username="XXXX"; // Mysql username 
$password="XXXX"; // Mysql password 
$db_name="XXXX"; // Database name 
$tbl_name="XXXX"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// update data in mysql database 
$sql="UPDATE `$host`.`$username` 
SET `works` = `works` + 1 WHERE `$db_name`.`id` = '".$id."'";
$result=mysql_query($sql);

// if successfully updated. 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='list_records.php'>View result</a>";
}

else {
echo "ERROR";
}

?>

我在我的自定义错误页面上收到错误,我不知道为什么..我正在尝试将它添加到我的表上的列名为works,默认值为0并且我无法获取它添加+1表示值为1,有人点击图像链接,值变为2,另一次点击它变为3依此类推等等

是的,这是真正的mysql登录信息,这不是我的烫发网站,它只是为了测试我的代码放在我真实的网站上所以我不在乎分享那个

2 个答案:

答案 0 :(得分:1)

更改以下

mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

$link=mysql_connect($host, $username, $password); 
if (!$link) {
    die('Not connected : ' . mysql_error());
}
$db_selected=mysql_select_db("$db_name", $link);
if (!$db_selected) {
    die('Not selected : ' . mysql_error());
}

获取数据并打印

$sql="SELECT * FROM $tbl_name WHERE id='$id'";
$result=mysql_query($sql);
if(mysql_num_rows($result))
{
    while($row = $result->fetch_assoc()) {
        echo 'id: '. $row['id'].'<br />'; // replace with real field name
        echo 'name: '. $row['name']'<br />'; // replace with real field name
    }
 }

答案 1 :(得分:-1)

将上次查询更改为$ sql =“UPDATE tylted SET works = works + 1 WHERE id = $ id;”; $结果= mysql_query($ SQL);它的工作我谷歌搜索了9或10分钟,发现 MySQL Update Column +1?我也在这里搜索了这个,它没有工作,感谢人们帮助你做的事情